'use strict';

var index = require('./index-CDEIqi_n.js');
var jsSdk = require('@verdocs/js-sdk');
var utils = require('./utils-Bxil_g6M.js');
var popper = require('./popper-Dmp8OTYh.js');
require('./_commonjsHelpers-BJu3ubxk.js');
require('./index-CeUF44L9.js');
require('./Types-RUyU-svM.js');

const verdocsPaginationCss = "@-webkit-keyframes verdocs-field-pulse{0%{background-color:rgba(0, 0, 0, 0.35)}50%{background-color:rgba(0, 0, 0, 0)}100%{background-color:rgba(0, 0, 0, 0.35)}}@keyframes verdocs-field-pulse{0%{background-color:rgba(0, 0, 0, 0.35)}50%{background-color:rgba(0, 0, 0, 0)}100%{background-color:rgba(0, 0, 0, 0.35)}}verdocs-pagination{gap:8px;display:-ms-flexbox;display:flex;-ms-flex-direction:row;flex-direction:row;font-family:\"Inter\", \"Barlow\", sans-serif}verdocs-pagination .page-button{height:38px;display:-ms-flexbox;display:flex;-ms-flex:0 0 38px;flex:0 0 38px;cursor:pointer;font-size:16px;font-weight:600;border-radius:4px;-ms-flex-align:center;align-items:center;-ms-flex-pack:center;justify-content:center;color:#33364b;border:1px solid #654dcb}verdocs-pagination .page-button svg{width:18px;height:18px}verdocs-pagination .page-button.selected{cursor:default;color:#ffffff;background:#654dcb}verdocs-pagination .ellipsis{width:38px;height:38px;display:-ms-flexbox;display:flex;-ms-flex:0 0 38px;flex:0 0 38px;line-height:32px;-ms-flex-pack:center;justify-content:center;color:#33364b}";

const ChevronDoubleLeft = `<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="M18.75 19.5l-7.5-7.5 7.5-7.5m-6 15L5.25 12l7.5-7.5" /></svg>`;
const ChevronDoubleRight = `<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="M11.25 4.5l7.5 7.5-7.5 7.5m-6-15l7.5 7.5-7.5 7.5" /></svg>`;
const VISIBLE_PAGES = 5;
const PageButton = ({ page, selected, onClick }) => {
    let label;
    if (page === 'first') {
        label = ChevronDoubleLeft;
    }
    else if (page === 'last') {
        label = ChevronDoubleRight;
    }
    else {
        label = String(page + 1);
    }
    return index.h("div", { class: `page-button ${page === selected ? 'selected' : ''}`, onClick: () => onClick(page), innerHTML: label });
};
const VerdocsQuickFilter$1 = class VerdocsQuickFilter {
    constructor(hostRef) {
        index.registerInstance(this, hostRef);
        this.selectPage = index.createEvent(this, "selectPage");
        /**
         * The currently selected page.
         */
        this.selectedPage = 0;
        /**
         * The total number of items.
         */
        this.itemCount = 1;
        /**
         * The number of displayed per page.
         */
        this.perPage = 10;
    }
    handleSelectPage(page) {
        var _a;
        this.selectedPage = page;
        (_a = this.selectPage) === null || _a === void 0 ? void 0 : _a.emit({ selectedPage: page });
    }
    render() {
        const firstPage = Math.max(0, this.selectedPage - 2);
        const numPages = this.itemCount > 0 ? Math.ceil(this.itemCount / this.perPage) : 0;
        const pagesToDisplay = jsSdk.integerSequence(0, Math.ceil(this.itemCount / this.perPage)).slice(firstPage, firstPage + VISIBLE_PAGES);
        return (index.h(index.Host, { key: 'b914afe290065b1f65b3632a1e205a0138d4ca96' }, this.selectedPage > 0 && index.h(PageButton, { key: '06d4c575361cffc6299409c66557208fef8180e9', page: 'first', selected: this.selectedPage, onClick: () => this.handleSelectPage(0) }), firstPage > 0 && index.h("div", { key: '14afa9724ff8159c20c372821fff58a98b721bc7', class: "ellipsis" }, "..."), pagesToDisplay.map(pageNumber => (index.h(PageButton, { page: pageNumber, selected: this.selectedPage, onClick: page => this.handleSelectPage(+page) }))), this.selectedPage < numPages - 1 && index.h("div", { key: '0b22a68cafaf3beda898442422a584cc9da811d9', class: "ellipsis" }, "..."), this.selectedPage < numPages - 1 && index.h(PageButton, { key: '56bfdb75c0a6bb0aab32aa61860051517fcf360b', page: 'last', selected: this.selectedPage, onClick: () => this.handleSelectPage(numPages - 1) })));
    }
    get el() { return index.getElement(this); }
};
VerdocsQuickFilter$1.style = verdocsPaginationCss;

const verdocsQuickFilterCss = "@-webkit-keyframes verdocs-field-pulse{0%{background-color:rgba(0, 0, 0, 0.35)}50%{background-color:rgba(0, 0, 0, 0)}100%{background-color:rgba(0, 0, 0, 0.35)}}@keyframes verdocs-field-pulse{0%{background-color:rgba(0, 0, 0, 0.35)}50%{background-color:rgba(0, 0, 0, 0)}100%{background-color:rgba(0, 0, 0, 0.35)}}verdocs-quick-filter{position:relative;font-family:\"Inter\", \"Barlow\", sans-serif}verdocs-quick-filter .control{outline:0;height:34px;display:-ms-flexbox;display:flex;cursor:pointer;border-radius:4px;-ms-flex-align:center;align-items:center;-ms-flex-direction:row;flex-direction:row;-webkit-box-sizing:border-box;box-sizing:border-box;background:#ffffff;-webkit-transition:all 100ms ease 0s;transition:all 100ms ease 0s;border:1px solid #cccccc}verdocs-quick-filter .title{padding:0 8px;font-size:14px;max-width:100%;overflow:hidden;color:#33364b;white-space:nowrap;-webkit-box-sizing:border-box;box-sizing:border-box;text-overflow:ellipsis}verdocs-quick-filter .title .prefix{display:inline;font-weight:700;margin:0 7px 0 0;color:#4c56cb}verdocs-quick-filter .arrow{width:32px;height:32px;border:none;outline:none;cursor:pointer;-ms-flex-align:center;align-items:center;display:-ms-inline-flexbox;display:inline-flex;-ms-flex-pack:center;justify-content:center;background:#ffffff;-webkit-transition:color 150ms ease 0s;transition:color 150ms ease 0s}verdocs-quick-filter .arrow svg{-webkit-transition:0.3s;transition:0.3s;fill:#cccccc}verdocs-quick-filter .separator{width:1px;display:block;margin-top:8px;margin-bottom:8px;-ms-flex-item-align:stretch;align-self:stretch;-webkit-box-sizing:border-box;box-sizing:border-box;background-color:#cccccc}verdocs-quick-filter .items{display:none;z-index:10000;position:absolute;border-radius:4px;background:#ffffff;-webkit-box-shadow:rgba(0, 0, 0, 0.1) 0 0 0 1px, rgba(0, 0, 0, 0.1) 0 4px 11px;box-shadow:rgba(0, 0, 0, 0.1) 0 0 0 1px, rgba(0, 0, 0, 0.1) 0 4px 11px}verdocs-quick-filter .items[data-show]{display:block}verdocs-quick-filter .option{width:100%;border:none;display:block;font-size:14px;text-align:left;background:none;padding:10px 20px;color:#1e2031}verdocs-quick-filter .option:hover{cursor:pointer;background:rgba(76, 86, 203, 0.2470588235)}verdocs-quick-filter .option[disabled]{color:#aaaaaa}verdocs-quick-filter .option[disabled]:hover{cursor:inherit;background:transparent;color:#aaaaaa}verdocs-quick-filter.open .items{display:block}verdocs-quick-filter.open .arrow svg{-webkit-transform:scaleY(-1);transform:scaleY(-1)}";

const DropdownArrow = `<svg height="20" width="20" viewBox="0 0 20 20" aria-hidden="true" focusable="false" class="css-8mmkcg"><path d="M4.516 7.548c0.436-0.446 1.043-0.481 1.576 0l3.908 3.747 3.908-3.747c0.533-0.481 1.141-0.446 1.574 0 0.436 0.445 0.408 1.197 0 1.615-0.406 0.418-4.695 4.502-4.695 4.502-0.217 0.223-0.502 0.335-0.787 0.335s-0.57-0.112-0.789-0.335c0 0-4.287-4.084-4.695-4.502s-0.436-1.17 0-1.615z"></path></svg>`;
const VerdocsQuickFilter = class {
    constructor(hostRef) {
        index.registerInstance(this, hostRef);
        this.optionSelected = index.createEvent(this, "optionSelected");
        /**
         * The menu options to display.
         */
        this.options = [];
        this.label = 'Filter';
        this.value = '';
        this.placeholder = 'Select...';
        // We need to do this to reliably disconnect the click-away listener
        this.clickListenerSymbol = new AbortController();
    }
    componentDidLoad() {
        this.popper = popper.createPopper(this.dropdownButton, this.dropdownMenu, { placement: 'bottom-start', modifiers: [{ name: 'offset', options: { offset: [-1, 14] } }] });
        window.addEventListener('click', this.clickListener.bind(this), { signal: this.clickListenerSymbol.signal });
    }
    disconnectedCallback() {
        this.clickListenerSymbol.abort();
    }
    clickListener(e) {
        const outside = !(e.target == this.el || this.el.contains(e.target));
        if (outside) {
            this.hide();
        }
    }
    handleSelectOption(e, option) {
        e.stopPropagation();
        this.optionSelected.emit(option);
        this.value = option.value;
        this.hide();
    }
    // See https://popper.js.org/docs/v2/tutorial/
    // What we're doing here is clearing event listeners when they aren't needed, to increase performance in lists
    showDropdown() {
        var _a, _b;
        this.open = true;
        this.dropdownMenu.setAttribute('data-show', '');
        this.dropdownMenu.removeAttribute('aria-hidden');
        (_a = this.popper) === null || _a === void 0 ? void 0 : _a.setOptions(options => ({
            ...options,
            modifiers: [...options.modifiers, { name: 'eventListeners', enabled: true }],
        })).catch(() => { });
        (_b = this.popper) === null || _b === void 0 ? void 0 : _b.update().catch(() => { });
    }
    handleHideDropdown(e) {
        var _a, _b;
        if (((_a = e === null || e === void 0 ? void 0 : e.target) === null || _a === void 0 ? void 0 : _a.localName) === 'button' && ((_b = e === null || e === void 0 ? void 0 : e.target) === null || _b === void 0 ? void 0 : _b.className) === 'arrow') {
            // This event is fired when a menu element is clicked because the button "loses focus" then too
            return;
        }
        this.hide();
    }
    toggleDropdown(e) {
        e.stopPropagation();
        if (this.open) {
            this.hide();
        }
        else {
            this.showDropdown();
        }
    }
    hide() {
        var _a;
        this.open = false;
        this.dropdownMenu.removeAttribute('data-show');
        this.dropdownMenu.setAttribute('aria-hidden', 'true');
        (_a = this.popper) === null || _a === void 0 ? void 0 : _a.setOptions(options => ({
            ...options,
            modifiers: [...options.modifiers, { name: 'eventListeners', enabled: false }],
        })).catch(() => { });
    }
    render() {
        var _a, _b, _c;
        const selectedOption = (_a = this.options) === null || _a === void 0 ? void 0 : _a.find(option => option.value === this.value);
        const longestOption = (_b = this.options) === null || _b === void 0 ? void 0 : _b.reduce((prev, curr) => (prev.label.length > curr.label.length ? prev : curr));
        const longestLabelWidth = utils.renderedTextWidth((longestOption === null || longestOption === void 0 ? void 0 : longestOption.label) || 'FILLER', '14px Inter, Barlow, sans-serif');
        const prefixWidth = utils.renderedTextWidth(this.label, '14px Inter, Barlow, sans-serif');
        const minWidth = `${longestLabelWidth + prefixWidth + 40}px`;
        return (index.h(index.Host, { key: '2775a9460418249e5830b3acc29bc501dfc0b7e9', class: { open: !!this.open }, style: { minWidth, display: 'inline-block' } }, index.h("div", { key: 'a0d32bee5bf3a83860d1ee34b70dd08e14ff6f5a', class: "control", onClick: e => this.toggleDropdown(e), onBlur: e => this.handleHideDropdown(e) }, index.h("div", { key: 'e28fc8c51bd4913814770bfa3eed9aec685fcdc7', class: "title", ref: el => (this.dropdownButton = el) }, index.h("span", { key: '56f45a29290ed821c2260b07e34c76ad6f4b3008', class: "prefix" }, this.label, ":"), selectedOption ? selectedOption.label : this.placeholder), index.h("div", { key: 'dcc1f197621169c83f7c935bb06f60f992ac3a58', style: { flex: '1' } }), index.h("div", { key: '059cede12c8b2c1bca6b53f7e1f01b424dd70882', class: "separator" }), index.h("div", { key: '722b64600dca7b946044a60700b042d61774dabb', class: "arrow", innerHTML: DropdownArrow, "aria-label": "Open Menu" })), index.h("div", { key: '3988d0fd9076c4bc1bfb9d45d67f9354022a15cd', class: "items", "aria-hidden": !this.open, ref: el => (this.dropdownMenu = el), style: { minWidth } }, (_c = this.options) === null || _c === void 0 ? void 0 : _c.map(option => option.label ? (index.h("button", { onClick: e => this.handleSelectOption(e, option), class: `option ${option.value === this.value ? 'selected' : ''}`, disabled: option.disabled }, option.label)) : (index.h("div", { class: "separator" }))))));
    }
    get el() { return index.getElement(this); }
};
VerdocsQuickFilter.style = verdocsQuickFilterCss;

exports.verdocs_pagination = VerdocsQuickFilter$1;
exports.verdocs_quick_filter = VerdocsQuickFilter;
//# sourceMappingURL=verdocs-pagination.verdocs-quick-filter.entry.cjs.js.map

//# sourceMappingURL=verdocs-pagination_2.cjs.entry.js.map