var __decorate = (this && this.__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;
};
import { MelodicComponent } from '@melodicdev/core';
import { paginationTemplate } from './pagination.template.js';
import { paginationStyles } from './pagination.styles.js';
/**
 * ml-pagination - Page navigation controls
 *
 * @example
 * ```html
 * <ml-pagination page="1" total-pages="10"></ml-pagination>
 * <ml-pagination page="5" total-pages="20" siblings="2"></ml-pagination>
 * ```
 *
 * @fires ml:page-change - Emitted when page changes, detail: { page }
 */
let PaginationComponent = class PaginationComponent {
    constructor() {
        /** Current active page (1-based) */
        this.page = 1;
        /** Total number of pages */
        this.totalPages = 1;
        /** Number of sibling pages to show around current */
        this.siblings = 1;
        this.goToPage = (page) => {
            const currentPage = Number(this.page);
            const total = Number(this.totalPages);
            if (page < 1 || page > total || page === currentPage)
                return;
            this.page = page;
            this.elementRef.dispatchEvent(new CustomEvent('ml:page-change', {
                bubbles: true,
                composed: true,
                detail: { page: this.page }
            }));
        };
        this.previous = () => {
            this.goToPage(Number(this.page) - 1);
        };
        this.next = () => {
            this.goToPage(Number(this.page) + 1);
        };
    }
    get pages() {
        const total = Math.max(1, Number(this.totalPages));
        const current = Math.min(Math.max(1, Number(this.page)), total);
        const siblings = Math.max(0, Number(this.siblings));
        const range = (start, end) => Array.from({ length: end - start + 1 }, (_, i) => ({ type: 'page', value: start + i }));
        const leftSibling = Math.max(current - siblings, 1);
        const rightSibling = Math.min(current + siblings, total);
        const showLeftEllipsis = leftSibling > 2;
        const showRightEllipsis = rightSibling < total - 1;
        if (!showLeftEllipsis && !showRightEllipsis) {
            return range(1, total);
        }
        if (!showLeftEllipsis && showRightEllipsis) {
            const leftRange = range(1, Math.max(rightSibling, 3 + siblings));
            return [...leftRange, { type: 'ellipsis' }, { type: 'page', value: total }];
        }
        if (showLeftEllipsis && !showRightEllipsis) {
            const rightRange = range(Math.min(leftSibling, total - 2 - siblings), total);
            return [{ type: 'page', value: 1 }, { type: 'ellipsis' }, ...rightRange];
        }
        return [
            { type: 'page', value: 1 },
            { type: 'ellipsis' },
            ...range(leftSibling, rightSibling),
            { type: 'ellipsis' },
            { type: 'page', value: total }
        ];
    }
    get hasPrevious() {
        return Number(this.page) > 1;
    }
    get hasNext() {
        return Number(this.page) < Number(this.totalPages);
    }
};
PaginationComponent = __decorate([
    MelodicComponent({
        selector: 'ml-pagination',
        template: paginationTemplate,
        styles: paginationStyles,
        attributes: ['page', 'total-pages', 'siblings']
    })
], PaginationComponent);
export { PaginationComponent };
