"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 });
const Semifunctional_1 = require("../../../../../Class/Semifunctional");
const _ids_1 = require("../../../../_ids");
const globalComponent_1 = require("../../../../globalComponent");
class RequestPictureInPicture extends Semifunctional_1.Semifunctional {
    constructor(system) {
        super({
            fi: ['media', 'opt'],
            i: ['stop'],
        }, {
            input: {
                media: {
                    ref: true,
                },
            },
        }, system, _ids_1.ID_REQUEST_PICTURE_IN_PICTURE);
        this._exit_picture_in_picture_promise = Promise.resolve();
    }
    _plunk() {
        const { api: { document: { exitPictureInPicture }, }, } = this.__system;
        if (this._unlisten) {
            this._unlisten();
            this._unlisten = undefined;
        }
        if (this._picture_in_picture) {
            this._exit_picture_in_picture_promise = exitPictureInPicture();
            this._picture_in_picture = undefined;
        }
    }
    f({ media, opt }, done) {
        return __awaiter(this, void 0, void 0, function* () {
            yield this._exit_picture_in_picture_promise;
            const globalId = media.getGlobalId();
            (0, globalComponent_1.listenGlobalComponent)(this.__system, globalId, (component) => __awaiter(this, void 0, void 0, function* () {
                if (component === null) {
                    this._plunk();
                }
                else {
                    if (document.pictureInPictureElement === component.$element) {
                        this.err('media is already Picture in Picture');
                    }
                    else {
                        const pictureInPicture = yield component.requestPictureInPicture();
                        const leavePnPListener = (event) => {
                            // console.log('RequestPictureInPicture', 'leavepictureinpicture')
                            this._picture_in_picture = undefined;
                            this._plunk();
                        };
                        component.$element.addEventListener('leavepictureinpicture', leavePnPListener);
                        this._unlisten = () => {
                            component.$element.removeEventListener('leavepictureinpicture', leavePnPListener);
                        };
                        this._picture_in_picture = pictureInPicture;
                    }
                }
            }));
        });
    }
    onIterDataInputData(name) {
        // if (name === 'stop') {
        this._plunk();
        this._backward('media');
        this._backward('opt');
        // }
    }
}
exports.default = RequestPictureInPicture;
//# sourceMappingURL=index.js.map