import { ESLMediaProviderRegistry } from './esl-media-registry' ; import type { ESLMedia } from './esl-media' ; export enum PlayerStates { BUFFERING = 3 , ENDED = 0 , PAUSED = 2 , PLAYING = 1 , UNSTARTED = - 1 , VIDEO_CUED = 5 , UNINITIALIZED = - 2 } export interface MediaProviderConfig { mediaSrc ? : string ; mediaId ? : string ; loop : boolean ; muted : boolean ; controls : boolean ; autoplay : boolean ; title : string ; preload ? : 'none' | 'metadata' | 'auto' | '' ; playsinline ? : boolean ; } export type ProviderType = ( new ( component , config ) => BaseProvider ) & typeof BaseProvider ; export type ProviderObservedParams = 'loop' | 'muted' | 'controls' ; author Alexey Stsefanovich (ala'n), Yuliya Adamskaya, Natallia Harshunova
  export abstract class BaseProvider { static readonly providerName : string ; static parseUrl ( url ) { return null ; } static parseConfig ( component ) { const { loop , muted , controls , autoplay , title , preload , playsinline , mediaId , mediaSrc } = component ; const config = { loop , muted , controls , autoplay , title , preload , playsinline } ; if ( mediaId ) Object . assign ( config , { mediaId } ) ; if ( mediaSrc ) Object . assign ( config , { mediaSrc } ) ; return config ; } protected config : MediaProviderConfig ; protected component : ESLMedia ; protected _el : HTMLElement ; protected _ready : Promise < any > ; public constructor ( component , config ) { this . config = config ; this . component = component ; } /** Wraps _ready promise */ get ready ( ) : Promise < any > { if ( ! this . _ready ) { const res = Promise . reject ( 'Not Initialized' ) ; res . catch ( ( e ) => console . log ( 'Rejected Media Operation: ' , e ) ) ; return res ; } return this . _ready ; } /** Bind the provider instance to the component */ public abstract bind ( ) ; /** Unbind the provider instance from the component */ public unbind ( ) { Array . from ( this . component . querySelectorAll ( '.esl-media-inner' ) ) . forEach ( ( el ) => el . parentNode && el . parentNode . removeChild ( el ) ) ; } /** Provider name */ public get name ( ) : string { return ( this . constructor as typeof BaseProvider ) . providerName ; } returns public abstract get state ( ) : PlayerStates ; returns public abstract get defaultAspectRatio ( ) : number ; returns public abstract get duration ( ) : number ; returns public abstract get currentTime ( ) : number ; /** Low-level provider 'seek to' method implementation */ protected abstract seekTo ( pos ? ) ; /** Low-level provider 'play' method implementation */ protected abstract play ( ) ; /** Low-level provider 'pause' method implementation */ protected abstract pause ( ) ; /** Low-level provider 'stop' method implementation */ protected abstract stop ( ) ; /** Set focus to the inner content */ public focus ( ) { this . _el ?. focus ( ) ; } protected onConfigChange ( param , value ) { this . config [ param ] = value ; } /** Set size for inner content */ public setSize ( width , height ) { if ( ! this . _el ) return ; this . _el . style . setProperty ( 'width' , width === 'auto' ? null : `${ width }px` ) ; this . _el . style . setProperty ( 'height' , height === 'auto' ? null : `${ height }px` ) ; } /**
     * Executes toggle action:
     * If the player is PAUSED then it starts playing otherwise it pause playing
     */ protected toggle ( ) { if ( [ PlayerStates . PAUSED , PlayerStates . UNSTARTED , PlayerStates . VIDEO_CUED ] . includes ( this . state ) ) { return this . play ( ) ; } else { return this . pause ( ) ; } } /** Executes onConfigChange action when api is ready */ public onSafeConfigChange ( param , value ) { this . ready . then ( ( ) => this . onConfigChange ( param , value ) ) ; } /** Executes seekTo action when api is ready */ public safeSeekTo ( pos ) { return this . ready . then ( ( ) => this . seekTo ( pos ) ) ; } /** Executes play when api is ready */ public safePlay ( ) { return this . ready . then ( ( ) => this . play ( ) ) ; } /** Executes pause when api is ready */ public safePause ( ) { return this . ready . then ( ( ) => this . pause ( ) ) ; } returns public safeStop ( ) { return this . ready . then ( ( ) => this . stop ( ) ) ; } returns public safeToggle ( ) { return this . ready . then ( ( ) => this . toggle ( ) ) ; } /**
     * Register current provider.
     * Can be used as a decorator.
     */ public static register ( this ) ; public static register ( this , provider ? ) ; public static register ( this , provider ? ) { provider = provider || this ; if ( provider === BaseProvider ) throw new Error ( '`BaseProvider` can\'t be registered.' ) ; if ( ! ( provider ?. prototype instanceof BaseProvider ) ) throw new Error ( 'Provider should be instanceof `BaseProvider`' ) ; ESLMediaProviderRegistry . instance . register ( provider ) ; } }