import { r as registerInstance, c as createEvent, h } from './index-baa5e267.js';
import { i as initI18n } from './index-41363fa6.js';
import '@sankhyalabs/core';

const ezPaginationCss = ".sc-ez-pagination-h{--ez-pagination__info--color:var(--color--petroleum-500);--ez-pagination__info--margin-right:var(--space--sm);--ez-pagination__info--text-weight:var(--text-weight--medium);--ez-pagination__info--font-size:var(--text--medium);--ez-pagination__info-range--font-weight:var(--font-weight--bold);--ez-pagination__pages-option-padding:var(--space--nano);--ez-pagination__pages-option-color:var(--color--petroleum-500);--ez-pagination__pages-option-weight:var(--text-weight--medium);--ez-pagination__pages-option-size:var(--text--medium)}.pagination-container.sc-ez-pagination{display:flex;align-items:center;justify-content:flex-start;overflow:hidden}.info.sc-ez-pagination{color:var(--ez-pagination__info--color);margin-right:var(--ez-pagination__info--margin-right);font-weight:var(--ez-pagination__info--text-weight);font-size:var(--ez-pagination__info--font-size);white-space:nowrap;overflow:hidden;text-overflow:ellipsis;min-width:0}.info-range.sc-ez-pagination{font-weight:var(--ez-pagination__info-range--font-weight)}.controls.sc-ez-pagination{display:flex;align-items:center;flex-shrink:0}.controls-compact.sc-ez-pagination{gap:var(--space--2xs)}.pages-options.sc-ez-pagination{display:flex;align-items:center;padding:0 var(--ez-pagination__pages-option-padding)}.page-button.sc-ez-pagination{color:var(--ez-pagination__pages-option-color);font-weight:var(--ez-pagination__pages-option-weight);font-size:var(--ez-pagination__pages-option-size);background-color:transparent;padding:0;border:none;cursor:pointer;min-width:24px;height:24px;display:flex;align-items:center;justify-content:center}.current-page.sc-ez-pagination{color:var(--color--primary)}.page-info.sc-ez-pagination{margin:0 8px}.ellipsis.sc-ez-pagination{color:var(--ez-pagination__pages-option-color);font-weight:var(--ez-pagination__pages-option-weight);font-size:var(--ez-pagination__pages-option-size);min-width:24px;height:24px;display:flex;align-items:center;justify-content:center}";

const EzPagination = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.ezPageChange = createEvent(this, "ezPageChange", 7);
    this.handlePreviousPage = () => {
      if (this.currentPage > 1) {
        this.currentPage--;
        this.ezPageChange.emit(this.currentPage);
      }
    };
    this.handleNextPage = () => {
      if (this.currentPage < this.getTotalPages()) {
        this.currentPage++;
        this.ezPageChange.emit(this.currentPage);
      }
    };
    this.handlePageClick = (page) => {
      this.currentPage = page;
      this.ezPageChange.emit(this.currentPage);
    };
    this.type = 'default';
    this.currentPage = 1;
    this.totalItems = 0;
    this.pageSize = 10;
    this.hideInfoLabel = false;
    this.pageLimit = 10;
  }
  async componentWillLoad() {
    this.i18n = await initI18n();
  }
  getTotalPages() {
    return Math.ceil(this.totalItems / this.pageSize);
  }
  renderNextBtn() {
    return h("ez-button", { "aria-label": this.i18n('ez-pagination.nextPage'), label: this.i18n('ez-pagination.nextPage'), "data-element-id": "pagination-next-button", size: "small", iconName: "arrow-right", class: "ez-button--primary", mode: "icon", enabled: this.currentPage < this.getTotalPages(), onClick: () => this.handleNextPage() });
  }
  renderPreviousBtn() {
    return h("ez-button", { label: this.i18n('ez-pagination.previousPage'), "aria-label": this.i18n('ez-pagination.previousPage'), "data-element-id": "pagination-previous-button", size: "small", iconName: "arrow-left", mode: "icon", enabled: this.currentPage > 1, onClick: () => this.handlePreviousPage() });
  }
  renderPageButton(page) {
    return (h("button", { "aria-label": `page-${page}`, class: { 'page-button': true, 'current-page': this.currentPage === page }, onClick: () => this.handlePageClick(page) }, page));
  }
  renderEllipsis() {
    return h("span", { "aria-label": 'page-ellipsis', class: "ellipsis" }, "...");
  }
  renderPageButtons(pages) {
    return pages.map(page => (page === '...' ? this.renderEllipsis() : this.renderPageButton(page)));
  }
  renderInfoLabel(firstItem, lastItem) {
    return !this.hideInfoLabel && (h("div", { class: "info" }, h("span", { class: 'info-range' }, firstItem, " at\u00E9 ", lastItem), " de ", this.totalItems));
  }
  renderDefault() {
    const firstItem = (this.currentPage - 1) * this.pageSize + 1;
    const lastItem = Math.min(this.currentPage * this.pageSize, this.totalItems);
    return (h("div", { class: "pagination-container" }, this.renderInfoLabel(firstItem, lastItem), h("div", { class: "controls controls-compact" }, this.renderPreviousBtn(), this.renderNextBtn())));
  }
  renderNumber() {
    const totalPages = this.getTotalPages();
    const { currentPage, pageLimit } = this;
    const pages = [];
    if (totalPages <= pageLimit) {
      for (let i = 1; i <= totalPages; i++) {
        pages.push(i);
      }
    }
    else {
      const range = (start, end) => Array.from({ length: end - start + 1 }, (_, i) => start + i);
      const mainPagesCount = pageLimit - 2;
      const showLeftEllipsis = currentPage > mainPagesCount;
      const showRightEllipsis = currentPage < totalPages - mainPagesCount + 1;
      if (!showLeftEllipsis) {
        // Exibe o início da paginação sem elipse à esquerda
        // Ex: 1 2 3 4 5 6 ... 10 (pageLimit = 8)
        pages.push(...range(1, pageLimit - 2));
        pages.push('...');
        pages.push(totalPages);
      }
      else if (!showRightEllipsis) {
        // Exibe o final da paginação sem elipse à direita
        // Ex: 1 ... 5 6 7 8 9 10 (pageLimit = 8)
        pages.push(1);
        pages.push('...');
        pages.push(...range(totalPages - (pageLimit - 3), totalPages));
      }
      else {
        // Exibe elipses em ambos os lados
        // Ex: 1 ... 4 5 6 7 ... 10 (pageLimit = 8)
        const innerPagesCount = pageLimit - 4; // Contabiliza 1, ..., ..., totalPages
        const aroundCount = Math.floor((innerPagesCount - 1) / 2);
        const startPage = currentPage - aroundCount;
        const endPage = currentPage + (innerPagesCount - 1 - aroundCount);
        pages.push(1);
        pages.push('...');
        pages.push(...range(startPage, endPage));
        pages.push('...');
        pages.push(totalPages);
      }
    }
    return (h("div", { class: "pagination-container" }, h("div", { class: "controls" }, this.renderPreviousBtn(), h("div", { class: "pages-options" }, this.renderPageButtons(pages)), this.renderNextBtn())));
  }
  render() {
    return this.type === 'number' ? this.renderNumber() : this.renderDefault();
  }
};
EzPagination.style = ezPaginationCss;

export { EzPagination as ez_pagination };
