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 { e as defaultIconPack, i as useLanguage } from './p-5b3a7f00.js';
import { e as elapsedDuration } from './p-382270d8.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 rtkPinnedMessageSelectorCss = ":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{position:relative;margin-left:var(--rtk-space-2, 8px);margin-right:var(--rtk-space-2, 8px);cursor:pointer;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));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}";
const RtkPinnedMessageSelectorStyle0 = rtkPinnedMessageSelectorCss;

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 RtkPinnedMessageSelector = /*@__PURE__*/ proxyCustomElement(class RtkPinnedMessageSelector extends H {
    constructor() {
        super();
        this.__registerHost();
        this.__attachShadow();
        this.dropdownToggle = createEvent(this, "rtkDropdownToggle", 7);
        this.pinnedMessageSelect = createEvent(this, "rtkPinnedMessageSelect", 7);
        this.isOpen = false;
        /** Icon pack */
        this.iconPack = defaultIconPack;
        /** Language */
        this.t = useLanguage();
        //NOTE(ikabra): Fix this value once backend supports pagination for participants
        this.pageSize = 25;
        this.pagesAllowed = 3;
        this.showPinnedMessages = true;
        this.toggle = (e) => {
            e.preventDefault();
            e.stopPropagation();
            this.isOpen = !this.isOpen;
            this.dropdownToggle.emit({ open: this.isOpen });
        };
        this.getPinnedChatMessages = async (timestamp, size, reversed) => {
            const localMeeting = this.meeting;
            if (!localMeeting)
                return [];
            try {
                const messages = await localMeeting.chat.fetchPinnedMessages({
                    timestamp,
                    limit: size,
                    direction: reversed ? 'before' : 'after',
                });
                return messages;
            }
            catch (err) {
                return [];
            }
        };
        this.createPinnedChatNodes = (messages) => {
            return messages.map((message) => {
                const preview = message.type === 'text'
                    ? (message.message || '').replace(/\s+/g, ' ').trim()
                    : message.type === 'file'
                        ? 'File Attachment'
                        : message.type === 'image'
                            ? 'Image Attachment'
                            : '';
                return (h("div", { class: "pinned-message", id: message.id, onClick: () => {
                        this.pinnedMessageSelect.emit(message);
                        this.close();
                    } }, h("div", { class: "pinned-message-container" }, h("rtk-avatar", { size: "sm", participant: {
                        name: message.displayName,
                        picture: '',
                    } }), h("div", { class: "pinned-message-content" }, h("div", { class: "pinned-message-preview", title: preview }, preview), h("div", { class: "pinned-message-meta" }, h("span", { class: "pinned-message-time" }, elapsedDuration(message.time, new Date(Date.now()))))))));
            });
        };
        this.pinChatListener = (data) => {
            var _a, _b;
            if (((_a = data.message.targetUserIds) === null || _a === void 0 ? void 0 : _a.length) > 0) {
                // pinned messages are not supported for private chat
                return;
            }
            /**
             * FIXME(ikabra): This is a temporary hack to refresh the list when a new message is pinned.
             * This is done to avoid duplicate messages in the list.
             * Fix is being implemented by socket where they are updating the `updatedAt` timestamp when the message is pinned.
             */
            (_b = this.$paginatedListRef) === null || _b === void 0 ? void 0 : _b.reset();
        };
        this.unpinChatListener = async (data) => {
            var _a, _b;
            if (((_a = data.message.targetUserIds) === null || _a === void 0 ? void 0 : _a.length) > 0) {
                // pinned messages are not supported for private chat
                return;
            }
            (_b = this.$paginatedListRef) === null || _b === void 0 ? void 0 : _b.onNodeDelete(data.message.id);
        };
        this.chatUpdateListener = async (data) => {
            if (data.action !== 'delete')
                return;
            this.unpinChatListener(data);
        };
    }
    connectedCallback() {
        this.meetingChanged(this.meeting);
    }
    disconnectedCallback() {
        this.disconnectMeeting(this.meeting);
    }
    disconnectMeeting(meeting) {
        var _a, _b, _c;
        (_a = meeting.chat) === null || _a === void 0 ? void 0 : _a.removeListener('pinMessage', this.pinChatListener);
        (_b = meeting.chat) === null || _b === void 0 ? void 0 : _b.removeListener('unpinMessage', this.unpinChatListener);
        (_c = meeting.chat) === null || _c === void 0 ? void 0 : _c.removeListener('chatUpdate', this.chatUpdateListener);
    }
    meetingChanged(meeting, oldMeeting) {
        var _a, _b, _c, _d;
        if (oldMeeting)
            this.disconnectMeeting(oldMeeting);
        if (!meeting)
            return;
        (_a = this.$paginatedListRef) === null || _a === void 0 ? void 0 : _a.reset();
        (_b = meeting.chat) === null || _b === void 0 ? void 0 : _b.addListener('pinMessage', this.pinChatListener);
        (_c = meeting.chat) === null || _c === void 0 ? void 0 : _c.addListener('unpinMessage', this.unpinChatListener);
        (_d = meeting.chat) === null || _d === void 0 ? void 0 : _d.addListener('chatUpdate', this.chatUpdateListener);
    }
    /** */
    async close() {
        if (!this.isOpen)
            return;
        this.isOpen = false;
        this.dropdownToggle.emit({ open: false });
    }
    async onChatSelectorChange(event) {
        var _a;
        const selectedUser = (_a = event.detail) === null || _a === void 0 ? void 0 : _a.selectedUser;
        // Everyone
        if (!selectedUser) {
            this.showPinnedMessages = true;
        }
        else {
            this.showPinnedMessages = false;
        }
    }
    render() {
        if (!this.showPinnedMessages)
            return null;
        return (h(Host, null, h("div", { class: "chat-header", onClick: this.toggle }, h("div", { class: "chat-header-label" }, h("rtk-icon", { icon: this.iconPack.pin, size: "sm" }), this.t('chat.pinned_msgs')), 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("rtk-paginated-list", { class: "pinned-message-selector-paginated-list", ref: (el) => (this.$paginatedListRef = el), pageSize: this.pageSize, pagesAllowed: 3, fetchData: this.getPinnedChatMessages, createNodes: this.createPinnedChatNodes, emptyListLabel: this.t('chat.empty_search') }, h("slot", null)))));
    }
    static get watchers() { return {
        "meeting": ["meetingChanged"]
    }; }
    static get style() { return RtkPinnedMessageSelectorStyle0; }
}, [1, "rtk-pinned-message-selector", {
        "meeting": [16],
        "iconPack": [16],
        "t": [16],
        "isOpen": [32],
        "pageSize": [32],
        "pagesAllowed": [32],
        "showPinnedMessages": [32],
        "close": [64]
    }, [[8, "rtkChatSelectorChange", "onChatSelectorChange"]], {
        "meeting": ["meetingChanged"]
    }]);
__decorate([
    SyncWithStore()
], RtkPinnedMessageSelector.prototype, "meeting", void 0);
__decorate([
    SyncWithStore()
], RtkPinnedMessageSelector.prototype, "iconPack", void 0);
__decorate([
    SyncWithStore()
], RtkPinnedMessageSelector.prototype, "t", void 0);
function defineCustomElement() {
    if (typeof customElements === "undefined") {
        return;
    }
    const components = ["rtk-pinned-message-selector", "rtk-avatar", "rtk-button", "rtk-icon", "rtk-paginated-list", "rtk-spinner"];
    components.forEach(tagName => { switch (tagName) {
        case "rtk-pinned-message-selector":
            if (!customElements.get(tagName)) {
                customElements.define(tagName, RtkPinnedMessageSelector);
            }
            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 { RtkPinnedMessageSelector as R, defineCustomElement as d };
