import type { MediaPlayerAdapter, MediaLifecycleEvent, } from '@financial-times/media-tracking-sdk' import Clip from '.' interface Opts { id?: string layout?: string autoplay?: boolean systemTitle?: string rootContentId?: string } class ClipMediaTrackerAdapter implements MediaPlayerAdapter { player: HTMLMediaElement private clip: Clip private options: Opts constructor(player: HTMLMediaElement, clip: Clip) { this.player = player this.clip = clip this.options = { id: clip.opts.id, layout: clip.opts.layout, autoplay: Boolean(clip.opts.autoplay), systemTitle: clip.opts.systemTitle, rootContentId: clip.opts.rootContentId, } } getCurrentTime(): number { return this.player.currentTime } getDuration(): number { return isNaN(this.player.duration) ? 0 : this.player.duration } getMediaId() { return this.options.id } getMediaName() { return this.options.systemTitle } getVideoType() { return 'clip' } getSourceUrl() { return this.options.id } isAutoplay() { return this.options.autoplay ?? this.player.autoplay } getVideoLayout() { return this.options.layout } getLoopCount() { return this.clip.loops } on(event: MediaLifecycleEvent, callback: (e: Event) => void): void { this.player.addEventListener(event, callback) } off(event: MediaLifecycleEvent, callback: (e: Event) => void): void { this.player.removeEventListener(event, callback) } } export default ClipMediaTrackerAdapter