import { p as proxyCustomElement, H, e as createEvent, h, c as Host } from './p-ae373651.js';
import { S as SyncWithStore } from './p-070aa695.js';
import { c as createDefaultConfig, e as defaultIconPack, i as useLanguage, f as defaultOverrides } from './p-5b3a7f00.js';
import './p-a83ccdbd.js';
import { d as defineCustomElement$5 } from './p-a3bf84d7.js';
import { d as defineCustomElement$4 } from './p-85587a5c.js';
import { d as defineCustomElement$3 } from './p-3e0452c4.js';
import { d as defineCustomElement$2 } from './p-7dd3acf5.js';
import { d as defineCustomElement$1 } from './p-e6245534.js';

const rtkChatSelectorCss = ":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)}.scrollbar{scrollbar-width:thin;scrollbar-color:var(--rtk-scrollbar-color, rgb(var(--rtk-colors-background-600, 60 60 60)))\n    var(--rtk-scrollbar-background, transparent)}.scrollbar::-webkit-scrollbar{height:var(--rtk-space-1\\.5, 6px);width:var(--rtk-space-1\\.5, 6px);border-radius:9999px;background-color:var(--rtk-scrollbar-background, transparent)}.scrollbar::-webkit-scrollbar-thumb{border-radius:9999px;background-color:var(--rtk-scrollbar-color, rgb(var(--rtk-colors-background-600, 60 60 60)))}:host{display:flex;width:100%;flex-direction:column;font-size:14px;position:relative;box-sizing:border-box;color:rgb(var(--rtk-colors-text-1000, 255 255 255))}.chat-header{margin-left:var(--rtk-space-2, 8px);margin-right:var(--rtk-space-2, 8px);gap:var(--rtk-space-2, 8px);padding-left:var(--rtk-space-4, 16px);padding-right:var(--rtk-space-4, 16px);padding-top:var(--rtk-space-3, 12px);padding-bottom:var(--rtk-space-3, 12px);display:flex;flex-direction:row;align-items:center;justify-content:space-between;--tw-bg-opacity:1;background-color:rgba(var(--rtk-colors-background-800, 30 30 30) / var(--tw-bg-opacity));color:rgb(var(--rtk-colors-text-600, 255 255 255 / 0.52));cursor:pointer;border-radius:var(--rtk-border-radius-sm, 4px);border:var(--rtk-border-width-sm, 1px) solid rgb(var(--rtk-colors-background-600, 60 60 60))}.chat-header-label{display:flex;flex-direction:row;align-items:center;justify-content:flex-start;gap:var(--rtk-space-2, 8px)}.dropdown{position:absolute;top:100%;z-index:10;padding:var(--rtk-space-0, 0px);--tw-bg-opacity:1;background-color:rgba(var(--rtk-colors-background-1000, 8 8 8) / var(--tw-bg-opacity));opacity:0;pointer-events:none;border-bottom-right-radius:var(--rtk-border-radius-sm, 4px);border-bottom-left-radius:var(--rtk-border-radius-sm, 4px);left:var(--rtk-space-2, 8px);right:var(--rtk-space-2, 8px);max-width:calc(100vw - var(--rtk-space-4, 16px));box-sizing:border-box}.dropdown.open{opacity:1;pointer-events:auto;border-width:1px;border-top-width:var(--rtk-border-width-none, 0);border-style:solid;--tw-border-opacity:1;border-color:rgba(var(--rtk-colors-background-600, 60 60 60) / var(--tw-border-opacity))}.dropdown rtk-paginated-list{box-sizing:border-box;display:flex;height:30vh;flex-direction:column}.public-chat-group-label{display:flex;cursor:pointer;flex-direction:row;align-items:center;justify-content:flex-start;gap:var(--rtk-space-2, 8px);border-left-width:var(--rtk-border-width-none, 0);border-right-width:var(--rtk-border-width-none, 0);border-top-width:var(--rtk-border-width-none, 0);border-bottom-width:var(--rtk-border-width-none, 0);border-bottom-width:1px;border-style:solid;--tw-border-opacity:1;border-color:rgba(var(--rtk-colors-background-600, 60 60 60) / var(--tw-border-opacity));padding-top:var(--rtk-space-4, 16px);padding-bottom:var(--rtk-space-4, 16px);padding-left:var(--rtk-space-2, 8px);padding-right:var(--rtk-space-2, 8px)}.public-chat-icon{display:flex;align-items:center;justify-content:center;padding:var(--rtk-space-1, 4px);border-radius:9999px;--tw-bg-opacity:1;background-color:rgba(var(--rtk-colors-brand-500, 33 96 253) / var(--tw-bg-opacity))}.public-chat-icon rtk-icon{color:rgb(var(--rtk-colors-text-on-brand-1000, var(--rtk-colors-text-1000, 255 255 255)))}";
const RtkChatSelectorStyle0 = rtkChatSelectorCss;

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 RtkChatSelector = /*@__PURE__*/ proxyCustomElement(class RtkChatSelector extends H {
    constructor() {
        super();
        this.__registerHost();
        this.__attachShadow();
        this.dropdownToggle = createEvent(this, "rtkDropdownToggle", 7);
        this.chatSelectorChange = createEvent(this, "rtkChatSelectorChange", 7);
        /** Config */
        this.config = createDefaultConfig();
        /** Icon pack */
        this.iconPack = defaultIconPack;
        /** Language */
        this.t = useLanguage();
        /** UI Overrides */
        this.overrides = defaultOverrides;
        this.isOpen = false;
        this.showPrivateChat = false;
        this.selectedUser = undefined;
        //NOTE(ikabra): Fix this value once backend supports pagination for participants
        this.pageSize = 100000;
        this.pagesAllowed = 3;
        this.unreadMap = new Map();
        this.disconnectMeeting = (meeting) => {
            var _a, _b, _c, _d;
            const { self, participants } = meeting || {};
            (_a = meeting.chat) === null || _a === void 0 ? void 0 : _a.off('chatUpdate', this.chatUpdateListener);
            (_b = self === null || self === void 0 ? void 0 : self.permissions) === null || _b === void 0 ? void 0 : _b.off('*', this.chatPermissionUpdateListener);
            (_c = participants === null || participants === void 0 ? void 0 : participants.joined) === null || _c === void 0 ? void 0 : _c.off('participantJoined', this.participantJoinedListener);
            (_d = participants === null || participants === void 0 ? void 0 : participants.joined) === null || _d === void 0 ? void 0 : _d.off('participantLeft', this.participantLeftListener);
        };
        this.toggle = (e) => {
            e.preventDefault();
            e.stopPropagation();
            this.isOpen = !this.isOpen;
            this.dropdownToggle.emit({ open: this.isOpen });
        };
        this.selectUser = async (user) => {
            if (user === null || user === void 0 ? void 0 : user.userId) {
                this.unreadMap.set(user.userId, false);
                this.$paginatedListRef.rerenderList();
            }
            this.selectedUser = user;
            this.chatSelectorChange.emit({ selectedUser: user });
            await this.close();
        };
        this.chatPermissionUpdateListener = () => {
            var _a, _b;
            this.showPrivateChat = !!(((_a = this.meeting.self.permissions.chatPrivate) === null || _a === void 0 ? void 0 : _a.canSend) ||
                ((_b = this.meeting.self.permissions.chatPrivate) === null || _b === void 0 ? void 0 : _b.canReceive));
            if (!this.showPrivateChat) {
                this.selectedUser = undefined;
                this.chatSelectorChange.emit({ selectedUser: undefined });
            }
        };
        this.onParticipantUpdate = () => {
            var _a, _b, _c, _d;
            if (!this.selectedUser)
                return;
            const participants = ((_d = (_c = (_b = (_a = this.meeting) === null || _a === void 0 ? void 0 : _a.participants) === null || _b === void 0 ? void 0 : _b.joined) === null || _c === void 0 ? void 0 : _c.toArray) === null || _d === void 0 ? void 0 : _d.call(_c)) || [];
            if (!participants.some((p) => { var _a; return p.id === ((_a = this.selectedUser) === null || _a === void 0 ? void 0 : _a.id); })) {
                this.selectedUser = undefined;
                this.chatSelectorChange.emit({ selectedUser: undefined });
            }
        };
        this.participantJoinedListener = (data) => {
            this.$paginatedListRef.onNewNode(data);
        };
        this.participantLeftListener = (data) => {
            this.$paginatedListRef.onNodeDelete(data.id);
            this.onParticipantUpdate();
        };
        // @ts-ignore - unused params
        this.getParticipants = async (timestamp, size, reversed) => {
            const meeting = this.meeting;
            if (!meeting)
                return [];
            /**
             * FIXME(ikabra): This is a temporary hack in place to handle the initial load
             * and not repeat participants when a user reaches the top of the list.
             * This must be replaced by actual pagination APIs from backend for participants.
             */
            if (!timestamp)
                return [];
            const participants = meeting.participants.joined.toArray();
            return participants;
        };
        this.createPaticipantNodes = (data) => {
            return data.map((participant) => {
                var _a;
                return (h("div", { class: "private-chat-label", id: participant.id, onClick: () => this.selectUser(participant) }, h("rtk-avatar", { size: "sm", participant: participant }), h("span", null, (_a = participant.name) !== null && _a !== void 0 ? _a : this.t('participant')), this.unreadMap.get(participant.userId) ? (h("div", { class: "private-chat-unread-badge" })) : null));
            });
        };
        this.chatUpdateListener = (data) => {
            var _a;
            if (data.action !== 'add')
                return;
            if (!((_a = data.message.targetUserIds) === null || _a === void 0 ? void 0 : _a.length))
                return;
            const selfUserId = this.meeting.self.userId;
            const otherUserId = data.message.targetUserIds.find((id) => id !== selfUserId);
            if (!otherUserId)
                return;
            this.unreadMap.set(otherUserId, true);
            this.$paginatedListRef.rerenderList();
        };
    }
    /** */
    async close() {
        if (!this.isOpen)
            return;
        this.isOpen = false;
        this.dropdownToggle.emit({ open: false });
    }
    connectedCallback() {
        this.meetingChanged(this.meeting);
        this.overridesChanged(this.overrides);
    }
    disconnectedCallback() {
        var _a, _b, _c;
        if (!this.meeting)
            return;
        const { self, participants } = this.meeting;
        (_a = self === null || self === void 0 ? void 0 : self.permissions) === null || _a === void 0 ? void 0 : _a.off('*', this.chatPermissionUpdateListener);
        (_b = participants === null || participants === void 0 ? void 0 : participants.joined) === null || _b === void 0 ? void 0 : _b.off('participantJoined', this.participantJoinedListener);
        (_c = participants === null || participants === void 0 ? void 0 : participants.joined) === null || _c === void 0 ? void 0 : _c.off('participantLeft', this.participantLeftListener);
    }
    overridesChanged(overrides) {
        var _a, _b;
        if (!this.meeting || this.meeting.self)
            return;
        this.showPrivateChat =
            !!(((_a = this.meeting.self.permissions.chatPrivate) === null || _a === void 0 ? void 0 : _a.canSend) ||
                ((_b = this.meeting.self.permissions.chatPrivate) === null || _b === void 0 ? void 0 : _b.canReceive)) && !overrides.disablePrivateChat;
    }
    meetingChanged(meeting, oldMeeting) {
        var _a, _b, _c, _d, _e, _f, _g;
        if (oldMeeting)
            this.disconnectMeeting(oldMeeting);
        if (!meeting || !meeting.chat)
            return;
        this.showPrivateChat =
            !!(((_a = meeting.self.permissions.chatPrivate) === null || _a === void 0 ? void 0 : _a.canSend) ||
                ((_b = meeting.self.permissions.chatPrivate) === null || _b === void 0 ? void 0 : _b.canReceive)) && !this.overrides.disablePrivateChat;
        this.onParticipantUpdate();
        (_c = meeting.chat) === null || _c === void 0 ? void 0 : _c.on('chatUpdate', this.chatUpdateListener);
        meeting.self.permissions.on('*', this.chatPermissionUpdateListener);
        (_e = (_d = meeting === null || meeting === void 0 ? void 0 : meeting.participants) === null || _d === void 0 ? void 0 : _d.joined) === null || _e === void 0 ? void 0 : _e.on('participantJoined', this.participantJoinedListener);
        (_g = (_f = meeting === null || meeting === void 0 ? void 0 : meeting.participants) === null || _f === void 0 ? void 0 : _f.joined) === null || _g === void 0 ? void 0 : _g.on('participantLeft', this.participantLeftListener);
    }
    render() {
        if (!this.showPrivateChat)
            return null;
        return (h(Host, null, h("div", { class: "chat-header", onClick: this.toggle }, h("div", { class: "chat-header-label" }, this.selectedUser ? (h("rtk-icon", { icon: this.iconPack.people, size: "sm" })) : (h("rtk-icon", { icon: this.iconPack.participants, size: "sm" })), !this.selectedUser ? this.t('chat.everyone') : this.selectedUser.name), h("div", { class: "chevron" }, h("rtk-icon", { icon: this.isOpen ? this.iconPack.chevron_up : this.iconPack.chevron_down, size: "sm" }))), h("div", { class: { dropdown: true, open: this.isOpen, scrollbar: true } }, h("div", { class: "public-chat-group-label", onClick: () => this.selectUser(undefined) }, h("div", { class: "public-chat-icon" }, h("rtk-icon", { icon: this.iconPack.participants, size: "sm" })), this.t('chat.everyone')), h("rtk-paginated-list", { class: "chat-selector-paginated-list", ref: (el) => (this.$paginatedListRef = el), pageSize: this.pageSize, pagesAllowed: 3, fetchData: this.getParticipants, createNodes: this.createPaticipantNodes, emptyListLabel: this.t('participants.empty_list') }, h("slot", null)))));
    }
    static get watchers() { return {
        "overrides": ["overridesChanged"],
        "meeting": ["meetingChanged"]
    }; }
    static get style() { return RtkChatSelectorStyle0; }
}, [1, "rtk-chat-selector", {
        "meeting": [16],
        "states": [16],
        "config": [16],
        "iconPack": [16],
        "t": [16],
        "overrides": [16],
        "size": [513],
        "isOpen": [32],
        "showPrivateChat": [32],
        "selectedUser": [32],
        "pageSize": [32],
        "pagesAllowed": [32],
        "unreadMap": [32],
        "close": [64]
    }, undefined, {
        "overrides": ["overridesChanged"],
        "meeting": ["meetingChanged"]
    }]);
__decorate([
    SyncWithStore()
], RtkChatSelector.prototype, "meeting", void 0);
__decorate([
    SyncWithStore()
], RtkChatSelector.prototype, "states", void 0);
__decorate([
    SyncWithStore()
], RtkChatSelector.prototype, "config", void 0);
__decorate([
    SyncWithStore()
], RtkChatSelector.prototype, "iconPack", void 0);
__decorate([
    SyncWithStore()
], RtkChatSelector.prototype, "t", void 0);
__decorate([
    SyncWithStore()
], RtkChatSelector.prototype, "overrides", void 0);
function defineCustomElement() {
    if (typeof customElements === "undefined") {
        return;
    }
    const components = ["rtk-chat-selector", "rtk-avatar", "rtk-button", "rtk-icon", "rtk-paginated-list", "rtk-spinner"];
    components.forEach(tagName => { switch (tagName) {
        case "rtk-chat-selector":
            if (!customElements.get(tagName)) {
                customElements.define(tagName, RtkChatSelector);
            }
            break;
        case "rtk-avatar":
            if (!customElements.get(tagName)) {
                defineCustomElement$5();
            }
            break;
        case "rtk-button":
            if (!customElements.get(tagName)) {
                defineCustomElement$4();
            }
            break;
        case "rtk-icon":
            if (!customElements.get(tagName)) {
                defineCustomElement$3();
            }
            break;
        case "rtk-paginated-list":
            if (!customElements.get(tagName)) {
                defineCustomElement$2();
            }
            break;
        case "rtk-spinner":
            if (!customElements.get(tagName)) {
                defineCustomElement$1();
            }
            break;
    } });
}
defineCustomElement();

export { RtkChatSelector as R, defineCustomElement as d };
