"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DEFAULT_STYLE = void 0;
const element_1 = require("../../../../../client/element");
const propHandler_1 = require("../../../../../client/propHandler");
const style_1 = require("../../../../../client/style");
const APINotImplementedError_1 = require("../../../../../exception/APINotImplementedError");
exports.DEFAULT_STYLE = {
    height: '100%',
    width: '100%',
    boxSizing: 'border-box',
    display: 'flex',
    // outline: 'none',
};
class VideoComp extends element_1.Element {
    constructor($props, $system) {
        super($props, $system);
        const { className, style = {}, src, autoplay = true, controls = true, } = this.$props;
        this.$element = this.$system.api.document.createElement('video');
        this.$element.controls = controls;
        if (className) {
            this.$element.className = className;
        }
        if (src) {
            this.$element.src = src;
        }
        this.$element.autoplay = autoplay;
        (0, style_1.applyStyle)(this.$element, Object.assign(Object.assign({}, exports.DEFAULT_STYLE), style));
        this.prop_handler = Object.assign(Object.assign({}, (0, propHandler_1.htmlPropHandler)(this, this.$element, exports.DEFAULT_STYLE)), { src: (src) => {
                if (src === undefined) {
                    this.$element.pause();
                    this.$element.removeAttribute('src'); // empty source
                    this.$element.load();
                }
                else {
                    this.$element.src = src;
                }
            }, stream: (stream) => {
                if (stream === undefined) {
                    this.$element.srcObject = null;
                }
                else {
                    stream.$get({}, (_stream) => {
                        this.$element.srcObject = _stream;
                    });
                }
            }, controls: (controls) => {
                if (controls === undefined) {
                    this.$element.removeAttribute('controls');
                }
                else {
                    this.$element.controls = controls;
                }
            }, currentTime: (t) => {
                if (t === undefined) {
                    //
                }
                else {
                    this.$element.currentTime = t;
                }
            } });
    }
    onPropChanged(prop, current) {
        // console.log('onPropChanged', prop, current)
        this.prop_handler[prop](current);
    }
    captureStream({ frameRate, }) {
        return __awaiter(this, void 0, void 0, function* () {
            // @ts-ignore
            if (this.$element.captureStream) {
                // @ts-ignore
                return this.$element.captureStream(frameRate);
            }
            else {
                throw new APINotImplementedError_1.APINotSupportedError('Capture Stream');
            }
        });
    }
    requestPictureInPicture() {
        return __awaiter(this, void 0, void 0, function* () {
            if (this.$element.requestPictureInPicture) {
                try {
                    return yield this.$element.requestPictureInPicture();
                }
                catch (err) {
                    switch (err.name) {
                        case 'InvalidStateError':
                            throw new Error('video is not loaded');
                        case 'NotAllowedError':
                            throw new Error(`must be handling a user gesture if there isn't already an element in Picture-in-Picture`);
                        default:
                            throw new Error(err.message);
                    }
                }
            }
            else {
                throw new APINotImplementedError_1.APINotSupportedError('Picture-in-Picture');
            }
        });
    }
    play() {
        this.$element.play();
    }
    pause() {
        this.$element.pause();
    }
}
exports.default = VideoComp;
//# sourceMappingURL=Component.js.map