import * as i0 from '@angular/core';
import { Directive, ViewChild, Input } from '@angular/core';
import { isNull } from '@ironsource/fusion-ui/utils';
import { BehaviorSubject, Subject, fromEvent } from 'rxjs';
import { map, switchMap, startWith } from 'rxjs/operators';

const _c0 = ["videoPlayer"];
class VideoPlayerBaseComponent {
    constructor() {
        this.width = '100%';
        this.height = '100%';
        this.options = {};
        this.isVideoPlaying$ = new BehaviorSubject(false);
        this.videoLoaded$ = new Subject();
    }
    get isAutoplay() {
        return !!this.options.autoplay ? true : null;
    }
    get isTimeline() {
        return this.options.showTimeline ? true : null;
    }
    ngOnInit() {
        this.options.noVideo = this.options.noVideo || {};
        if (!isNull(this.isAutoplay) && this.isAutoplay) {
            this.isVideoPlaying$.next(true);
        }
        this.videoDuration$ = this.getVideoDuration();
        this.videoCurrentTime$ = this.getVideoCurrentTime();
        if (this.isTimeline) {
            this.progressValue$ = this.updateProgress();
        }
    }
    ngOnChanges(changes) {
        if (this.videoPlayer) {
            if (!isNull(this.isAutoplay) && this.isAutoplay) {
                this.isVideoPlaying$.next(true);
            }
            else {
                this.videoPlayer.nativeElement.pause();
                this.isVideoPlaying$.next(false);
            }
        }
    }
    ngAfterViewInit() {
        this.videoLoaded$.next();
    }
    onVideoPlayPauseButtonClicked(event, video) {
        if (this.isVideoPlaying$.getValue()) {
            video.pause();
        }
        else {
            video.play();
        }
        this.isVideoPlaying$.next(!this.isVideoPlaying$.getValue());
        event.stopPropagation();
    }
    getVideoDuration() {
        return this.videoLoaded$.asObservable().pipe(switchMap(_ => fromEvent(this.videoPlayer.nativeElement, 'loadedmetadata').pipe(map(($event) => {
            return parseInt($event.target.duration.toString(), 10);
        }))), startWith(0));
    }
    getVideoCurrentTime() {
        return this.videoLoaded$.asObservable().pipe(switchMap(_ => fromEvent(this.videoPlayer.nativeElement, 'timeupdate').pipe(map(($event) => {
            return parseInt($event.target.currentTime.toString(), 10);
        }))), startWith(0));
    }
    updateProgress() {
        return this.videoLoaded$.asObservable().pipe(switchMap(_ => fromEvent(this.videoPlayer.nativeElement, 'timeupdate').pipe(map(($event) => {
            const player = $event.target;
            return Math.floor((100 / player.duration) * player.currentTime);
        }))), startWith(0));
    }
    static { this.ɵfac = function VideoPlayerBaseComponent_Factory(t) { return new (t || VideoPlayerBaseComponent)(); }; }
    static { this.ɵdir = /*@__PURE__*/ i0.ɵɵdefineDirective({ type: VideoPlayerBaseComponent, viewQuery: function VideoPlayerBaseComponent_Query(rf, ctx) { if (rf & 1) {
            i0.ɵɵviewQuery(_c0, 5);
        } if (rf & 2) {
            let _t;
            i0.ɵɵqueryRefresh(_t = i0.ɵɵloadQuery()) && (ctx.videoPlayer = _t.first);
        } }, inputs: { src: "src", width: "width", height: "height", thumbnail: "thumbnail", options: "options" }, features: [i0.ɵɵNgOnChangesFeature] }); }
}
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(VideoPlayerBaseComponent, [{
        type: Directive
    }], null, { videoPlayer: [{
            type: ViewChild,
            args: ['videoPlayer']
        }], src: [{
            type: Input
        }], width: [{
            type: Input
        }], height: [{
            type: Input
        }], thumbnail: [{
            type: Input
        }], options: [{
            type: Input
        }] }); })();

/**
 * Generated bundle index. Do not edit.
 */

export { VideoPlayerBaseComponent };
//# sourceMappingURL=ironsource-fusion-ui-components-video-player-common-base.mjs.map
