'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const index$1 = require('./index-2e5758b6.js');
const uiStore = require('./ui-store-583d205a.js');
const index = require('./index-3f8e5441.js');
const debounce = require('./debounce-6080cc73.js');

const rtkGridPaginationCss = ":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;-webkit-user-select:none;-moz-user-select:none;user-select:none;align-items:center;font-size:16px}:host([data-hidden]){display:none}:host([size='sm']){font-size:12px}:host([size='sm']) .center{margin-left:var(--rtk-space-0, 0px);margin-right:var(--rtk-space-0, 0px)}:host([size='sm']) rtk-button{height:var(--rtk-space-7, 28px);width:var(--rtk-space-7, 28px)}:host([size='sm']) rtk-button rtk-icon{height:var(--rtk-space-4, 16px);width:var(--rtk-space-4, 16px)}rtk-button{--tw-shadow:0 0 #0000;--tw-shadow-colored:0 0 #0000;box-shadow:var(--tw-ring-offset-shadow, 0 0 #0000), var(--tw-ring-shadow, 0 0 #0000), var(--tw-shadow)}.center{margin-left:var(--rtk-space-2, 8px);margin-right:var(--rtk-space-2, 8px);color:rgb(var(--rtk-colors-text-700, 255 255 255 / 0.64))}.center .page{color:rgb(var(--rtk-colors-text-900, 255 255 255 / 0.88))}.center .slash{margin-left:var(--rtk-space-0\\.5, 2px);margin-right:var(--rtk-space-0\\.5, 2px)}.center .pages{align-self:flex-end;font-size:12px}:host([variant='rounded']){overflow:hidden;border-radius:9999px;--tw-bg-opacity:1;background-color:rgba(var(--rtk-colors-background-800, 30 30 30) / var(--tw-bg-opacity))}:host([variant='rounded']) rtk-button{border-radius:9999px;border-width:var(--rtk-border-width-none, 0);border-style:none;background-color:transparent;color:rgb(var(--rtk-colors-text-700, 255 255 255 / 0.64));transition-property:color, background-color, border-color, text-decoration-color, fill, stroke;transition-timing-function:cubic-bezier(0.4, 0, 0.2, 1);transition-duration:150ms}:host([variant='rounded']) rtk-button:not([disabled]):hover{--tw-bg-opacity:1;background-color:rgba(var(--rtk-colors-background-600, 60 60 60) / var(--tw-bg-opacity))}:host([variant='rounded']) rtk-button:not([disabled]):focus{--tw-bg-opacity:1;background-color:rgba(var(--rtk-colors-background-600, 60 60 60) / var(--tw-bg-opacity))}:host([variant='grid']) rtk-button{position:absolute;top:50%;height:var(--rtk-space-20, 80px);width:var(--rtk-space-20, 80px);opacity:0.2;transition-property:color, background-color, border-color, text-decoration-color, fill, stroke, opacity, box-shadow, transform, filter, backdrop-filter;transition-timing-function:cubic-bezier(0.4, 0, 0.2, 1);transition-duration:150ms}:host([variant='grid']) rtk-button:hover{opacity:1}:host([variant='grid']) rtk-button{transform:translateY(-50%)}:host([variant='grid']) rtk-button.prev{left:var(--rtk-space-0, 0px);border-top-right-radius:9999px;border-bottom-right-radius:9999px}:host([variant='grid']) rtk-button.next{right:var(--rtk-space-0, 0px);border-top-left-radius:9999px;border-bottom-left-radius:9999px}:host([variant='grid']) rtk-button.auto{left:var(--rtk-space-0, 0px);right:var(--rtk-space-0, 0px);border-top-right-radius:9999px;border-bottom-right-radius:9999px}:host([variant='grid']) .dots{position:absolute;left:50%;bottom:var(--rtk-space-3, 12px);display:flex;align-items:center;transform:translateX(-50%)}:host([variant='grid']) .dots .dot{margin-right:var(--rtk-space-2, 8px);height:var(--rtk-space-2, 8px);width:var(--rtk-space-2, 8px);cursor:pointer;border-radius:9999px;--tw-bg-opacity:1;background-color:rgba(var(--rtk-colors-background-700, 44 44 44) / var(--tw-bg-opacity))}:host([variant='grid']) .dots .dot:last-child{margin-right:var(--rtk-space-0, 0px)}:host([variant='grid']) .dots .dot.active{--tw-bg-opacity:1;background-color:rgba(var(--rtk-colors-brand-500, 33 96 253) / var(--tw-bg-opacity))}:host([variant='grid'][size='sm']) rtk-button{height:var(--rtk-space-14, 56px);width:var(--rtk-space-14, 56px);opacity:0.1}";
const RtkGridPaginationStyle0 = rtkGridPaginationCss;

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 MASS_ACTIONS_DEBOUNCE_TIMER = 50; // In ms
const RtkGridPagination = class {
    constructor(hostRef) {
        index$1.registerInstance(this, hostRef);
        /** Variant */
        this.variant = 'rounded';
        /** Icon Pack */
        this.iconPack = uiStore.defaultIconPack;
        /** Language */
        this.t = uiStore.useLanguage();
        this.page = 1;
        this.pageCount = 0;
        this.activeCount = 0;
        this.activeComputedCount = 0;
        this.showPagination = false;
        this.onPageChanged = ({ currentPage, pageCount, }) => {
            this.pageCount = pageCount;
            this.page = currentPage;
        };
        this.toggleGridPagination = debounce.debounce_1(() => {
            var _a;
            const { self, participants } = this.meeting;
            const { mobile, desktop } = (_a = self.config) === null || _a === void 0 ? void 0 : _a.maxVideoStreams;
            const maxCount = this.size === 'sm' ? mobile : desktop;
            const stagePeopleCount = participants.joined
                .toArray()
                .filter((p) => p.stageStatus === 'ON_STAGE').length;
            if (stagePeopleCount < maxCount) {
                this.showPagination = false;
            }
            else {
                this.showPagination = true;
            }
        }, MASS_ACTIONS_DEBOUNCE_TIMER);
        this.onParticipantJoin = debounce.debounce_1(() => {
            this.pageCount = this.meeting.participants.pageCount;
            this.toggleGridPagination();
        }, MASS_ACTIONS_DEBOUNCE_TIMER);
        this.onParticipantLeave = debounce.debounce_1(() => {
            this.pageCount = this.meeting.participants.pageCount;
            this.toggleGridPagination();
        }, MASS_ACTIONS_DEBOUNCE_TIMER);
        this.onStateStatusUpdate = debounce.debounce_1(() => {
            this.pageCount = this.meeting.participants.pageCount;
            this.toggleGridPagination();
        }, MASS_ACTIONS_DEBOUNCE_TIMER);
        this.prevPage = () => {
            if (!this.meeting)
                return;
            const { participants } = this.meeting;
            if (this.page > 1) {
                participants.setPage((this.page -= 1));
            }
            else if (participants.viewMode === 'PAGINATED') {
                participants.setViewMode('ACTIVE_GRID');
            }
        };
        this.nextPage = () => {
            if (!this.meeting)
                return;
            const { participants } = this.meeting;
            if (this.page > 0 && this.page < this.pageCount) {
                participants.setPage((this.page += 1));
            }
            else if (participants.count > 0 && this.pageCount === 0) {
                participants.setViewMode('PAGINATED');
            }
        };
    }
    connectedCallback() {
        this.meetingChanged(this.meeting);
        this.sizeChanged();
    }
    disconnectedCallback() {
        if (!this.meeting)
            return;
        const { participants, stage } = this.meeting;
        participants.removeListener('pageChanged', this.onPageChanged);
        participants.removeListener('viewModeChanged', this.onPageChanged);
        participants.joined.removeListener('participantJoined', this.onParticipantJoin);
        participants.joined.removeListener('participantLeft', this.onParticipantLeave);
        participants.joined.removeListener('stageStatusUpdate', this.onStateStatusUpdate);
        stage.removeListener('stageStatusUpdate', this.onStateStatusUpdate);
    }
    meetingChanged(meeting) {
        if (meeting != null) {
            const { stage, participants } = meeting;
            this.page = participants.currentPage;
            this.pageCount = participants.pageCount;
            participants.addListener('viewModeChanged', this.onPageChanged);
            participants.addListener('pageChanged', this.onPageChanged);
            participants.joined.addListener('participantJoined', this.onParticipantJoin);
            participants.joined.addListener('participantLeft', this.onParticipantLeave);
            participants.joined.addListener('stageStatusUpdate', this.onStateStatusUpdate);
            stage.addListener('stageStatusUpdate', this.onStateStatusUpdate);
            this.activeCount = this.meeting.participants.joined.size;
            this.activeComputedCount = this.meeting.participants.joined.size;
            this.toggleGridPagination();
        }
    }
    sizeChanged() {
        this.toggleGridPagination();
    }
    render() {
        if (!this.meeting)
            return null;
        const { meta } = this.meeting;
        const isAudioRoom = (meta === null || meta === void 0 ? void 0 : meta.viewType) === 'AUDIO_ROOM';
        if (isAudioRoom || !this.showPagination) {
            return index$1.h(index$1.Host, { "data-hidden": true });
        }
        return (index$1.h(index$1.Host, null, index$1.h("rtk-button", { class: "prev", variant: "secondary", kind: "icon", disabled: this.pageCount === 0, onClick: this.prevPage, "aria-label": this.t('page.prev') }, index$1.h("rtk-icon", { icon: this.iconPack.chevron_left })), this.variant !== 'grid' && (index$1.h("div", { class: "center" }, index$1.h("span", { class: "page" }, this.pageCount === 0 ? (index$1.h("rtk-tooltip", { label: this.t('layout.auto') }, index$1.h("rtk-button", { kind: "icon", class: "auto" }, index$1.h("rtk-icon", { icon: this.iconPack.wand })))) : (this.page)), this.pageCount !== 0 && [
            index$1.h("span", { class: "slash" }, "/"),
            index$1.h("span", { class: "pages" }, this.pageCount),
        ])), this.variant === 'grid' && this.pageCount > 0 && (index$1.h("div", { class: "dots" }, [...Array(this.pageCount)].map((_, index) => (index$1.h("div", { key: `dot-${index}`, class: { dot: true, active: index + 1 === this.page } }))))), index$1.h("rtk-button", { class: "next", variant: "secondary", kind: "icon", disabled: this.page !== 0 && this.page === this.pageCount, onClick: this.nextPage, "aria-label": this.t('page.next') }, index$1.h("rtk-icon", { icon: this.iconPack.chevron_right, tabIndex: -1, "aria-hidden": true }))));
    }
    static get watchers() { return {
        "meeting": ["meetingChanged"],
        "size": ["sizeChanged"]
    }; }
};
__decorate([
    index.SyncWithStore()
], RtkGridPagination.prototype, "meeting", void 0);
__decorate([
    index.SyncWithStore()
], RtkGridPagination.prototype, "states", void 0);
__decorate([
    index.SyncWithStore()
], RtkGridPagination.prototype, "iconPack", void 0);
__decorate([
    index.SyncWithStore()
], RtkGridPagination.prototype, "t", void 0);
RtkGridPagination.style = RtkGridPaginationStyle0;

exports.rtk_grid_pagination = RtkGridPagination;
