import { p as proxyCustomElement, H, e as createEvent, h, c as Host } from './p-ae373651.js';
import { e as defaultIconPack, i as useLanguage } from './p-5b3a7f00.js';
import { a as canViewParticipants } from './p-27d6399a.js';
import { S as SyncWithStore } from './p-070aa695.js';
import { d as defineCustomElement$4 } from './p-0031a3c0.js';
import { d as defineCustomElement$3 } from './p-3e0452c4.js';
import { d as defineCustomElement$2 } from './p-e6245534.js';

const rtkParticipantsToggleCss = ":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}.waiting-participants-count{position:absolute;right:var(--rtk-space-3, 12px);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;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}:host([variant='horizontal']) .waiting-participants-count{right:var(--rtk-space-4, 16px);top:50%;transform:translateY(-50%)}";
const RtkParticipantsToggleStyle0 = rtkParticipantsToggleCss;

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 RtkParticipantsToggle$1 = /*@__PURE__*/ proxyCustomElement(class RtkParticipantsToggle extends H {
    constructor() {
        super();
        this.__registerHost();
        this.__attachShadow();
        this.stateUpdate = createEvent(this, "rtkStateUpdate", 7);
        /** Variant */
        this.variant = 'button';
        /** Icon pack */
        this.iconPack = defaultIconPack;
        /** Language */
        this.t = useLanguage();
        this.participantsActive = false;
        this.waitlistedParticipants = [];
        this.stageRequestedParticipants = [];
        this.badgeCount = 0;
        this.canViewParticipants = false;
        this.updateStageRequests = async (stageRequests) => {
            var _a, _b, _c;
            if (!stageRequests) {
                stageRequests = (_c = (_b = (_a = this.meeting.stage) === null || _a === void 0 ? void 0 : _a.getAccessRequests()) === null || _b === void 0 ? void 0 : _b.stageRequests) !== null && _c !== void 0 ? _c : [];
            }
            this.stageRequestedParticipants = stageRequests;
            this.stageRequestedParticipants =
                this.meeting.stage.status === 'REQUESTED_TO_JOIN_STAGE'
                    ? [this.meeting.self, ...stageRequests]
                    : stageRequests;
            this.updateBadgeCount();
        };
        this.updateBadgeCount = () => {
            this.badgeCount = this.waitlistedParticipants.length + this.stageRequestedParticipants.length;
        };
        this.updateCanView = () => {
            this.canViewParticipants = canViewParticipants(this.meeting);
        };
    }
    connectedCallback() {
        this.meetingChanged(this.meeting);
        this.statesChanged(this.states);
    }
    disconnectedCallback() {
        var _a, _b, _c;
        if (!this.meeting)
            return;
        (_b = (_a = this.meeting) === null || _a === void 0 ? void 0 : _a.stage) === null || _b === void 0 ? void 0 : _b.removeListener('stageStatusUpdate', this.updateCanView);
        this.waitlistedParticipantJoinedListener &&
            this.meeting.participants.waitlisted.removeListener('participantJoined', this.waitlistedParticipantJoinedListener);
        this.waitlistedParticipantLeftListener &&
            this.meeting.participants.waitlisted.removeListener('participantLeft', this.waitlistedParticipantLeftListener);
        (_c = this.meeting.stage) === null || _c === void 0 ? void 0 : _c.removeListener('stageAccessRequestUpdate', this.updateStageRequests);
    }
    meetingChanged(meeting) {
        var _a;
        if (!meeting)
            return;
        this.canViewParticipants = canViewParticipants(meeting);
        (_a = meeting === null || meeting === void 0 ? void 0 : meeting.stage) === null || _a === void 0 ? void 0 : _a.on('stageStatusUpdate', this.updateCanView);
        if (meeting.self.permissions.acceptWaitingRequests) {
            this.waitlistedParticipants = meeting.participants.waitlisted.toArray();
            this.waitlistedParticipantJoinedListener = (participant) => {
                if (!this.waitlistedParticipants.some((p) => p.id === participant.id)) {
                    this.waitlistedParticipants = [...this.waitlistedParticipants, participant];
                    this.updateBadgeCount();
                }
            };
            this.waitlistedParticipantLeftListener = (participant) => {
                this.waitlistedParticipants = this.waitlistedParticipants.filter((p) => p.id !== participant.id);
                this.updateBadgeCount();
            };
            meeting.participants.waitlisted.addListener('participantJoined', this.waitlistedParticipantJoinedListener);
            meeting.participants.waitlisted.addListener('participantLeft', this.waitlistedParticipantLeftListener);
        }
        if (this.meeting.self.permissions.stageEnabled &&
            this.meeting.self.permissions.acceptStageRequests) {
            this.updateStageRequests();
            meeting === null || meeting === void 0 ? void 0 : meeting.stage.on('stageAccessRequestUpdate', this.updateStageRequests);
        }
        this.updateBadgeCount();
    }
    statesChanged(states) {
        if (states != null) {
            this.participantsActive = states.activeSidebar === true && states.sidebar === 'participants';
        }
    }
    toggleParticipantsTab() {
        const states = this.states;
        this.participantsActive = !((states === null || states === void 0 ? void 0 : states.activeSidebar) && (states === null || states === void 0 ? void 0 : states.sidebar) === 'participants');
        this.stateUpdate.emit({
            activeSidebar: this.participantsActive,
            sidebar: this.participantsActive ? 'participants' : undefined,
            activeMoreMenu: false,
        });
    }
    handleParticipantsActiveChange() {
        // Participants sidebar closed without opening a different sidebar
        if (!this.participantsActive && !this.states.activeSidebar) {
            this.buttonEl.focus();
        }
    }
    render() {
        if (!this.canViewParticipants)
            return h(Host, { "data-hidden": true });
        const text = this.t('participants');
        return (h(Host, { title: text }, this.badgeCount !== 0 && !this.participantsActive && (h("div", { class: "waiting-participants-count", part: "waiting-participants-count" }, h("span", null, this.badgeCount <= 100 ? this.badgeCount : '99+'))), h("rtk-controlbar-button", { ref: (el) => (this.buttonEl = el), part: "controlbar-button", size: this.size, iconPack: this.iconPack, class: { active: this.participantsActive }, onClick: () => this.toggleParticipantsTab(), icon: this.iconPack.participants, label: text, variant: this.variant })));
    }
    static get watchers() { return {
        "meeting": ["meetingChanged"],
        "states": ["statesChanged"],
        "participantsActive": ["handleParticipantsActiveChange"]
    }; }
    static get style() { return RtkParticipantsToggleStyle0; }
}, [1, "rtk-participants-toggle", {
        "variant": [513],
        "meeting": [16],
        "states": [16],
        "size": [513],
        "iconPack": [16],
        "t": [16],
        "participantsActive": [32],
        "waitlistedParticipants": [32],
        "stageRequestedParticipants": [32],
        "badgeCount": [32],
        "canViewParticipants": [32]
    }, undefined, {
        "meeting": ["meetingChanged"],
        "states": ["statesChanged"],
        "participantsActive": ["handleParticipantsActiveChange"]
    }]);
__decorate([
    SyncWithStore()
], RtkParticipantsToggle$1.prototype, "meeting", void 0);
__decorate([
    SyncWithStore()
], RtkParticipantsToggle$1.prototype, "states", void 0);
__decorate([
    SyncWithStore()
], RtkParticipantsToggle$1.prototype, "iconPack", void 0);
__decorate([
    SyncWithStore()
], RtkParticipantsToggle$1.prototype, "t", void 0);
function defineCustomElement$1() {
    if (typeof customElements === "undefined") {
        return;
    }
    const components = ["rtk-participants-toggle", "rtk-controlbar-button", "rtk-icon", "rtk-spinner"];
    components.forEach(tagName => { switch (tagName) {
        case "rtk-participants-toggle":
            if (!customElements.get(tagName)) {
                customElements.define(tagName, RtkParticipantsToggle$1);
            }
            break;
        case "rtk-controlbar-button":
            if (!customElements.get(tagName)) {
                defineCustomElement$4();
            }
            break;
        case "rtk-icon":
            if (!customElements.get(tagName)) {
                defineCustomElement$3();
            }
            break;
        case "rtk-spinner":
            if (!customElements.get(tagName)) {
                defineCustomElement$2();
            }
            break;
    } });
}
defineCustomElement$1();

const RtkParticipantsToggle = RtkParticipantsToggle$1;
const defineCustomElement = defineCustomElement$1;

export { RtkParticipantsToggle, defineCustomElement };
