'use strict';

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

const index = require('./index-305226cb.js');

const bsPaginationCss = ".bs-pagination{background-color:var(--bs-bg-base);color:var(--bs-ink-base);display:flex;gap:1.5rem;justify-content:center}.bs-pagination:where([data-variant=\"group\"]){gap:2.5rem}.bs-pagination:where([data-variant=\"results\"]){gap:1rem}.bs-pagination :where(button){align-items:center;border-radius:4px;cursor:pointer;display:flex;min-height:1.5rem;justify-content:center;outline:2px solid transparent;min-width:1.5rem}.bs-pagination button:where(:focus),.bs-pagination button:where(:focus-visible){outline:2px solid var(--bs-blue-base)}.bs-pagination button:where(:focus:not(:focus-visible)){outline:2px solid transparent}.bs-pagination button:where(:disabled){color:var(--bs-ink-disabled)}.bs-pagination button:where([data-selected]){background-color:var(--bs-blue-base);color:var(--bs-gray-50)}.bs-pagination:where([data-fixed]){background-color:var(--bs-bg-base-elevated);bottom:0;left:0;padding:1rem;position:fixed;width:100%}.bs-pagination :where(.bs-pagination-page-numbers){display:flex;gap:1rem}.bs-pagination :where(.bs-pagination-carets){display:flex;gap:1.5rem}.bs-pagination :where(.bs-pagination-word-caret){align-items:center;gap:0.5rem;width:auto}.bs-pagination :where(.bs-pagination-results-numbers){display:flex;gap:0.5rem}.bs-pagination :where(.bs-pagination-results-numbers) button{padding-left:2px;padding-right:2px;width:auto}bs-pagination{display:block}";

const BsPagination = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.selchange = index.createEvent(this, "selchange", 7);
    this.showFirstLastButtons = false;
    this.prevPageText = undefined;
    this.firstPageText = undefined;
    this.nextPageText = undefined;
    this.lastPageText = undefined;
    this.fixed = false;
    this.variant = 'base';
    this.selection = undefined;
    this.centerDisplay = 'numbers';
    this.breakText = '…';
    this.pageRange = 3;
    this.pageCount = undefined;
    this.centerLabel = undefined;
    this.itemsPerGroup = 25;
    this.itemsCount = undefined;
    this.resultsIncrements = undefined;
    this.selectionState = undefined;
  }
  componentDidLoad() {
    // If no selection initially given, select the first option
    if (this.selection === undefined) {
      if (this.variant === 'results') {
        this.selectionState = parseInt(this.resultsIncrements.split(',')[0]);
      }
      else {
        this.selectionState = 1;
      }
    }
    else {
      this.selectionState = this.selection;
    }
  }
  handleSelectionPropChange(newVal) {
    this.selectionState = newVal;
  }
  handleSelectionStateChange(newVal, oldVal) {
    // Don't emit on initial load
    if (oldVal !== undefined) {
      this.selchange.emit(newVal);
    }
  }
  get actualPageCount() {
    if (!this._actualPageCount) {
      if (this.variant === 'group') {
        this._actualPageCount = Math.ceil(this.itemsCount / this.itemsPerGroup);
      }
      else if (this.variant === 'base') {
        this._actualPageCount = this.pageCount;
      }
    }
    return this._actualPageCount;
  }
  /**
   * Reset actual page count when any of these props changes so it can be
   * re-calculated on next `get`.
   */
  handlePageCountChange() {
    this._actualPageCount = undefined;
  }
  /**
   * Handles special variant cases such as `centerDisplay="text"` having the
   * same style as `variant="group"`.
   */
  getVariant() {
    if (this.variant === 'base') {
      if (this.centerDisplay === 'text') {
        return 'group';
      }
      else {
        return undefined;
      }
    }
    return this.variant;
  }
  handleFirstNav() {
    this.selectionState = 1;
  }
  getFirstPageButton() {
    const disabled = this.selectionState === 1;
    if (this.showFirstLastButtons) {
      if (this.firstPageText) {
        return (index.h("button", { type: "button", class: "bs-pagination-word-caret", title: this.firstPageText, disabled: disabled, onClick: () => this.handleFirstNav() }, index.h("bs-icon-caret-left-double", null), this.firstPageText));
      }
      else {
        return (index.h("button", { type: "button", title: "first", disabled: disabled, onClick: () => this.handleFirstNav() }, index.h("bs-icon-caret-left-double", null)));
      }
    }
    return undefined;
  }
  handlePrevNav() {
    // Use Math.max to never go below the first page
    this.selectionState = Math.max(this.selectionState - 1, 1);
  }
  getPrevPageButton() {
    const disabled = this.selectionState === 1;
    if (this.prevPageText) {
      return (index.h("button", { type: "button", class: "bs-pagination-word-caret", title: this.prevPageText, disabled: disabled, onClick: () => this.handlePrevNav() }, index.h("bs-icon-caret-left", null), this.prevPageText));
    }
    else {
      return (index.h("button", { type: "button", title: "prev", disabled: disabled, onClick: () => this.handlePrevNav() }, index.h("bs-icon-caret-left", null)));
    }
  }
  handleLastNav() {
    this.selectionState = this.actualPageCount;
  }
  getLastPageButton() {
    const disabled = this.selectionState === this.actualPageCount;
    if (this.showFirstLastButtons) {
      if (this.lastPageText) {
        return (index.h("button", { type: "button", class: "bs-pagination-word-caret", title: this.lastPageText, disabled: disabled, onClick: () => this.handleLastNav() }, this.lastPageText, index.h("bs-icon-caret-right-double", null)));
      }
      else {
        return (index.h("button", { type: "button", title: "last", disabled: disabled, onClick: () => this.handleLastNav() }, index.h("bs-icon-caret-right-double", null)));
      }
    }
    return undefined;
  }
  handleNextNav() {
    // Use Math.min to never go over the pageCount
    this.selectionState = Math.min(this.selectionState + 1, this.actualPageCount);
  }
  getNextPageButton() {
    const disabled = this.selectionState === this.actualPageCount;
    if (this.nextPageText) {
      return (index.h("button", { type: "button", class: "bs-pagination-word-caret", title: this.nextPageText, disabled: disabled, onClick: () => this.handleNextNav() }, this.nextPageText, index.h("bs-icon-caret-right", null)));
    }
    else {
      return (index.h("button", { type: "button", title: "next", disabled: disabled, onClick: () => this.handleNextNav() }, index.h("bs-icon-caret-right", null)));
    }
  }
  determinePageNumberRange() {
    // Start assuming all pages shown
    let left = 1;
    let right = this.actualPageCount;
    // Not showing all pages, so time to calculate
    if (this.pageRange < this.actualPageCount) {
      // Look left/right
      const offset = (this.pageRange - 1) / 2;
      left = this.selectionState - offset;
      right = this.selectionState + offset;
      // Extended past left end, so look right by the extra amount
      if (left < 1) {
        right = right + (1 - left);
        left = 1;
      }
      // Extended past right end, so look left by the extra amount
      if (right > this.actualPageCount) {
        left = left - (right - this.actualPageCount);
        right = this.actualPageCount;
      }
    }
    // Both ends are already included in the output, so this left/right update
    // prevents duplicates of them
    if (left === 1) {
      left++;
    }
    if (right === this.actualPageCount) {
      right--;
    }
    return { left, right };
  }
  handleSelection(selection) {
    this.selectionState = selection;
  }
  getPageNumbersDisplay() {
    // Always show first page number
    const pageNumbers = [
      index.h("button", { type: "button", key: "page-1", "data-selected": this.selectionState === 1 ? '' : undefined, disabled: this.selectionState === 1, onClick: () => this.handleSelection(1) }, "1")
    ];
    // Only one page, no need for further calculation
    if (this.actualPageCount === 1) {
      return pageNumbers;
    }
    const { left, right } = this.determinePageNumberRange();
    // Show left dots if left is at least two spaces away from the first page
    if (left > 2) {
      pageNumbers.push(index.h("span", null, this.breakText));
    }
    // Add the page numbers to display (e.g. for pageRange === 3, [7, 8, 9])
    for (let i = left; i <= right; i++) {
      pageNumbers.push(index.h("button", { type: "button", key: `page-${i}`, "data-selected": this.selectionState === i ? '' : undefined, disabled: this.selectionState === i, onClick: () => this.handleSelection(i) }, i));
    }
    // Show right dots if the right is at least two spaces away from the last page
    if (right < this.actualPageCount - 1) {
      pageNumbers.push(index.h("span", null, this.breakText));
    }
    // Always show the last page number
    pageNumbers.push(index.h("button", { type: "button", key: `page-${this.actualPageCount}`, "data-selected": this.selectionState === this.actualPageCount ? '' : undefined, disabled: this.selectionState === this.actualPageCount, onClick: () => this.handleSelection(this.actualPageCount) }, this.actualPageCount));
    return pageNumbers;
  }
  getPageTextDisplay() {
    var _a;
    return `${(_a = this.centerLabel) !== null && _a !== void 0 ? _a : 'Page'} ${this.selectionState} of ${this.actualPageCount}`;
  }
  getGroupTextDisplay() {
    var _a;
    // selectionState starts at 1 rather than 0
    const first = ((this.selectionState - 1) * this.itemsPerGroup) + 1;
    // Use Math.min to never go over the totalCounts
    const last = Math.min((first - 1) + this.itemsPerGroup, this.itemsCount);
    return `${(_a = this.centerLabel) !== null && _a !== void 0 ? _a : 'Results'} ${first} - ${last} of ${this.itemsCount}`;
  }
  getResultsDisplay() {
    const increments = this.resultsIncrements.split(',');
    const result = increments.map((increment) => {
      const incrementNum = parseInt(increment);
      return (index.h("button", { type: "button", key: `results-${increment}`, "data-selected": this.selectionState === incrementNum ? '' : undefined, disabled: this.selectionState === incrementNum, onClick: () => this.handleSelection(incrementNum) }, increment));
    });
    return result;
  }
  render() {
    return (index.h(index.Host, null, index.h("div", { class: "bs-pagination", "data-variant": this.getVariant(), "data-fixed": this.fixed }, (this.variant === 'base' || this.variant === 'group') && (index.h("div", { class: "bs-pagination-carets" }, this.getFirstPageButton(), this.getPrevPageButton())), this.variant === 'base' && this.centerDisplay === 'numbers' && (index.h("div", { class: "bs-pagination-page-numbers" }, this.getPageNumbersDisplay())), this.variant === 'base' && this.centerDisplay === 'text' && (index.h("span", null, this.getPageTextDisplay())), this.variant === 'group' && (index.h("span", null, this.getGroupTextDisplay())), this.variant === 'results' && [
      index.h("span", null, "Results Per Page:"),
      index.h("div", { class: "bs-pagination-results-numbers" }, this.getResultsDisplay())
    ], (this.variant === 'base' || this.variant === 'group') && (index.h("div", { class: "bs-pagination-carets" }, this.getNextPageButton(), this.getLastPageButton())))));
  }
  static get watchers() { return {
    "selection": ["handleSelectionPropChange"],
    "selectionState": ["handleSelectionStateChange"],
    "pageCount": ["handlePageCountChange"],
    "itemsCount": ["handlePageCountChange"],
    "itemsPerGroup": ["handlePageCountChange"]
  }; }
};
BsPagination.style = bsPaginationCss;

exports.bs_pagination = BsPagination;

//# sourceMappingURL=bs-pagination.cjs.entry.js.map