<commit_before>/**
* Adds cue points support
*/
( function( mw, $ ) { "use strict";

mw.KCuePoints = function( embedPlayer ){
	return this.init( embedPlayer );
};
mw.KCuePoints.prototype = {

	// The bind postfix:
	bindPostfix: '.kCuePoints',
	midCuePointsArray: [],
	liveCuePointsIntervalId: null,

	init: function( embedPlayer ){
		var _this = this;
		// Remove any old bindings:
		this.destroy();
		// Setup player ref:
		this.embedPlayer = embedPlayer;

		// Process cue points
		embedPlayer.bindHelper('KalturaSupport_CuePointsReady' + this.bindPostfix, function() {
			_this.processCuePoints();
			// Add player bindings:
			_this.addPlayerBindings();
			//Set live cuepoint polling
			if (mw.getConfig("EmbedPlayer.LiveCuepoints")){
				_this.requestLiveCuepoints();
			}
		});
	},
	destroy: function(){
		if (this.liveCuePointsIntervalId) {
			clearInterval( this.liveCuePointsIntervalId );
			this.liveCuePointsIntervalId = null;
		}
		$( this.embedPlayer ).unbind( this.bindPostfix );
	},
	/*
	 * Trigger Pre/Post cue points and create Mid cue points array
	 */
	processCuePoints: function() {
		var _this = this;
		var cuePoints = this.getCuePoints();
		this.requestThumbAsset(cuePoints, function(){
			_this.embedPlayer.triggerHelper( 'KalturaSupport_ThumbCuePointsReady' );
		});
		// Create new array with midrolls only
		var newCuePointsArray = [];
		$.each( cuePoints, function( idx, cuePoint ){
			if( _this.getVideoAdType( cuePoint ) == 'pre' || _this.getVideoAdType( cuePoint ) == 'post' ) {
				_this.triggerCuePoint( cuePoint );
			} else {
				// Midroll
				newCuePointsArray.push( cuePoint );
			}
		});

		this.midCuePointsArray = newCuePointsArray;
	},
	requestThumbAsset: function(cuePoints, callback){
		var _this = this;
		var requestArray = [];
		var response = [];
		var requestCuePoints = cuePoints || this.getCuePoints();
		var thumbCuePoint = $.grep(requestCuePoints, function(cuePoint){
			return (cuePoint.cuePointType == 'thumbCuePoint.Thumb');
		});

		$.each(thumbCuePoint, function(index, item) {
			requestArray.push(
				{
					'service': 'thumbAsset',
					'action': 'getUrl',
					'id': item.assetId
				}
			);
			response[index] = { id: item.id, url: null};
		});

		if (requestArray.length){
			// do the api request
			this.getKalturaClient().doRequest( requestArray, function ( data ) {
				// Validate result
				$.each(data, function(index, res) {
					if ( !_this.isValidResult( res ) ) {
						data[index] = null;
					}
				});
				$.each(thumbCuePoint, function(index, item){
					item.thumbnailUrl = data[index];
				});
				if (callback){
					callback();
				}
			} );
		} else {
			if (callback){
				callback();
			}
		}
	},
	getKalturaClient: function() {
		if( ! this.kClient ) {
			this.kClient = mw.kApiGetPartnerClient( this.embedPlayer.kwidgetid );
		}
		return this.kClient;
	},
	isValidResult: function( data ){
		// Check if we got error
		if( !data
			||
			( data.code && data.message )
			){
			return false;
		}
		return true;
	},
	/**
	 * Adds player cue point bindings
	 */
	addPlayerBindings: function(){
		var _this = this;
		// Get first cue point<commit_msg>FEC-1943 #comment Add support for cue point pulling #time 1d 4h<commit_after>/**
* Adds cue points support
*/
( function( mw, $ ) { "use strict";

mw.KCuePoints = function( embedPlayer ){
	return this.init( embedPlayer );
};
mw.KCuePoints.prototype = {

	// The bind postfix:
	bindPostfix: '.kCuePoints',
	midCuePointsArray: [],
	liveCuePointsIntervalId: null,

	init: function( embedPlayer ){
		var _this = this;
		// Remove any old bindings:
		this.destroy();
		// Setup player ref:
		this.embedPlayer = embedPlayer;

		// Process cue points
		embedPlayer.bindHelper('KalturaSupport_CuePointsReady' + this.bindPostfix, function() {
			_this.processCuePoints();
			// Add player bindings:
			_this.addPlayerBindings();
			//Set live cuepoint polling
			if (mw.getConfig("EmbedPlayer.LiveCuepoints")){
				_this.requestLiveCuepoints();
			}
		});
	},
	destroy: function(){
		if (this.liveCuePointsIntervalId) {
			clearInterval( this.liveCuePointsIntervalId );
			this.liveCuePointsIntervalId = null;
		}
		$( this.embedPlayer ).unbind( this.bindPostfix );
	},
	/*
	 * Trigger Pre/Post cue points and create Mid cue points array
	 */
	processCuePoints: function() {
		var _this = this;
		var cuePoints = this.getCuePoints();
		this.requestThumbAsset(cuePoints, function(){
			_this.embedPlayer.triggerHelper( 'KalturaSupport_ThumbCuePointsReady' );
		});
		// Create new array with midrolls only
		var newCuePointsArray = [];
		$.each( cuePoints, function( idx, cuePoint ){
			if( _this.getVideoAdType( cuePoint ) == 'pre' || _this.getVideoAdType( cuePoint ) == 'post' ) {
				_this.triggerCuePoint( cuePoint );
			} else {
				// Midroll
				newCuePointsArray.push( cuePoint );
			}
		});

		this.midCuePointsArray = newCuePointsArray;
	},
	requestThumbAsset: function(cuePoints, callback){
		var _this = this;
		var requestArray = [];
		var response = [];
		var requestCuePoints = cuePoints || this.getCuePoints();
		var thumbCuePoint = $.grep(requestCuePoints, function(cuePoint){
			return (cuePoint.cuePointType == 'thumbCuePoint.Thumb');
		});

		$.each(thumbCuePoint, function(index, item) {
			requestArray.push(
				{
					'service': 'thumbAsset',
					'action': 'getUrl',
					'id': item.assetId
				}
			);
			response[index] = { id: item.id, url: null};
		});

		if (requestArray.length){
			// do the api request
			this.getKalturaClient().doRequest( requestArray, function ( data ) {
				// Validate result
				$.each(data, function(index, res) {
					if ( !_this.isValidResult( res ) ) {
						data[index] = null;
					}
				});
				$.each(thumbCuePoint, function(index, item){
					item.thumbnailUrl = data[index];
				});
				if (callback){
					callback();
				}
			} );
		} else {
			if (callback){
				callback();
			}
		}
	},
	requestLiveCuepoints: function(){
		var _this = this;

		// Create associative cuepoint array to enable comparing new cuepoints vs existing ones
		var cuePoints = this.getCuePoints();
		this.associativeCuePoints = {};
		$.each( cuePoints, function ( index, cuePoint ) {
			_this.associativeCuePoints[cuePoint.id] = cuePoint;
		} );

		//Start live cuepoint pulling
		this.liveCuePointsIntervalId = setInterval(function(){
			var entryId = _this.embedPlayer.kentryid;
			var lastUpdatedAt = _this.getLastUpdateTime() + 1;
			_this.getKalturaClient().doRequest({
					'service': 'cuepoint_cuepoint',
					'action': 'list',
					'filter:entryIdEqual': entryId,
					'filter:objectType':'KalturaCuePointFilter',
					'filter:cuePointTypeEqual':	'thumbCuePoint.Thumb',
					'filter:updatedAtGreaterThanOrEqual' : lastUpdatedAt
				},
				function( data ){
					// if an error pop out:
					if( !data || data.code ){
						// todo: add error handling
						mw.log("Error:: KCuePoints could not retrieve live cuepoints");
						return ;
					}
					_this.updateCuePoints( data.objects );
					_this.embedPlayer.triggerHelper( 'KalturaSupport_CuePointsUpdated', [data.totalCount] );
				}
			);
		}, mw.getConfig("EmbedPlayer.LiveCuepointsRequestInterval") || 10000);
	},
	updateCuePoints: function( rawCuePoints ){
		if (rawCuePoints.length > 0) {
			var _this = this;

			var associativeRawCuePoints = {};
			$.each( rawCuePoints, function ( index, cuePoint ) {
				associativeRawCuePoints[cuePoint.id] = cuePoint;
			} );

			var updatedCuePoints = [];
			//Only add new cuepoints or existing cuepoints which have a newer updateAt value
			$.each( associativeRawCuePoints, function ( index, rawCuePoint ) {
				if ( (!_this.associativeCuePoints[index]) ||
					 ( _this.associativeCuePoints[index] &&
					   _this.associativeCuePoints[index].updatedAt < rawCuePoint.updatedAt ) ) {
					_this.associativeCuePoints[index] = rawCuePoint;
					updatedCuePoints.push( rawCuePoint );
				}
			} );

			if ( updatedCuePoints.length > 0 ) {
				var cuePoints = this.getCuePoints();
				//update cuepoints
				$.merge(cuePoints, updatedCuePoints);
				//update midpoint cuepoints
				$.merge(this.midCuePointsArray, updatedCuePoints);
				//Request thumb asset only for new cuepoints
				this.requestThumbAsset(updatedCuePoints, function(){
					_this.embedPlayer.triggerHelper( 'KalturaSupport_ThumbCuePointsUpdated' );
				});
				// sort the cuePoitns by startTime:
				this.midCuePointsArray.sort( function ( a, b ) {
					return a.startTime - b.startTime;
				} );
			}
		}
	},
	getLastUpdateTime: function(){
		var cuePoints = this.getCuePoints();
		var lastUpdateTime= -1;
		$.each(cuePoints, function(key, cuePoint){
			if (lastUpdateTime < cuePoint.updatedAt){
				lastUpdateTime = cuePoint.updatedAt;
			}
		});
		return lastUpdateTime;
	},
	getKalturaClient: function() {
		if( ! this.kClient ) {
			this.kClient = mw.kApiGetPartnerClient( this.embedPlayer.kwidgetid );
		}
		return this.kClient;
	},
	isValidResult: function( data ){
		// Check if we got error
		if( !data
			||
			( data.code && data.message )
			){
			return false;
		}
		return true;
	},
	/**
	 * Adds player cue point bindings
	 */
	addPlayerBindings: function(){
		var _this = this;
		// Get first cue point
{'fraction_non_alphanumeric': 0.13465735486687036, 'fraction_numerical': 0.001636839807944129, 'mean_word_length': 3.436108422071636, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1079256', 'n_tokens_mistral': 3668, 'n_tokens_neox': 3024, 'n_words': 646}