"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.pickNextLowerDefinition=exports.pickNextLiveCandidate=exports.isValidLiveEngineMode=exports.buildStreamSwitchPayload=exports.buildLiveCandidatesByEngine=exports.buildLiveCandidates=void 0;var _utils=require("../../utils/utils");function _createForOfIteratorHelper(r,e){var n="undefined"!=typeof Symbol&&r[Symbol.iterator]||r["@@iterator"];if(!n){if(Array.isArray(r)||(n=_unsupportedIterableToArray(r))||e&&r&&"number"==typeof r.length){n&&(r=n);var i=0,t=function(){};return{s:t,n:function(){return i>=r.length?{done:!0}:{done:!1,value:r[i++]}},e:function(r){throw r},f:t}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var o,a=!0,l=!1;return{s:function(){n=n.call(r)},n:function(){var r=n.next();return a=r.done,r},e:function(r){l=!0,o=r},f:function(){try{a||null==n.return||n.return()}finally{if(l)throw o}}}}function _unsupportedIterableToArray(r,e){if(r){if("string"==typeof r)return _arrayLikeToArray(r,e);var n=Object.prototype.toString.call(r).slice(8,-1);return"Object"===n&&r.constructor&&(n=r.constructor.name),"Map"===n||"Set"===n?Array.from(r):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?_arrayLikeToArray(r,e):void 0}}function _arrayLikeToArray(r,e){(null==e||e>r.length)&&(e=r.length);for(var n=0,i=new Array(e);n<e;n++)i[n]=r[n];return i}var buildLiveCandidates=function(r,e){var n=[],i=function(r,e,i,t){var o=(0,_utils.normalizePullUrl)(e);o&&n.push({protocol:r,url:o,fromPrimary:i,mapDefinition:t})},t=function(r,e,n,t){i(r,e,!0,t);var o,a=_createForOfIteratorHelper(Array.isArray(n)?n:[]);try{for(a.s();!(o=a.n()).done;){var l=o.value;i(r,l,!1,t)}}catch(r){a.e(r)}finally{a.f()}},o=((null==r?void 0:r.FlvPlayList)||[]).find(function(r){return r.definition===e});o&&t("flv",o.url,o.backUrlList,o.mapDefinition);var a=((null==r?void 0:r.RtmpPlayList)||[]).find(function(r){return r.definition===e});return a&&t("rtmp",a.url,a.backUrlList,a.mapDefinition),n};exports.buildLiveCandidates=buildLiveCandidates;var buildLiveCandidatesByEngine=function(r,e,n){if("video"===n){var i=[],t=function(r,e,n,t){var o=(0,_utils.normalizePullUrl)(e);o&&i.push({protocol:r,url:o,fromPrimary:n,mapDefinition:t})},o=((null==r?void 0:r.VideoPlayList)||[]).filter(function(r){return r.definition===e}),a=((null==r?void 0:r.HlsPlayList)||[]).filter(function(r){return r.definition===e});return(o.length?o:a).forEach(function(r){!function(r,e,n,i){t(r,e,!0,i);var o,a=_createForOfIteratorHelper(Array.isArray(n)?n:[]);try{for(a.s();!(o=a.n()).done;){var l=o.value;t(r,l,!1,i)}}catch(r){a.e(r)}finally{a.f()}}(r.protocol||r.streamType||"hls",r.url,r.backUrlList,r.mapDefinition)}),i}return buildLiveCandidates(r,e)};exports.buildLiveCandidatesByEngine=buildLiveCandidatesByEngine;var pickNextLowerDefinition=function(r,e){var n=(Array.isArray(e)?e:[]).filter(function(r){return r&&"auto"!==r});if(!n.length)return null;if(!r)return n[n.length-1];var i=n.indexOf(r);return i<0?n[n.length-1]:i>=n.length-1?null:n[i+1]};exports.pickNextLowerDefinition=pickNextLowerDefinition;var pickNextLiveCandidate=function(r){var e=Array.isArray(null==r?void 0:r.candidates)?r.candidates:[];if(!e.length)return null;var n=(null==r?void 0:r.roundMax)||0,i=Number.isFinite(Number(null==r?void 0:r.currentIndex))?r.currentIndex:0,t=Number.isFinite(Number(null==r?void 0:r.roundIndex))&&r.roundIndex>0?r.roundIndex:1,o=e[i]||{},a=i+1,l=e[a],u=t;if(!l){if(n&&t>=n)return null;u=t+1,a=0,l=e[0]}return{current:o,next:l,nextIndex:a,nextRound:u}};exports.pickNextLiveCandidate=pickNextLiveCandidate;var buildStreamSwitchPayload=function(r){var e,n=null!==(e=null==r?void 0:r.now)&&void 0!==e?e:Date.now(),i=null==r?void 0:r.current,t=null==r?void 0:r.next;return{startMs:n,protocolFrom:null==i?void 0:i.protocol,protocolTo:null==t?void 0:t.protocol,fromPrimary:null==i?void 0:i.fromPrimary,reason:null==r?void 0:r.reason,retryIndex:null==r?void 0:r.retryIndex,roundIndex:null==r?void 0:r.roundIndex,fromUrl:null==i?void 0:i.url,toUrl:null==t?void 0:t.url}};exports.buildStreamSwitchPayload=buildStreamSwitchPayload;var SUPPORTED_ENGINE_MODES=["video","live-player","auto"],isValidLiveEngineMode=function(r){return SUPPORTED_ENGINE_MODES.includes(r)};exports.isValidLiveEngineMode=isValidLiveEngineMode;