import { p as proxyCustomElement, H, h } from './p-ae373651.js';
import { c as createDefaultConfig, e as defaultIconPack, i as useLanguage } from './p-5b3a7f00.js';
import './p-a83ccdbd.js';
import { S as SyncWithStore } from './p-070aa695.js';
import { d as defineCustomElement$8 } from './p-a3bf84d7.js';
import { d as defineCustomElement$7 } from './p-85587a5c.js';
import { d as defineCustomElement$6 } from './p-3e0452c4.js';
import { d as defineCustomElement$5 } from './p-0304f83a.js';
import { d as defineCustomElement$4 } from './p-02d63986.js';
import { d as defineCustomElement$3 } from './p-0c8d2918.js';
import { d as defineCustomElement$2 } from './p-e6245534.js';
import { d as defineCustomElement$1 } from './p-d48b4170.js';

const rtkParticipantsViewerListCss = ":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:flex;height:100%;width:100%;flex-direction:column;font-size:14px}.list{margin-bottom:var(--rtk-space-4, 16px);display:flex;height:100%;flex-direction:column}h3,.heading-count{margin:var(--rtk-space-0, 0px);align-items:center;justify-content:center;padding:var(--rtk-space-0, 0px);font-size:16px;font-weight:400;color:rgb(var(--rtk-colors-text-900, 255 255 255 / 0.88));text-align:center}.heading-count{font-size:14px}.participants{margin-top:var(--rtk-space-2, 8px);margin-bottom:var(--rtk-space-0, 0px);padding:var(--rtk-space-0, 0px)}.empty-viewers-list{margin-top:var(--rtk-space-2, 8px);margin-bottom:var(--rtk-space-2, 8px);text-align:center;font-size:12px;color:rgb(var(--rtk-colors-text-700, 255 255 255 / 0.64))}";
const RtkParticipantsViewerListStyle0 = rtkParticipantsViewerListCss;

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 RtkParticipantsViewers = /*@__PURE__*/ proxyCustomElement(class RtkParticipantsViewers extends H {
    constructor() {
        super();
        this.__registerHost();
        this.__attachShadow();
        this.updateStageViewers = () => {
            this.getViewers(this.search);
        };
        /** Config */
        this.config = createDefaultConfig();
        /** Hide Viewer Count Header */
        this.hideHeader = false;
        /** Icon pack */
        this.iconPack = defaultIconPack;
        /** View mode for participants list */
        this.view = 'sidebar';
        /** Search */
        this.search = '';
        /** Language */
        this.t = useLanguage();
        this.stageViewers = [];
        this.createParticipantNode = (participant) => {
            return (h("rtk-participant", { role: "listitem", key: participant.id, meeting: this.meeting, participant: participant, view: this.view, iconPack: this.iconPack, config: this.config, t: this.t }));
        };
        // TODO: (ishita1805) Remove viewtype check when we start supporting viewers in livestream.
        this.shouldShowViewers = () => {
            var _a, _b, _c;
            return (_c = (_b = (_a = this.meeting) === null || _a === void 0 ? void 0 : _a.self) === null || _b === void 0 ? void 0 : _b.permissions) === null || _c === void 0 ? void 0 : _c.stageEnabled;
        };
    }
    connectedCallback() {
        this.meetingChanged(this.meeting);
        this.searchChanged(this.search);
    }
    meetingChanged(meeting) {
        if (!meeting)
            return;
        this.participantJoinedListener = (participant) => {
            if (participant.stageStatus === 'ON_STAGE')
                return;
            // Do not append if participant name or id does not match search query
            const lowerCaseSearch = this.search.toLowerCase();
            if (lowerCaseSearch.length > 0 && !participant.name.toLowerCase().includes(lowerCaseSearch))
                return;
            this.stageViewers = [
                ...this.stageViewers.filter((p) => p.id !== participant.id),
                participant,
            ];
        };
        this.participantLeftListener = (participant) => {
            this.stageViewers = this.stageViewers.filter((p) => p.id !== participant.id);
        };
        meeting.participants.joined.addListener('participantJoined', this.participantJoinedListener);
        meeting.participants.joined.addListener('participantLeft', this.participantLeftListener);
        meeting.participants.joined.on('stageStatusUpdate', this.updateStageViewers);
        meeting.stage.on('stageStatusUpdate', this.updateStageViewers);
    }
    searchChanged(search) {
        this.getViewers(search);
    }
    disconnectedCallback() {
        const { participants, stage } = this.meeting;
        this.participantJoinedListener &&
            this.meeting.participants.joined.removeListener('participantJoined', this.participantJoinedListener);
        this.participantLeftListener &&
            this.meeting.participants.joined.removeListener('participantLeft', this.participantLeftListener);
        participants.joined.removeListener('stageStatusUpdate', this.updateStageViewers);
        stage.removeListener('stageStatusUpdate', this.updateStageViewers);
    }
    getViewers(search) {
        let list = this.meeting.stage.status === 'ON_STAGE' ? [] : [this.meeting.self];
        list = [...list, ...this.meeting.participants.joined.toArray()].filter((p) => p.stageStatus !== 'ON_STAGE');
        if (search === '') {
            this.stageViewers = list;
        }
        else {
            this.stageViewers = list.filter((p) => { var _a; return ((_a = p.name) !== null && _a !== void 0 ? _a : p.id).toLowerCase().includes(search.toLowerCase()); });
        }
    }
    render() {
        if (!this.meeting)
            return null;
        if (this.view !== 'sidebar' || !this.shouldShowViewers())
            return;
        return (h("div", { class: "list" }, !this.hideHeader && (h("div", { class: "heading-count", part: "heading-count" }, this.t('viewers'), " (", this.stageViewers.length, ")")), h("rtk-virtualized-participant-list", { items: this.stageViewers, renderItem: this.createParticipantNode, class: "participants", part: "participants", style: { height: '100%' }, emptyListElement: h("div", { class: "empty-viewers-list" }, this.search.length > 0
                ? this.t('participants.errors.empty_results')
                : this.t('participants.empty_list')) })));
    }
    static get watchers() { return {
        "meeting": ["meetingChanged"],
        "search": ["searchChanged"]
    }; }
    static get style() { return RtkParticipantsViewerListStyle0; }
}, [1, "rtk-participants-viewer-list", {
        "meeting": [16],
        "config": [16],
        "size": [513],
        "hideHeader": [4, "hide-header"],
        "iconPack": [16],
        "view": [1],
        "search": [1],
        "t": [16],
        "stageViewers": [32]
    }, undefined, {
        "meeting": ["meetingChanged"],
        "search": ["searchChanged"]
    }]);
__decorate([
    SyncWithStore()
], RtkParticipantsViewers.prototype, "meeting", void 0);
__decorate([
    SyncWithStore()
], RtkParticipantsViewers.prototype, "config", void 0);
__decorate([
    SyncWithStore()
], RtkParticipantsViewers.prototype, "iconPack", void 0);
__decorate([
    SyncWithStore()
], RtkParticipantsViewers.prototype, "t", void 0);
function defineCustomElement() {
    if (typeof customElements === "undefined") {
        return;
    }
    const components = ["rtk-participants-viewer-list", "rtk-avatar", "rtk-button", "rtk-icon", "rtk-menu-item", "rtk-menu-list", "rtk-participant", "rtk-spinner", "rtk-virtualized-participant-list"];
    components.forEach(tagName => { switch (tagName) {
        case "rtk-participants-viewer-list":
            if (!customElements.get(tagName)) {
                customElements.define(tagName, RtkParticipantsViewers);
            }
            break;
        case "rtk-avatar":
            if (!customElements.get(tagName)) {
                defineCustomElement$8();
            }
            break;
        case "rtk-button":
            if (!customElements.get(tagName)) {
                defineCustomElement$7();
            }
            break;
        case "rtk-icon":
            if (!customElements.get(tagName)) {
                defineCustomElement$6();
            }
            break;
        case "rtk-menu-item":
            if (!customElements.get(tagName)) {
                defineCustomElement$5();
            }
            break;
        case "rtk-menu-list":
            if (!customElements.get(tagName)) {
                defineCustomElement$4();
            }
            break;
        case "rtk-participant":
            if (!customElements.get(tagName)) {
                defineCustomElement$3();
            }
            break;
        case "rtk-spinner":
            if (!customElements.get(tagName)) {
                defineCustomElement$2();
            }
            break;
        case "rtk-virtualized-participant-list":
            if (!customElements.get(tagName)) {
                defineCustomElement$1();
            }
            break;
    } });
}
defineCustomElement();

export { RtkParticipantsViewers as R, defineCustomElement as d };
