'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 rtkMicToggleCss = ":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}:host(.audioDisabled) :slotted(rtk-icon){--tw-text-opacity:1;color:rgba(var(--rtk-colors-danger, 255 45 45) / var(--tw-text-opacity))}";
const RtkMicToggleStyle0 = rtkMicToggleCss;

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 RtkMicToggle = class {
    constructor(hostRef) {
        index$1.registerInstance(this, hostRef);
        this.stateUpdate = index$1.createEvent(this, "rtkStateUpdate", 7);
        this.audioUpdateListener = ({ audioEnabled }) => {
            this.audioEnabled = audioEnabled;
        };
        this.stageStatusListener = () => {
            this.stageStatus = this.meeting.stage.status;
            this.canProduceAudio = this.meeting.self.permissions.canProduceAudio === 'ALLOWED';
        };
        this.mediaPermissionUpdateListener = ({ kind, message }) => {
            if (kind === 'audio') {
                this.micPermission = message;
            }
        };
        this.meetingPermissionsUpdateListener = (patch) => {
            var _a;
            if ((_a = patch === null || patch === void 0 ? void 0 : patch.media) === null || _a === void 0 ? void 0 : _a.audio) {
                this.canProduceAudio = this.meeting.self.permissions.canProduceAudio === 'ALLOWED';
            }
        };
        /** Variant */
        this.variant = 'button';
        /** Icon pack */
        this.iconPack = uiStore.defaultIconPack;
        /** Language */
        this.t = uiStore.useLanguage();
        this.audioEnabled = false;
        this.canProduceAudio = false;
        this.micPermission = 'NOT_REQUESTED';
        this.stageStatus = 'OFF_STAGE';
        this.toggleMic = () => {
            var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
            (_a = this.meeting) === null || _a === void 0 ? void 0 : _a.__internals__.logger.info('rtkMicToggle::toggleMic', {
                media: {
                    audio: {
                        enabled: Boolean((_c = (_b = this.meeting) === null || _b === void 0 ? void 0 : _b.self) === null || _c === void 0 ? void 0 : _c.audioEnabled),
                        permission: this.micPermission,
                        canProduce: (_f = (_e = (_d = this.meeting) === null || _d === void 0 ? void 0 : _d.self) === null || _e === void 0 ? void 0 : _e.permissions) === null || _f === void 0 ? void 0 : _f.canProduceAudio,
                    },
                },
                webinar: {
                    stageStatus: (_h = (_g = this.meeting) === null || _g === void 0 ? void 0 : _g.stage) === null || _h === void 0 ? void 0 : _h.status,
                },
                livestream: {
                    stageStatus: (_k = (_j = this.meeting) === null || _j === void 0 ? void 0 : _j.stage) === null || _k === void 0 ? void 0 : _k.status,
                },
                moduleExists: {
                    self: Boolean((_l = this.meeting) === null || _l === void 0 ? void 0 : _l.self),
                },
            });
            if (this.hasPermissionError()) {
                const permissionModalSettings = {
                    enabled: true,
                    kind: 'audio',
                };
                this.stateUpdate.emit({ activePermissionsMessage: permissionModalSettings });
                return false;
            }
            const self = (_m = this.meeting) === null || _m === void 0 ? void 0 : _m.self;
            if (self == null || !this.canProduceAudio) {
                return;
            }
            if (self.audioEnabled) {
                self.disableAudio();
            }
            else {
                self.enableAudio();
            }
        };
    }
    connectedCallback() {
        this.meetingChanged(this.meeting);
    }
    disconnectedCallback() {
        var _a, _b, _c, _d, _e, _f, _g;
        (_a = this.meeting) === null || _a === void 0 ? void 0 : _a.self.removeListener('audioUpdate', this.audioUpdateListener);
        (_b = this.meeting) === null || _b === void 0 ? void 0 : _b.self.removeListener('mediaPermissionUpdate', this.mediaPermissionUpdateListener);
        (_d = (_c = this.meeting) === null || _c === void 0 ? void 0 : _c.stage) === null || _d === void 0 ? void 0 : _d.removeListener('stageStatusUpdate', this.stageStatusListener);
        (_g = (_f = (_e = this.meeting) === null || _e === void 0 ? void 0 : _e.self) === null || _f === void 0 ? void 0 : _f.permissions) === null || _g === void 0 ? void 0 : _g.removeListener('permissionsUpdate', this.meetingPermissionsUpdateListener);
    }
    meetingChanged(meeting) {
        var _a, _b;
        if (meeting != null) {
            const { self, stage } = meeting;
            this.canProduceAudio = this.meeting.self.permissions.canProduceAudio === 'ALLOWED';
            this.micPermission = meeting.self.mediaPermissions.audio || 'NOT_REQUESTED';
            this.audioEnabled = self.audioEnabled;
            this.stageStatus = meeting.stage.status;
            self.addListener('audioUpdate', this.audioUpdateListener);
            self.addListener('mediaPermissionUpdate', this.mediaPermissionUpdateListener);
            stage === null || stage === void 0 ? void 0 : stage.addListener('stageStatusUpdate', this.stageStatusListener);
            (_b = (_a = meeting.self) === null || _a === void 0 ? void 0 : _a.permissions) === null || _b === void 0 ? void 0 : _b.addListener('permissionsUpdate', this.meetingPermissionsUpdateListener);
        }
    }
    hasPermissionError() {
        return this.micPermission === 'DENIED' || this.micPermission === 'SYSTEM_DENIED';
    }
    getState() {
        let tooltipLabel = '';
        let label = '';
        let icon = '';
        let classList = {};
        let hasError = this.hasPermissionError();
        let couldNotStart = this.micPermission === 'COULD_NOT_START';
        if (this.audioEnabled && !hasError) {
            label = this.t('mic_on');
            icon = this.iconPack.mic_on;
        }
        else {
            label = this.t('mic_off');
            icon = this.iconPack.mic_off;
            classList['red-icon'] = true;
        }
        if (couldNotStart) {
            tooltipLabel = this.t('perm_could_not_start.audio');
        }
        else if (this.micPermission === 'SYSTEM_DENIED') {
            tooltipLabel = this.t('perm_sys_denied.audio');
        }
        else if (this.micPermission === 'DENIED') {
            tooltipLabel = this.t('perm_denied.audio');
        }
        else {
            tooltipLabel = this.audioEnabled ? this.t('disable_mic') : this.t('enable_mic');
        }
        return {
            tooltipLabel,
            label,
            icon,
            classList,
            showWarning: hasError || couldNotStart,
            disable: hasError,
        };
    }
    render() {
        if (!this.meeting)
            return null;
        if (!this.canProduceAudio ||
            ['OFF_STAGE', 'REQUESTED_TO_JOIN_STAGE'].includes(this.stageStatus)) {
            return index$1.h(index$1.Host, { "data-hidden": true });
        }
        const { tooltipLabel, label, icon, classList, showWarning, disable } = this.getState();
        return (index$1.h(index$1.Host, { title: label }, index$1.h("rtk-tooltip", { kind: "block", label: tooltipLabel, part: "tooltip" }, index$1.h("rtk-controlbar-button", { part: "controlbar-button", icon: icon, label: label, size: this.size, iconPack: this.iconPack, variant: this.variant, class: classList, onClick: this.toggleMic, showWarning: showWarning, disabled: disable }))));
    }
    static get watchers() { return {
        "meeting": ["meetingChanged"]
    }; }
};
__decorate([
    index.SyncWithStore()
], RtkMicToggle.prototype, "meeting", void 0);
__decorate([
    index.SyncWithStore()
], RtkMicToggle.prototype, "iconPack", void 0);
__decorate([
    index.SyncWithStore()
], RtkMicToggle.prototype, "t", void 0);
RtkMicToggle.style = RtkMicToggleStyle0;

exports.rtk_mic_toggle = RtkMicToggle;
