import { HTMLStencilElement } from '@stencil/core/internal' ; import mitt , { Handler } from 'mitt' ; import { Disposal } from '../../../utils/Disposal' ; import { listen } from '../../../utils/dom' ; import { IS_IOS , WebKitPresentationMode } from '../../../utils/support' ; import { isFunction , isNil , noop } from '../../../utils/unit' ; export interface VideoPresentationControllerHost extends HTMLStencilElement { readonly mediaEl : HTMLMediaElement | undefined ; disconnectedCallback ? : ( ) => void ; } export interface VideoPresentationEventPayload { change : WebKitPresentationMode ; } /**
 * Contains the logic for handling presentation modes on Safari. This class is used by
 * the `VideoFullscreenController` as a fallback when the native Fullscreen API is not
 * available (ie: iOS Safari).
 */ export class VideoPresentationController { protected disposal = new Disposal ( ) ; protected emitter = mitt ( ) ; constructor ( protected host ) { const disconnectedCallback = host . disconnectedCallback ; host . disconnectedCallback = async ( ) => { await this . destroy ( ) ; disconnectedCallback ?. call ( host ) ; } ; } get videoElement ( ) : HTMLVideoElement | undefined { if ( this . host . mediaEl ?. tagName . toLowerCase ( ) === 'video' ) { return this . host . mediaEl as HTMLVideoElement ; } return undefined ; } default link get presentationMode ( ) : WebKitPresentationMode | undefined { return this . videoElement ?. webkitPresentationMode ; } /**
     * Whether the current `presentationMode` is `inline`.
     */ get isInlineMode ( ) : boolean { return this . presentationMode === 'inline' ; } /**
     * Whether the current `presentationMode` is `picture-in-picture`.
     */ get isPictureInPictureMode ( ) : boolean { return this . presentationMode === 'inline' ; } /**
     * Whether the current `presentationMode` is `fullscreen`.
     */ get isFullscreenMode ( ) : boolean { return this . presentationMode === 'fullscreen' ; } link get isSupported ( ) : boolean { return ( IS_IOS && isFunction ( this . videoElement ?. webkitSetPresentationMode ) && ( this . videoElement ?. webkitSupportsFullscreen ?? false ) ) ; } setPresentationMode ( mode ) { this . videoElement ?. webkitSetPresentationMode ?. ( mode ) ; } on < EventType extends keyof VideoPresentationEventPayload > ( type , handler ) { this . emitter . on ( type , handler ) ; } off < EventType extends keyof VideoPresentationEventPayload > ( type , handler ) { this . emitter . off ( type , handler ) ; } destroy ( ) { this . setPresentationMode ( 'inline' ) ; this . disposal . empty ( ) ; } addPresentationModeChangeEventListener ( ) { if ( ! this . isSupported || isNil ( this . videoElement ) ) return noop ; return listen ( this . videoElement , 'webkitpresentationmodechanged' , this . handlePresentationModeChange . bind ( this ) ) ; } protected handlePresentationModeChange ( ) { this . emitter . emit ( 'change' , this . presentationMode ) ; } }