'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const index$1 = require('./index-2e5758b6.js');
const uiStore = require('./ui-store-583d205a.js');
const index = require('./index-3f8e5441.js');

const canJoinStage = (meeting) => {
    var _a, _b;
    return (((_a = meeting === null || meeting === void 0 ? void 0 : meeting.self) === null || _a === void 0 ? void 0 : _a.permissions.stageEnabled) && ((_b = meeting === null || meeting === void 0 ? void 0 : meeting.self) === null || _b === void 0 ? void 0 : _b.permissions.stageAccess) === 'ALLOWED');
};
const canRequestToJoinStage = (meeting) => {
    var _a, _b;
    return (((_a = meeting === null || meeting === void 0 ? void 0 : meeting.self) === null || _a === void 0 ? void 0 : _a.permissions.stageEnabled) &&
        ((_b = meeting === null || meeting === void 0 ? void 0 : meeting.self) === null || _b === void 0 ? void 0 : _b.permissions.stageAccess) !== 'NOT_ALLOWED');
};

const rtkStageToggleCss = ":host{line-height:initial;font-family:var(--rtk-font-family, sans-serif);font-feature-settings:normal;font-variation-settings:normal}p{margin:var(--rtk-space-0, 0px);padding:var(--rtk-space-0, 0px)}:host{display:block}:host([data-hidden]){display:none}";
const RtkStageToggleStyle0 = rtkStageToggleCss;

var __decorate = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
        r = Reflect.decorate(decorators, target, key, desc);
    else
        for (var i = decorators.length - 1; i >= 0; i--)
            if (d = decorators[i])
                r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
const RtkStageToggle = class {
    constructor(hostRef) {
        index$1.registerInstance(this, hostRef);
        this.stateUpdate = index$1.createEvent(this, "rtkStateUpdate", 7);
        /** Variant */
        this.variant = 'button';
        /** Icon pack */
        this.iconPack = uiStore.defaultIconPack;
        /** Language */
        this.t = uiStore.useLanguage();
        this.stageStatus = 'OFF_STAGE';
        this.state = {
            label: '',
            disabled: false,
            icon: '',
        };
        this.stageCallback = async () => {
            var _a, _b, _c, _d, _e, _f, _g;
            const stageStatus = (_a = this.meeting.stage) === null || _a === void 0 ? void 0 : _a.status;
            if (stageStatus === 'ON_STAGE') {
                await ((_c = (_b = this === null || this === void 0 ? void 0 : this.meeting) === null || _b === void 0 ? void 0 : _b.stage) === null || _c === void 0 ? void 0 : _c.leave());
            }
            if (stageStatus === 'OFF_STAGE') {
                (_e = (_d = this === null || this === void 0 ? void 0 : this.meeting) === null || _d === void 0 ? void 0 : _d.stage) === null || _e === void 0 ? void 0 : _e.requestAccess();
                if (canJoinStage(this.meeting)) {
                    this.states.activeJoinStage = true;
                    this.stateUpdate.emit({ activeJoinStage: true });
                }
            }
            if (stageStatus === 'REQUESTED_TO_JOIN_STAGE') {
                (_g = (_f = this === null || this === void 0 ? void 0 : this.meeting) === null || _f === void 0 ? void 0 : _f.stage) === null || _g === void 0 ? void 0 : _g.cancelRequestAccess();
            }
        };
    }
    connectedCallback() {
        this.meetingChanged(this.meeting);
    }
    stageStatusHandler(meeting, status) {
        this.stageStatus = status;
        if (status === 'ACCEPTED_TO_JOIN_STAGE') {
            meeting.self.setupTracks({ audio: false, video: false });
            this.stateUpdate.emit({ activeJoinStage: true });
        }
        this.state = this.getState();
    }
    disconnectedCallback() {
        var _a, _b;
        (_b = (_a = this.meeting) === null || _a === void 0 ? void 0 : _a.stage) === null || _b === void 0 ? void 0 : _b.removeListener('stageStatusUpdate', (status) => this.stageStatusHandler(this.meeting, status));
    }
    meetingChanged(meeting) {
        var _a, _b, _c, _d;
        if (!meeting)
            return;
        this.stageStatus = (_a = meeting.stage) === null || _a === void 0 ? void 0 : _a.status;
        this.stageStatusHandler(meeting, (_b = meeting.stage) === null || _b === void 0 ? void 0 : _b.status);
        (_d = (_c = this.meeting) === null || _c === void 0 ? void 0 : _c.stage) === null || _d === void 0 ? void 0 : _d.on('stageStatusUpdate', (status) => this.stageStatusHandler(meeting, status));
    }
    getState() {
        let label = '';
        let icon = '';
        let disabled = false;
        switch (this.stageStatus) {
            case 'ON_STAGE': {
                icon = this.iconPack.leave_stage;
                label = this.t('stage_request.leave_stage');
                disabled = false;
                break;
            }
            case 'ACCEPTED_TO_JOIN_STAGE': {
                icon = this.iconPack.join_stage;
                label = this.t('stage_request.request');
                disabled = true;
                break;
            }
            case 'REQUESTED_TO_JOIN_STAGE': {
                icon = this.iconPack.join_stage;
                label = this.t('stage_request.requested');
                disabled = false;
                break;
            }
            default: {
                icon = this.iconPack.join_stage;
                label = this.t('stage_request.request');
                disabled = false;
                break;
            }
        }
        return { label, disabled, icon };
    }
    render() {
        if (!canRequestToJoinStage(this.meeting))
            return index$1.h(index$1.Host, { "data-hidden": true });
        return (index$1.h(index$1.Host, { title: this.state.label }, index$1.h("rtk-tooltip", { placement: "top", kind: "block", label: this.state.label, delay: 600, part: "tooltip" }, index$1.h("rtk-controlbar-button", { part: "controlbar-button", size: this.size, iconPack: this.iconPack, variant: this.variant, label: this.state.label, icon: this.state.icon, onClick: this.stageCallback, disabled: this.state.disabled, showWarning: false }))));
    }
    static get watchers() { return {
        "meeting": ["meetingChanged"]
    }; }
};
__decorate([
    index.SyncWithStore()
], RtkStageToggle.prototype, "meeting", void 0);
__decorate([
    index.SyncWithStore()
], RtkStageToggle.prototype, "iconPack", void 0);
__decorate([
    index.SyncWithStore()
], RtkStageToggle.prototype, "states", void 0);
__decorate([
    index.SyncWithStore()
], RtkStageToggle.prototype, "t", void 0);
RtkStageToggle.style = RtkStageToggleStyle0;

exports.rtk_stage_toggle = RtkStageToggle;
