'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 rtkRecordingToggleCss = ":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}";
const RtkRecordingToggleStyle0 = rtkRecordingToggleCss;

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 RtkRecordingToggle = class {
    constructor(hostRef) {
        index$1.registerInstance(this, hostRef);
        this.apiError = index$1.createEvent(this, "rtkApiError", 7);
        /** Variant */
        this.variant = 'button';
        /** Language */
        this.t = uiStore.useLanguage();
        /** Icon pack */
        this.iconPack = uiStore.defaultIconPack;
        /** Disable the button */
        this.disabled = false;
        this.canRecord = false;
        this.permissionsUpdateListener = () => {
            this.canRecord = this.meeting.self.permissions.canRecord === true;
        };
        this.toggleRecording = async () => {
            var _a, _b, _c;
            if (this.isLoading() || this.disabled)
                return;
            switch (this.recordingState) {
                case 'IDLE':
                    try {
                        await ((_a = this.meeting) === null || _a === void 0 ? void 0 : _a.recording.start());
                        return;
                    }
                    catch (_d) {
                        this.apiError.emit({
                            trace: this.t('recording.start'),
                            message: this.t('recording.error.start'),
                        });
                    }
                    return;
                case 'RECORDING':
                    try {
                        await ((_b = this.meeting) === null || _b === void 0 ? void 0 : _b.recording.stop());
                        return;
                    }
                    catch (_e) {
                        this.apiError.emit({
                            trace: this.t('recording.stop'),
                            message: this.t('recording.error.stop'),
                        });
                    }
                    return;
                case 'PAUSED':
                    try {
                        await ((_c = this.meeting) === null || _c === void 0 ? void 0 : _c.recording.resume());
                        return;
                    }
                    catch (_f) {
                        this.apiError.emit({
                            trace: this.t('recording.resume'),
                            message: this.t('recording.error.resume'),
                        });
                    }
                    return;
                case 'STARTING':
                case 'STOPPING':
                default:
                    return;
            }
        };
        this.isLoading = () => {
            return (!this.meeting || this.recordingState === 'STARTING' || this.recordingState === 'STOPPING');
        };
    }
    connectedCallback() {
        this.meetingChanged(this.meeting);
    }
    disconnectedCallback() {
        var _a, _b;
        this.recordingStateUpdateListener &&
            ((_a = this.meeting) === null || _a === void 0 ? void 0 : _a.recording.removeListener('recordingUpdate', this.recordingStateUpdateListener));
        (_b = this.meeting) === null || _b === void 0 ? void 0 : _b.self.permissions.removeListener('permissionsUpdate', this.permissionsUpdateListener);
    }
    meetingChanged(meeting) {
        if (meeting != null) {
            this.recordingState = meeting.recording.recordingState;
            this.permissionsUpdateListener();
            this.recordingStateUpdateListener = (recordingState) => {
                this.recordingState = recordingState;
            };
            meeting.recording.addListener('recordingUpdate', this.recordingStateUpdateListener);
            meeting.self.permissions.addListener('permissionsUpdate', this.permissionsUpdateListener);
        }
    }
    getLabel() {
        switch (this.recordingState) {
            case 'IDLE':
                return 'recording.idle';
            case 'RECORDING':
                return 'recording.stop';
            case 'STARTING':
                return 'recording.starting';
            case 'STOPPING':
                return 'recording.stopping';
            case 'PAUSED':
                return 'recording.resume';
            default:
                return 'recording.loading';
        }
    }
    getIcon() {
        switch (this.recordingState) {
            case 'IDLE':
                return this.iconPack.recording;
            case 'RECORDING':
                return this.iconPack.stop_recording;
            case 'STARTING':
            case 'STOPPING':
            default:
                return this.iconPack.recording;
        }
    }
    render() {
        if (!this.meeting)
            return null;
        if (!this.canRecord)
            return;
        return (index$1.h(index$1.Host, { title: this.t(this.recordingState === 'RECORDING' ? 'recording.stop' : 'recording.idle') }, index$1.h("rtk-controlbar-button", { part: "controlbar-button", size: this.size, iconPack: this.iconPack, onClick: this.toggleRecording, icon: this.getIcon(), isLoading: this.isLoading(), label: this.t(this.getLabel()), variant: this.variant, disabled: this.disabled })));
    }
    static get watchers() { return {
        "meeting": ["meetingChanged"]
    }; }
};
__decorate([
    index.SyncWithStore()
], RtkRecordingToggle.prototype, "meeting", void 0);
__decorate([
    index.SyncWithStore()
], RtkRecordingToggle.prototype, "t", void 0);
__decorate([
    index.SyncWithStore()
], RtkRecordingToggle.prototype, "iconPack", void 0);
RtkRecordingToggle.style = RtkRecordingToggleStyle0;

exports.rtk_recording_toggle = RtkRecordingToggle;
