'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 rtkLivestreamToggleCss = ":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 RtkLivestreamToggleStyle0 = rtkLivestreamToggleCss;

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 RtkLivestreamToggle = class {
    constructor(hostRef) {
        index$1.registerInstance(this, hostRef);
        this.stateUpdate = index$1.createEvent(this, "rtkStateUpdate", 7);
        this.apiError = index$1.createEvent(this, "rtkApiError", 7);
        /** Variant */
        this.variant = 'button';
        /** Icon pack */
        this.iconPack = uiStore.defaultIconPack;
        /** Language */
        this.t = uiStore.useLanguage();
        /** Is Livestream active */
        this.livestreamState = 'IDLE';
        this.livestreamStateListener = (state) => {
            this.livestreamState = state;
            if (state === 'LIVESTREAMING' || state === 'IDLE') {
                this.stateUpdate.emit({ activeMoreMenu: false });
            }
        };
        this.isLoading = () => {
            return (!this.meeting || this.livestreamState === 'STARTING' || this.livestreamState === 'STOPPING');
        };
    }
    connectedCallback() {
        this.meetingChanged(this.meeting);
    }
    disconnectedCallback() {
        this.clearListeners();
    }
    meetingChanged(meeting) {
        var _a, _b;
        if (!meeting)
            return;
        this.livestreamState = (_a = this.meeting.livestream) === null || _a === void 0 ? void 0 : _a.state;
        (_b = this.meeting.livestream) === null || _b === void 0 ? void 0 : _b.on('livestreamUpdate', this.livestreamStateListener);
    }
    async toggleLivestream() {
        var _a, _b;
        if (this.livestreamState === 'LIVESTREAMING') {
            try {
                await ((_a = this.meeting.livestream) === null || _a === void 0 ? void 0 : _a.stop());
            }
            catch (_c) {
                this.apiError.emit({
                    trace: this.t('livestream.stop'),
                    message: this.t('livestream.error.stop'),
                });
            }
        }
        else {
            try {
                await ((_b = this.meeting.livestream) === null || _b === void 0 ? void 0 : _b.start());
            }
            catch (_d) {
                this.apiError.emit({
                    trace: this.t('livestream.start'),
                    message: this.t('livestream.error.start'),
                });
            }
        }
    }
    clearListeners() {
        var _a;
        (_a = this.meeting.livestream) === null || _a === void 0 ? void 0 : _a.removeListener('livestreamUpdate', this.livestreamStateListener);
    }
    getLivestreamLabel() {
        switch (this.livestreamState) {
            case 'IDLE':
                return this.t('livestream.go_live');
            case 'LIVESTREAMING':
                return this.t('livestream.end_live');
            case 'WAITING_ON_MANUAL_INGESTION':
                return this.t('livestream.waiting_on_manual_ingestion');
            case 'STARTING':
                return this.t('livestream.starting');
            case 'STOPPING':
                return this.t('livestream.stopping');
            default:
                return this.t('livestream.error');
        }
    }
    getLivestreamIcon() {
        switch (this.livestreamState) {
            case 'IDLE':
                return this.iconPack.start_livestream;
            case 'LIVESTREAMING':
                return this.iconPack.stop_livestream;
            case 'WAITING_ON_MANUAL_INGESTION':
                return this.iconPack.start_livestream;
            case 'STARTING':
            case 'STOPPING':
            default:
                return this.iconPack.stop_livestream;
        }
    }
    render() {
        if (!this.meeting)
            return null;
        if (!uiStore.isLiveStreamHost(this.meeting))
            return index$1.h(index$1.Host, { "data-hidden": true });
        return (index$1.h(index$1.Host, null, index$1.h("rtk-controlbar-button", { part: "controlbar-button", size: this.size, iconPack: this.iconPack, isLoading: this.isLoading(), class: { 'active-livestream': this.livestreamState === 'LIVESTREAMING' }, onClick: () => this.toggleLivestream(), icon: this.getLivestreamIcon(), disabled: this.isLoading(), label: this.t(this.getLivestreamLabel()), variant: this.variant })));
    }
    static get watchers() { return {
        "meeting": ["meetingChanged"]
    }; }
};
__decorate([
    index.SyncWithStore()
], RtkLivestreamToggle.prototype, "meeting", void 0);
__decorate([
    index.SyncWithStore()
], RtkLivestreamToggle.prototype, "iconPack", void 0);
__decorate([
    index.SyncWithStore()
], RtkLivestreamToggle.prototype, "t", void 0);
RtkLivestreamToggle.style = RtkLivestreamToggleStyle0;

exports.rtk_livestream_toggle = RtkLivestreamToggle;
