'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 rtkCameraToggleCss = ":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 RtkCameraToggleStyle0 = rtkCameraToggleCss;

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 RtkCameraToggle = class {
    constructor(hostRef) {
        index$1.registerInstance(this, hostRef);
        this.stateUpdate = index$1.createEvent(this, "rtkStateUpdate", 7);
        this.videoUpdateListener = ({ videoEnabled }) => {
            this.videoEnabled = videoEnabled;
        };
        this.stageStatusListener = () => {
            this.stageStatus = this.meeting.stage.status;
            this.canProduceVideo = this.meeting.self.permissions.canProduceVideo === 'ALLOWED';
        };
        this.mediaPermissionUpdateListener = ({ kind, message }) => {
            if (kind === 'video') {
                this.cameraPermission = message;
            }
        };
        this.meetingPermissionsUpdateListener = (patch) => {
            var _a;
            if ((_a = patch === null || patch === void 0 ? void 0 : patch.media) === null || _a === void 0 ? void 0 : _a.video) {
                this.canProduceVideo = this.meeting.self.permissions.canProduceVideo === 'ALLOWED';
            }
        };
        /** Variant */
        this.variant = 'button';
        /** Icon pack */
        this.iconPack = uiStore.defaultIconPack;
        /** Language */
        this.t = uiStore.useLanguage();
        this.videoEnabled = false;
        this.canProduceVideo = false;
        this.cameraPermission = 'NOT_REQUESTED';
        this.stageStatus = 'OFF_STAGE';
        this.toggleCamera = () => {
            var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
            (_a = this.meeting) === null || _a === void 0 ? void 0 : _a.__internals__.logger.info('rtkCameraToggle::toggleCamera', {
                media: {
                    video: {
                        enabled: Boolean((_c = (_b = this.meeting) === null || _b === void 0 ? void 0 : _b.self) === null || _c === void 0 ? void 0 : _c.videoEnabled),
                        permission: this.cameraPermission,
                        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.canProduceVideo,
                    },
                },
                webinar: {
                    stageStatus: (_g = this.meeting) === null || _g === void 0 ? void 0 : _g.stage.status,
                },
                livestream: {
                    stageStatus: (_j = (_h = this.meeting) === null || _h === void 0 ? void 0 : _h.stage) === null || _j === void 0 ? void 0 : _j.status,
                },
                moduleExists: {
                    self: Boolean((_k = this.meeting) === null || _k === void 0 ? void 0 : _k.self),
                },
            });
            if (this.hasPermissionError()) {
                const permissionModalSettings = {
                    enabled: true,
                    kind: 'video',
                };
                this.stateUpdate.emit({ activePermissionsMessage: permissionModalSettings });
                return false;
            }
            const self = (_l = this.meeting) === null || _l === void 0 ? void 0 : _l.self;
            if (self == null || !this.canProduceVideo) {
                return;
            }
            if (self.videoEnabled) {
                self.disableVideo();
            }
            else {
                self.enableVideo();
            }
        };
    }
    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('videoUpdate', this.videoUpdateListener);
        (_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.canProduceVideo = this.meeting.self.permissions.canProduceVideo === 'ALLOWED';
            this.cameraPermission = self.mediaPermissions.video || 'NOT_REQUESTED';
            this.videoEnabled = self.videoEnabled;
            this.stageStatus = meeting.stage.status;
            self.addListener('videoUpdate', this.videoUpdateListener);
            self.addListener('mediaPermissionUpdate', this.mediaPermissionUpdateListener);
            stage === null || stage === void 0 ? void 0 : stage.addListener('stageStatusUpdate', this.stageStatusListener);
            (_b = (_a = meeting === null || meeting === void 0 ? void 0 : meeting.self) === null || _a === void 0 ? void 0 : _a.permissions) === null || _b === void 0 ? void 0 : _b.addListener('permissionsUpdate', this.meetingPermissionsUpdateListener);
        }
    }
    hasPermissionError() {
        return this.cameraPermission === 'DENIED' || this.cameraPermission === 'SYSTEM_DENIED';
    }
    getState() {
        let tooltipLabel = '';
        let label = '';
        let icon = '';
        let classList = {};
        let hasError = this.hasPermissionError();
        let couldNotStart = this.cameraPermission === 'COULD_NOT_START';
        if (this.videoEnabled && !hasError) {
            label = this.t('video_on');
            icon = this.iconPack.video_on;
        }
        else {
            label = this.t('video_off');
            icon = this.iconPack.video_off;
            classList['red-icon'] = true;
        }
        if (couldNotStart) {
            tooltipLabel = this.t('perm_could_not_start.video');
        }
        else if (this.cameraPermission === 'SYSTEM_DENIED') {
            tooltipLabel = this.t('perm_sys_denied.video');
        }
        else if (this.cameraPermission === 'DENIED') {
            tooltipLabel = this.t('perm_denied.video');
        }
        else {
            tooltipLabel = this.videoEnabled ? this.t('disable_video') : this.t('enable_video');
        }
        return {
            tooltipLabel,
            label,
            icon,
            classList,
            showWarning: hasError || couldNotStart,
            disable: hasError,
        };
    }
    render() {
        var _a;
        if (!this.meeting)
            return null;
        if (!this.canProduceVideo ||
            ((_a = this.meeting) === null || _a === void 0 ? void 0 : _a.meta.viewType) === 'AUDIO_ROOM' ||
            ['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", size: this.size, iconPack: this.iconPack, class: classList, variant: this.variant, label: label, icon: icon, onClick: this.toggleCamera, showWarning: showWarning, disabled: disable }))));
    }
    static get watchers() { return {
        "meeting": ["meetingChanged"]
    }; }
};
__decorate([
    index.SyncWithStore()
], RtkCameraToggle.prototype, "meeting", void 0);
__decorate([
    index.SyncWithStore()
], RtkCameraToggle.prototype, "iconPack", void 0);
__decorate([
    index.SyncWithStore()
], RtkCameraToggle.prototype, "t", void 0);
RtkCameraToggle.style = RtkCameraToggleStyle0;

exports.rtk_camera_toggle = RtkCameraToggle;
