import { Host, h } from "@stencil/core";
export class CpslPagination {
    constructor() {
        this.handlePrevClick = () => {
            if (this.currentPage > 0) {
                this.currentPage--;
            }
        };
        this.handleNextClick = () => {
            if (this.currentPage < this.totalPages - 1) {
                this.currentPage++;
            }
        };
        this.handlePageClick = (value) => () => {
            if (value >= 0 && value <= this.totalPages - 1) {
                this.currentPage = value;
            }
        };
        this.currentPage = 0;
        this.initialPage = undefined;
        this.totalPages = undefined;
        this.visiblePages = 5;
    }
    watchChange() {
        this.cpslPaginationChanged.emit(this.currentPage);
    }
    componentWillLoad() {
        var _a;
        this.currentPage = (_a = this.initialPage) !== null && _a !== void 0 ? _a : 0;
        if (this.visiblePages < 5) {
            this.visiblePages = 5;
        }
    }
    render() {
        const filteredPages = this.totalPages <= this.visiblePages ? [...Array(this.totalPages).keys()] : [];
        if (this.totalPages > this.visiblePages) {
            filteredPages.push(0);
            const isFirstSelected = this.currentPage === 0;
            const isLastSelected = this.currentPage === this.totalPages - 1;
            const isFirstOrLastSelected = isFirstSelected || isLastSelected;
            let remainingVisible = this.visiblePages - (isFirstOrLastSelected ? 2 : 3);
            const halfRemaining = Math.round(remainingVisible / 2);
            const numberBefore = Math.max(this.currentPage - 1, 0);
            const numberAfter = Math.max(this.totalPages - 2 - this.currentPage, 0);
            let beforeHalf = halfRemaining;
            let afterHalf = remainingVisible - halfRemaining;
            if (numberBefore >= beforeHalf) {
                if (numberAfter < afterHalf) {
                    beforeHalf += afterHalf - numberAfter;
                    afterHalf = numberAfter;
                }
            }
            else {
                afterHalf += beforeHalf - numberBefore;
                beforeHalf = numberBefore;
            }
            while (beforeHalf > 0) {
                filteredPages.push(this.currentPage - beforeHalf);
                beforeHalf--;
            }
            if (!isFirstSelected) {
                filteredPages.push(this.currentPage);
            }
            const pagesAfter = [];
            while (afterHalf > 0) {
                pagesAfter.push(this.currentPage + afterHalf);
                afterHalf--;
            }
            filteredPages.push(...pagesAfter.reverse());
            if (!isLastSelected) {
                filteredPages.push(this.totalPages - 1);
            }
        }
        return (h(Host, { key: '030d0a78198c77a243ae9996dd8192437c3c370b' }, h("cpsl-button-group", { key: 'b12f592a5cf2cdab91c82516206e4a831bdad5d6', selectedId: `${this.currentPage}` }, h("cpsl-button", { key: 'a24be555c3be19f63a5011aacac7046dbd8bdecd', class: "arrow-button", onClick: this.handlePrevClick }, h("cpsl-icon", { key: 'e82fe20b2091ede6ac99c16dfb433b47d7490293', class: { 'icon': true, 'start-icon': true }, icon: "arrowNarrow" })), filteredPages.map(page => (h("cpsl-button", { key: page, variant: "secondary", fullWidth: true, id: `${page}`, onClick: this.handlePageClick(page) }, h("cpsl-text", { variant: "bodyS" }, page + 1)))), h("cpsl-button", { key: '3d4c5c6b211a1543818746da22739df63a4afcee', class: "arrow-button", onClick: this.handleNextClick }, h("cpsl-icon", { key: 'f623fb7dc739556480bc90d1ce296baa8f5afcb0', class: "icon", icon: "arrowNarrow" })))));
    }
    static get is() { return "cpsl-pagination"; }
    static get encapsulation() { return "shadow"; }
    static get originalStyleUrls() {
        return {
            "$": ["cpsl-pagination.scss"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["cpsl-pagination.css"]
        };
    }
    static get properties() {
        return {
            "initialPage": {
                "type": "number",
                "mutable": false,
                "complexType": {
                    "original": "number",
                    "resolved": "number",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "The initial page to select.\nDefault is 0."
                },
                "attribute": "initial-page",
                "reflect": false
            },
            "totalPages": {
                "type": "number",
                "mutable": false,
                "complexType": {
                    "original": "number",
                    "resolved": "number",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "The total number of pages."
                },
                "attribute": "total-pages",
                "reflect": false
            },
            "visiblePages": {
                "type": "number",
                "mutable": true,
                "complexType": {
                    "original": "number",
                    "resolved": "number",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "The number of pages visible to select.\nDefault is 5.\nMin is 5."
                },
                "attribute": "visible-pages",
                "reflect": false,
                "defaultValue": "5"
            }
        };
    }
    static get states() {
        return {
            "currentPage": {}
        };
    }
    static get events() {
        return [{
                "method": "cpslPaginationChanged",
                "name": "cpslPaginationChanged",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Emitted when exit animation finishes."
                },
                "complexType": {
                    "original": "number",
                    "resolved": "number",
                    "references": {}
                }
            }];
    }
    static get watchers() {
        return [{
                "propName": "currentPage",
                "methodName": "watchChange"
            }];
    }
}
//# sourceMappingURL=cpsl-pagination.js.map
