import { r as registerInstance, d as createEvent, h, a as Host } from './index-55463a54.js';
import { e as defaultIconPack, i as useLanguage } from './ui-store-76bb0e8e.js';
import { c as canViewChat } from './sidebar-4b23298b.js';
import { S as SyncWithStore } from './index-dafb4b7f.js';

const rtkChatToggleCss = ":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{position:relative;display:block}:host([data-hidden]){display:none}.unread-count{position:absolute;right:var(--rtk-space-4, 16px);box-sizing:border-box;padding:var(--rtk-space-0\\.5, 2px);-webkit-user-select:none;-moz-user-select:none;user-select:none;--tw-bg-opacity:1;background-color:rgba(var(--rtk-colors-brand-500, 33 96 253) / var(--tw-bg-opacity));font-size:12px;color:rgb(var(--rtk-colors-text-on-brand-1000, var(--rtk-colors-text-1000, 255 255 255)));display:flex;height:var(--rtk-space-5, 20px);min-width:var(--rtk-space-5, 20px);align-items:center;justify-content:center;border-radius:9999px;z-index:1}.unread-count-dot{position:absolute;right:var(--rtk-space-3, 12px);z-index:10;--tw-bg-opacity:1;background-color:rgba(var(--rtk-colors-brand-500, 33 96 253) / var(--tw-bg-opacity));border-radius:50%;display:flex;height:var(--rtk-space-4, 16px);width:var(--rtk-space-4, 16px);align-items:center;justify-content:center}:host([variant='horizontal']) .unread-count{right:var(--rtk-space-4, 16px);top:50%;transform:translateY(-50%)}";
const RtkChatToggleStyle0 = rtkChatToggleCss;

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 RtkChatToggle = class {
    constructor(hostRef) {
        registerInstance(this, hostRef);
        this.stateUpdate = createEvent(this, "rtkStateUpdate", 7);
        this.unreadMessageCount = 0;
        /** Variant */
        this.variant = 'button';
        /** Icon pack */
        this.iconPack = defaultIconPack;
        /** Language */
        this.t = useLanguage();
        this.chatActive = false;
        this.canViewChat = false;
        this.pageSize = 11;
        this.onChatUpdate = ({ action, message }) => {
            var _a;
            if (this.chatActive)
                return;
            if (action === 'add' && message.userId !== ((_a = this.meeting) === null || _a === void 0 ? void 0 : _a.self.userId)) {
                if (this.unreadMessageCount <= 10) {
                    this.unreadMessageCount += 1;
                }
            }
        };
        this.toggleChat = () => {
            const states = this.states;
            this.chatActive = !((states === null || states === void 0 ? void 0 : states.activeSidebar) && (states === null || states === void 0 ? void 0 : states.sidebar) === 'chat');
            if (this.chatActive) {
                this.unreadMessageCount = 0;
            }
            this.stateUpdate.emit({
                activeSidebar: this.chatActive,
                sidebar: this.chatActive ? 'chat' : undefined,
                activeMoreMenu: false,
            });
        };
        this.updateCanView = () => {
            this.canViewChat = canViewChat(this.meeting);
        };
    }
    connectedCallback() {
        this.meetingChanged(this.meeting);
        this.statesChanged(this.states);
    }
    disconnectedCallback() {
        var _a, _b, _c, _d, _e, _f;
        (_b = (_a = this.meeting) === null || _a === void 0 ? void 0 : _a.chat) === null || _b === void 0 ? void 0 : _b.removeListener('chatUpdate', this.onChatUpdate);
        (_d = (_c = this.meeting) === null || _c === void 0 ? void 0 : _c.stage) === null || _d === void 0 ? void 0 : _d.removeListener('stageStatusUpdate', this.updateCanView);
        (_f = (_e = this.meeting) === null || _e === void 0 ? void 0 : _e.self) === null || _f === void 0 ? void 0 : _f.permissions.removeListener('chatUpdate', this.updateCanView);
    }
    meetingChanged(meeting) {
        var _a, _b, _c;
        if (!meeting)
            return;
        this.setUnreadMessageCount();
        this.canViewChat = canViewChat(meeting);
        (_a = meeting.chat) === null || _a === void 0 ? void 0 : _a.addListener('chatUpdate', this.onChatUpdate);
        (_b = meeting === null || meeting === void 0 ? void 0 : meeting.stage) === null || _b === void 0 ? void 0 : _b.on('stageStatusUpdate', this.updateCanView);
        (_c = meeting === null || meeting === void 0 ? void 0 : meeting.self) === null || _c === void 0 ? void 0 : _c.permissions.on('chatUpdate', this.updateCanView);
    }
    statesChanged(states) {
        if (states != null) {
            this.chatActive = states.activeSidebar === true && states.sidebar === 'chat';
        }
    }
    async setUnreadMessageCount() {
        var _a, _b;
        const chat = this.meeting.chat;
        if (!chat)
            return;
        const meetingStartedTimeMs = (_b = (_a = this.meeting.meta) === null || _a === void 0 ? void 0 : _a.meetingStartedTimestamp.getTime()) !== null && _b !== void 0 ? _b : 0;
        const messages = await chat.fetchPublicMessages({
            timestamp: meetingStartedTimeMs,
            limit: this.pageSize,
            direction: 'after',
        });
        this.unreadMessageCount = messages.length;
    }
    handleChatActiveChange() {
        // Chat sidebar closed without opening a different sidebar
        if (!this.chatActive && !this.states.activeSidebar) {
            this.buttonEl.focus();
        }
    }
    render() {
        if (!this.meeting)
            return null;
        if (!this.canViewChat)
            return h(Host, { "data-hidden": true });
        return (h(Host, { title: this.t('chat') }, this.unreadMessageCount !== 0 && !this.chatActive && (h("div", { class: "unread-count", part: "unread-count" }, h("span", null, this.unreadMessageCount < this.pageSize ? this.unreadMessageCount : '10+'))), h("rtk-controlbar-button", { ref: (el) => (this.buttonEl = el), part: "controlbar-button", size: this.size, iconPack: this.iconPack, class: { active: this.chatActive }, onClick: this.toggleChat, icon: this.iconPack.chat, label: this.t('chat'), variant: this.variant })));
    }
    static get watchers() { return {
        "meeting": ["meetingChanged"],
        "states": ["statesChanged"],
        "chatActive": ["handleChatActiveChange"]
    }; }
};
__decorate([
    SyncWithStore()
], RtkChatToggle.prototype, "meeting", void 0);
__decorate([
    SyncWithStore()
], RtkChatToggle.prototype, "states", void 0);
__decorate([
    SyncWithStore()
], RtkChatToggle.prototype, "iconPack", void 0);
__decorate([
    SyncWithStore()
], RtkChatToggle.prototype, "t", void 0);
RtkChatToggle.style = RtkChatToggleStyle0;

export { RtkChatToggle as rtk_chat_toggle };
