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 { d as canViewPolls } 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 rtkPollsToggleCss = ":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-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;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}:host([variant='horizontal']) .unread-count{right:var(--rtk-space-4, 16px);top:50%;transform:translateY(-50%)}";
const RtkPollsToggleStyle0 = rtkPollsToggleCss;

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 RtkPollsToggle$1 = /*@__PURE__*/ proxyCustomElement(class RtkPollsToggle 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.pollsActive = false;
        this.unreadPollsCount = 0;
        this.canViewPolls = false;
        this.onPollsUpdate = ({ newPoll }) => {
            if (newPoll === true)
                this.unreadPollsCount += 1;
        };
        this.updateCanView = () => {
            this.canViewPolls = canViewPolls(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.polls) === null || _b === void 0 ? void 0 : _b.removeListener('pollsUpdate', this.onPollsUpdate);
        (_d = (_c = this.meeting) === null || _c === void 0 ? void 0 : _c.self) === null || _d === void 0 ? void 0 : _d.permissions.removeListener('pollsUpdate', this.updateCanView);
        (_f = (_e = this.meeting) === null || _e === void 0 ? void 0 : _e.stage) === null || _f === void 0 ? void 0 : _f.removeListener('stageStatusUpdate', this.updateCanView);
    }
    meetingChanged(meeting) {
        var _a, _b;
        if (meeting && meeting.polls) {
            this.unreadPollsCount = meeting.polls.items.length;
            this.meeting.polls.addListener('pollsUpdate', this.onPollsUpdate);
            (_a = meeting === null || meeting === void 0 ? void 0 : meeting.self) === null || _a === void 0 ? void 0 : _a.permissions.addListener('pollsUpdate', this.updateCanView);
            this.canViewPolls = canViewPolls(meeting);
            (_b = meeting === null || meeting === void 0 ? void 0 : meeting.stage) === null || _b === void 0 ? void 0 : _b.on('stageStatusUpdate', this.updateCanView);
        }
    }
    statesChanged(states) {
        if (states != null) {
            this.pollsActive = states.activeSidebar === true && states.sidebar === 'polls';
        }
    }
    togglePollsTab() {
        const states = this.states;
        this.unreadPollsCount = 0;
        this.pollsActive = !((states === null || states === void 0 ? void 0 : states.activeSidebar) && (states === null || states === void 0 ? void 0 : states.sidebar) === 'polls');
        this.stateUpdate.emit({
            activeSidebar: this.pollsActive,
            sidebar: this.pollsActive ? 'polls' : undefined,
            activeMoreMenu: false,
        });
    }
    handlePollsActiveChange() {
        // Polls sidebar closed without opening a different sidebar
        if (!this.pollsActive && !this.states.activeSidebar) {
            this.buttonEl.focus();
        }
    }
    render() {
        if (!this.canViewPolls)
            return h(Host, { "data-hidden": true });
        const text = this.t('polls');
        // TODO(callmetarush): Just showing polls for all V2 users irrespective of themes
        // untill we get ui theme for V2.
        return (h(Host, { title: text }, this.unreadPollsCount !== 0 && !this.pollsActive && (h("div", { class: "unread-count", part: "unread-count" }, h("span", null, this.unreadPollsCount <= 100 ? this.unreadPollsCount : '99+'))), h("rtk-controlbar-button", { ref: (el) => (this.buttonEl = el), part: "controlbar-button", size: this.size, iconPack: this.iconPack, class: { active: this.pollsActive }, onClick: () => this.togglePollsTab(), icon: this.iconPack.poll, label: text, variant: this.variant })));
    }
    static get watchers() { return {
        "meeting": ["meetingChanged"],
        "states": ["statesChanged"],
        "pollsActive": ["handlePollsActiveChange"]
    }; }
    static get style() { return RtkPollsToggleStyle0; }
}, [1, "rtk-polls-toggle", {
        "variant": [513],
        "meeting": [16],
        "states": [16],
        "size": [513],
        "iconPack": [16],
        "t": [16],
        "pollsActive": [32],
        "unreadPollsCount": [32],
        "canViewPolls": [32]
    }, undefined, {
        "meeting": ["meetingChanged"],
        "states": ["statesChanged"],
        "pollsActive": ["handlePollsActiveChange"]
    }]);
__decorate([
    SyncWithStore()
], RtkPollsToggle$1.prototype, "meeting", void 0);
__decorate([
    SyncWithStore()
], RtkPollsToggle$1.prototype, "states", void 0);
__decorate([
    SyncWithStore()
], RtkPollsToggle$1.prototype, "iconPack", void 0);
__decorate([
    SyncWithStore()
], RtkPollsToggle$1.prototype, "t", void 0);
function defineCustomElement$1() {
    if (typeof customElements === "undefined") {
        return;
    }
    const components = ["rtk-polls-toggle", "rtk-controlbar-button", "rtk-icon", "rtk-spinner"];
    components.forEach(tagName => { switch (tagName) {
        case "rtk-polls-toggle":
            if (!customElements.get(tagName)) {
                customElements.define(tagName, RtkPollsToggle$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 RtkPollsToggle = RtkPollsToggle$1;
const defineCustomElement = defineCustomElement$1;

export { RtkPollsToggle, defineCustomElement };
