"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const Vue = require("vue");
const props = require("../utils/props.js");
const create = require("../utils/create.js");
const format = require("../utils/format.js");
const constant = require("../utils/constant.js");
const [name, bem, t] = create.createNamespace("pagination");
const makePage = (number, text, active) => ({
  number,
  text,
  active
});
const paginationProps = {
  mode: props.makeStringProp("multi"),
  prevText: String,
  nextText: String,
  pageCount: props.makeNumericProp(0),
  modelValue: props.makeNumberProp(0),
  totalItems: props.makeNumericProp(0),
  showPageSize: props.makeNumericProp(5),
  itemsPerPage: props.makeNumericProp(10),
  forceEllipses: Boolean,
  showPrevButton: props.truthProp,
  showNextButton: props.truthProp
};
var stdin_default = Vue.defineComponent({
  name,
  props: paginationProps,
  emits: ["change", "update:modelValue"],
  setup(props2, {
    emit,
    slots
  }) {
    const count = Vue.computed(() => {
      const {
        pageCount,
        totalItems,
        itemsPerPage
      } = props2;
      const count2 = +pageCount || Math.ceil(+totalItems / +itemsPerPage);
      return Math.max(1, count2);
    });
    const pages = Vue.computed(() => {
      const items = [];
      const pageCount = count.value;
      const showPageSize = +props2.showPageSize;
      const {
        modelValue,
        forceEllipses
      } = props2;
      let startPage = 1;
      let endPage = pageCount;
      const isMaxSized = showPageSize < pageCount;
      if (isMaxSized) {
        startPage = Math.max(modelValue - Math.floor(showPageSize / 2), 1);
        endPage = startPage + showPageSize - 1;
        if (endPage > pageCount) {
          endPage = pageCount;
          startPage = endPage - showPageSize + 1;
        }
      }
      for (let number = startPage; number <= endPage; number++) {
        const page = makePage(number, number, number === modelValue);
        items.push(page);
      }
      if (isMaxSized && showPageSize > 0 && forceEllipses) {
        if (startPage > 1) {
          const prevPages = makePage(startPage - 1, "...");
          items.unshift(prevPages);
        }
        if (endPage < pageCount) {
          const nextPages = makePage(endPage + 1, "...");
          items.push(nextPages);
        }
      }
      return items;
    });
    const updateModelValue = (value, emitChange) => {
      value = format.clamp(value, 1, count.value);
      if (props2.modelValue !== value) {
        emit("update:modelValue", value);
        if (emitChange) {
          emit("change", value);
        }
      }
    };
    Vue.watchEffect(() => updateModelValue(props2.modelValue));
    const renderDesc = () => Vue.createVNode("li", {
      "class": bem("page-desc")
    }, [slots.pageDesc ? slots.pageDesc() : `${props2.modelValue}/${count.value}`]);
    const renderPrevButton = () => {
      const {
        mode,
        modelValue,
        showPrevButton
      } = props2;
      if (!showPrevButton) {
        return;
      }
      const slot = slots["prev-text"];
      const disabled = modelValue === 1;
      return Vue.createVNode("li", {
        "class": [bem("item", {
          disabled,
          border: mode === "simple",
          prev: true
        }), constant.BORDER_SURROUND]
      }, [Vue.createVNode("button", {
        "type": "button",
        "disabled": disabled,
        "onClick": () => updateModelValue(modelValue - 1, true)
      }, [slot ? slot() : props2.prevText || t("prev")])]);
    };
    const renderNextButton = () => {
      const {
        mode,
        modelValue,
        showNextButton
      } = props2;
      if (!showNextButton) {
        return;
      }
      const slot = slots["next-text"];
      const disabled = modelValue === count.value;
      return Vue.createVNode("li", {
        "class": [bem("item", {
          disabled,
          border: mode === "simple",
          next: true
        }), constant.BORDER_SURROUND]
      }, [Vue.createVNode("button", {
        "type": "button",
        "disabled": disabled,
        "onClick": () => updateModelValue(modelValue + 1, true)
      }, [slot ? slot() : props2.nextText || t("next")])]);
    };
    const renderPages = () => pages.value.map((page) => Vue.createVNode("li", {
      "class": [bem("item", {
        active: page.active,
        page: true
      }), constant.BORDER_SURROUND]
    }, [Vue.createVNode("button", {
      "type": "button",
      "aria-current": page.active || void 0,
      "onClick": () => updateModelValue(page.number, true)
    }, [slots.page ? slots.page(page) : page.text])]));
    return () => Vue.createVNode("nav", {
      "role": "navigation",
      "class": bem()
    }, [Vue.createVNode("ul", {
      "class": bem("items")
    }, [renderPrevButton(), props2.mode === "simple" ? renderDesc() : renderPages(), renderNextButton()])]);
  }
});
exports.default = stdin_default;
exports.paginationProps = paginationProps;
