"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const vue = require("vue");
const comp = require("../../ui/src/comp.js");
const XEUtils = require("xe-utils");
require("../../ui/index.js");
const log = require("../../ui/src/log.js");
const select = require("../../select/src/select.js");
const numberInput = require("../../number-input/src/number-input.js");
const config = require("../../../../@vxe-ui/core/es/src/config.js");
const useFns = require("../../../../@vxe-ui/core/es/src/useFns.js");
const i18n = require("../../../../@vxe-ui/core/es/src/i18n.js");
const event = require("../../../../@vxe-ui/core/es/src/event.js");
const icon = require("../../../../@vxe-ui/core/es/src/icon.js");
const VxePagerComponent = comp.defineVxeComponent({
  name: "VxePager",
  props: {
    size: {
      type: String,
      default: () => config.getConfig().pager.size || config.getConfig().size
    },
    // 自定义布局
    layouts: {
      type: Array,
      default: () => config.getConfig().pager.layouts || ["PrevJump", "PrevPage", "Jump", "PageCount", "NextPage", "NextJump", "Sizes", "Total"]
    },
    // 当前页
    currentPage: {
      type: Number,
      default: 1
    },
    // 加载中
    loading: Boolean,
    // 每页大小
    pageSize: {
      type: Number,
      default: () => config.getConfig().pager.pageSize || 10
    },
    // 总条数
    total: { type: Number, default: 0 },
    // 显示页码按钮的数量
    pagerCount: {
      type: Number,
      default: () => config.getConfig().pager.pagerCount || 7
    },
    // 每页大小选项列表
    pageSizes: {
      type: Array,
      default: () => config.getConfig().pager.pageSizes || [10, 15, 20, 50, 100]
    },
    // 列对齐方式
    align: {
      type: String,
      default: () => config.getConfig().pager.align
    },
    // 带边框
    border: {
      type: Boolean,
      default: () => config.getConfig().pager.border
    },
    // 带背景颜色
    background: {
      type: Boolean,
      default: () => config.getConfig().pager.background
    },
    // 配套的样式
    perfect: {
      type: Boolean,
      default: () => config.getConfig().pager.perfect
    },
    // 当只有一页时隐藏
    autoHidden: {
      type: Boolean,
      default: () => config.getConfig().pager.autoHidden
    },
    transfer: {
      type: Boolean,
      default: () => config.getConfig().pager.transfer
    },
    className: [String, Function],
    pageSizePlacement: {
      type: String,
      default: () => config.getConfig().pager.pageSizePlacement
    },
    // 自定义图标
    iconPrevPage: String,
    iconJumpPrev: String,
    iconJumpNext: String,
    iconNextPage: String,
    iconJumpMore: String,
    iconHomePage: String,
    iconEndPage: String
  },
  emits: [
    "update:pageSize",
    "update:currentPage",
    "page-change"
  ],
  setup(props, context) {
    const { slots, emit } = context;
    const xID = XEUtils.uniqueId();
    const { computeSize } = useFns.useSize(props);
    const $xeGrid = vue.inject("$xeGrid", null);
    const reactData = vue.reactive({
      inpCurrPage: props.currentPage
    });
    const refElem = vue.ref();
    const refMaps = {
      refElem
    };
    const computePageCount = vue.computed(() => {
      return getPageCount(props.total, props.pageSize);
    });
    const computeNumList = vue.computed(() => {
      const { pagerCount } = props;
      const pageCount = computePageCount.value;
      const len = pageCount > pagerCount ? pagerCount - 2 : pagerCount;
      const rest = [];
      for (let index = 0; index < len; index++) {
        rest.push(index);
      }
      return rest;
    });
    const computeOffsetNumber = vue.computed(() => {
      return Math.floor((props.pagerCount - 2) / 2);
    });
    const computeSizeList = vue.computed(() => {
      return props.pageSizes.map((item) => {
        if (XEUtils.isNumber(item)) {
          return {
            value: item,
            label: `${i18n.getI18n("vxe.pager.pagesize", [item])}`
          };
        }
        return Object.assign({ value: "", label: "" }, item);
      });
    });
    const $xePager = {
      xID,
      props,
      context,
      getRefMaps: () => refMaps
    };
    const dispatchEvent = (type, params, evnt) => {
      emit(type, event.createEvent(evnt, { $pager: $xePager }, params));
    };
    const getPageCount = (total, size) => {
      return Math.max(Math.ceil(total / size), 1);
    };
    const handleJumpPageEvent = (evnt, currentPage) => {
      emit("update:currentPage", currentPage);
      if (evnt && currentPage !== props.currentPage) {
        dispatchEvent("page-change", { type: "current", pageSize: props.pageSize, currentPage }, evnt);
      }
    };
    const handleChangeCurrentPage = (currentPage, evnt) => {
      emit("update:currentPage", currentPage);
      if (evnt && currentPage !== props.currentPage) {
        dispatchEvent("page-change", { type: "current", pageSize: props.pageSize, currentPage }, evnt);
      }
    };
    const triggerJumpEvent = (params) => {
      const { $event } = params;
      const inputElem = $event.target;
      const inpValue = XEUtils.toInteger(inputElem.value);
      const pageCount = computePageCount.value;
      const current = inpValue <= 0 ? 1 : inpValue >= pageCount ? pageCount : inpValue;
      const currPage = XEUtils.toValueString(current);
      inputElem.value = currPage;
      reactData.inpCurrPage = currPage;
      handleChangeCurrentPage(current, $event);
    };
    const handleHomePage = (evnt) => {
      const { currentPage } = props;
      if (currentPage > 1) {
        handleChangeCurrentPage(1, evnt);
      }
    };
    const handleEndPage = (evnt) => {
      const { currentPage } = props;
      const pageCount = computePageCount.value;
      if (currentPage < pageCount) {
        handleChangeCurrentPage(pageCount, evnt);
      }
    };
    const handlePrevPage = (evnt) => {
      const { currentPage } = props;
      const pageCount = computePageCount.value;
      if (currentPage > 1) {
        handleChangeCurrentPage(Math.min(pageCount, Math.max(currentPage - 1, 1)), evnt);
      }
    };
    const handleNextPage = (evnt) => {
      const { currentPage } = props;
      const pageCount = computePageCount.value;
      if (currentPage < pageCount) {
        handleChangeCurrentPage(Math.min(pageCount, currentPage + 1), evnt);
      }
    };
    const handlePrevJump = (evnt) => {
      const numList = computeNumList.value;
      handleChangeCurrentPage(Math.max(props.currentPage - numList.length, 1), evnt);
    };
    const handleNextJump = (evnt) => {
      const pageCount = computePageCount.value;
      const numList = computeNumList.value;
      handleChangeCurrentPage(Math.min(props.currentPage + numList.length, pageCount), evnt);
    };
    const pageSizeEvent = (params) => {
      const { value, $event } = params;
      const pageSize = XEUtils.toNumber(value);
      const pageCount = getPageCount(props.total, pageSize);
      let currentPage = props.currentPage;
      if (currentPage > pageCount) {
        currentPage = pageCount;
        emit("update:currentPage", pageCount);
      }
      emit("update:pageSize", pageSize);
      if ($event) {
        dispatchEvent("page-change", { type: "size", pageSize, currentPage }, $event);
      }
    };
    const jumpKeydownEvent = (params) => {
      const { $event } = params;
      if (event.globalEvents.hasKey($event, event.GLOBAL_EVENT_KEYS.ENTER)) {
        triggerJumpEvent(params);
      } else if (event.globalEvents.hasKey($event, event.GLOBAL_EVENT_KEYS.ARROW_UP)) {
        $event.preventDefault();
        handleNextPage($event);
      } else if (event.globalEvents.hasKey($event, event.GLOBAL_EVENT_KEYS.ARROW_DOWN)) {
        $event.preventDefault();
        handlePrevPage($event);
      }
    };
    const renderHomePage = () => {
      const { currentPage, total } = props;
      const homeSlot = slots.home;
      const pageCount = computePageCount.value;
      if (homeSlot) {
        return vue.h("span", {
          class: "vxe-pager--custom-home-btn"
        }, homeSlot({ $pager: $xePager, total, currentPage, pageCount }));
      }
      return vue.h("button", {
        class: ["vxe-pager--home-btn", {
          "is--disabled": currentPage <= 1
        }],
        type: "button",
        title: i18n.getI18n("vxe.pager.homePageTitle"),
        onClick: handleHomePage
      }, [
        vue.h("i", {
          class: ["vxe-pager--btn-icon", props.iconHomePage || icon.getIcon().PAGER_HOME]
        })
      ]);
    };
    const renderPrevPage = () => {
      const { currentPage, total } = props;
      const prevPageSlot = slots.prevPage || slots["prev-page"];
      const pageCount = computePageCount.value;
      if (prevPageSlot) {
        return vue.h("span", {
          class: "vxe-pager--custom-prev-btn"
        }, prevPageSlot({ $pager: $xePager, total, currentPage, pageCount }));
      }
      return vue.h("button", {
        class: ["vxe-pager--prev-btn", {
          "is--disabled": currentPage <= 1
        }],
        type: "button",
        title: i18n.getI18n("vxe.pager.prevPageTitle"),
        onClick: handlePrevPage
      }, [
        vue.h("i", {
          class: ["vxe-pager--btn-icon", props.iconPrevPage || icon.getIcon().PAGER_PREV_PAGE]
        })
      ]);
    };
    const renderPrevJump = (tagName) => {
      const { currentPage, total } = props;
      const prevJumpSlot = slots.prevJump || slots["prev-jump"];
      const pageCount = computePageCount.value;
      if (prevJumpSlot) {
        return vue.h("span", {
          class: "vxe-pager--custom-jump-prev"
        }, prevJumpSlot({ $pager: $xePager, total, currentPage, pageCount }));
      }
      return vue.h(tagName || "button", {
        class: ["vxe-pager--jump-prev", {
          "is--fixed": !tagName,
          "is--disabled": currentPage <= 1
        }],
        type: "button",
        title: i18n.getI18n("vxe.pager.prevJumpTitle"),
        onClick: handlePrevJump
      }, [
        tagName ? vue.h("i", {
          class: ["vxe-pager--jump-more-icon", props.iconJumpMore || icon.getIcon().PAGER_JUMP_MORE]
        }) : null,
        vue.h("i", {
          class: ["vxe-pager--jump-icon", props.iconJumpPrev || icon.getIcon().PAGER_JUMP_PREV]
        })
      ]);
    };
    const renderNextJump = (tagName) => {
      const { currentPage, total } = props;
      const nextJumpSlot = slots.nextJump || slots["next-jump"];
      const pageCount = computePageCount.value;
      if (nextJumpSlot) {
        return vue.h("span", {
          class: "vxe-pager--custom-jump-next"
        }, nextJumpSlot({ $pager: $xePager, total, currentPage, pageCount }));
      }
      return vue.h(tagName || "button", {
        class: ["vxe-pager--jump-next", {
          "is--fixed": !tagName,
          "is--disabled": currentPage >= pageCount
        }],
        type: "button",
        title: i18n.getI18n("vxe.pager.nextJumpTitle"),
        onClick: handleNextJump
      }, [
        tagName ? vue.h("i", {
          class: ["vxe-pager--jump-more-icon", props.iconJumpMore || icon.getIcon().PAGER_JUMP_MORE]
        }) : null,
        vue.h("i", {
          class: ["vxe-pager--jump-icon", props.iconJumpNext || icon.getIcon().PAGER_JUMP_NEXT]
        })
      ]);
    };
    const renderNextPage = () => {
      const { currentPage, total } = props;
      const nextPageSlot = slots.nextPage || slots["next-page"];
      const pageCount = computePageCount.value;
      if (nextPageSlot) {
        return vue.h("span", {
          class: "vxe-pager--custom-next-btn"
        }, nextPageSlot({ $pager: $xePager, total, currentPage, pageCount }));
      }
      return vue.h("button", {
        class: ["vxe-pager--next-btn", {
          "is--disabled": currentPage >= pageCount
        }],
        type: "button",
        title: i18n.getI18n("vxe.pager.nextPageTitle"),
        onClick: handleNextPage
      }, [
        vue.h("i", {
          class: ["vxe-pager--btn-icon", props.iconNextPage || icon.getIcon().PAGER_NEXT_PAGE]
        })
      ]);
    };
    const renderEndPage = () => {
      const { currentPage, total } = props;
      const endSlot = slots.end;
      const pageCount = computePageCount.value;
      if (endSlot) {
        return vue.h("span", {
          class: "vxe-pager--custom-end-btn"
        }, endSlot({ $pager: $xePager, total, currentPage, pageCount }));
      }
      return vue.h("button", {
        class: ["vxe-pager--end-btn", {
          "is--disabled": currentPage >= pageCount
        }],
        type: "button",
        title: i18n.getI18n("vxe.pager.endPageTitle"),
        onClick: handleEndPage
      }, [
        vue.h("i", {
          class: ["vxe-pager--btn-icon", props.iconEndPage || icon.getIcon().PAGER_END]
        })
      ]);
    };
    const renderNumber = (showJump) => {
      const { currentPage, total, pagerCount } = props;
      const numberSlot = showJump ? slots.numberJump || slots["number-jump"] : slots.number;
      const nums = [];
      const pageCount = computePageCount.value;
      const numList = computeNumList.value;
      const offsetNumber = computeOffsetNumber.value;
      const isOv = pageCount > pagerCount;
      const isLt = isOv && currentPage > offsetNumber + 1;
      const isGt = isOv && currentPage < pageCount - offsetNumber;
      const restList = [];
      let startNumber = 1;
      if (isOv) {
        if (currentPage >= pageCount - offsetNumber) {
          startNumber = Math.max(pageCount - numList.length + 1, 1);
        } else {
          startNumber = Math.max(currentPage - offsetNumber, 1);
        }
      }
      if (showJump && isLt) {
        restList.push(1);
        nums.push(vue.h("button", {
          class: "vxe-pager--num-btn",
          type: "button",
          onClick: (evnt) => handleJumpPageEvent(evnt, 1)
        }, "1"), renderPrevJump("span"));
      }
      numList.forEach((item, index) => {
        const number = startNumber + index;
        if (number <= pageCount) {
          restList.push(number);
          nums.push(vue.h("button", {
            key: number,
            class: ["vxe-pager--num-btn", {
              "is--active": currentPage === number
            }],
            type: "button",
            onClick: (evnt) => handleJumpPageEvent(evnt, number)
          }, `${number}`));
        }
      });
      if (showJump && isGt) {
        restList.push(pageCount);
        nums.push(renderNextJump("button"), vue.h("button", {
          class: "vxe-pager--num-btn",
          type: "button",
          onClick: (evnt) => handleJumpPageEvent(evnt, pageCount)
        }, pageCount));
      }
      if (numberSlot) {
        return vue.h("span", {
          class: "vxe-pager--custom-btn-wrapper"
        }, numberSlot({ $pager: $xePager, total, numList: restList, currentPage, pageCount }));
      }
      return vue.h("span", {
        class: "vxe-pager--btn-wrapper"
      }, nums);
    };
    const renderJumpNumber = () => {
      return renderNumber(true);
    };
    const renderSizes = () => {
      const { total, currentPage, pageSize, pageSizePlacement, transfer } = props;
      const sizesSlot = slots.sizes;
      const sizeList = computeSizeList.value;
      const pageCount = computePageCount.value;
      if (sizesSlot) {
        return vue.h("span", {
          class: "vxe-pager--custom-sizes"
        }, sizesSlot({ $pager: $xePager, total, currentPage, pageCount, pageSize, options: sizeList }));
      }
      return vue.h(select.default, {
        class: "vxe-pager--sizes",
        modelValue: pageSize,
        placement: pageSizePlacement,
        transfer,
        options: sizeList,
        onChange: pageSizeEvent
      });
    };
    const renderJump = (isFull) => {
      const { total } = props;
      const { inpCurrPage } = reactData;
      const jumpSlot = isFull ? slots.fullJump || slots["full-jump"] : slots.jump;
      const pageCount = computePageCount.value;
      if (jumpSlot) {
        return vue.h("span", {
          class: "vxe-pager--custom-jump"
        }, jumpSlot({ $pager: $xePager, total, currentPage: inpCurrPage, pageCount }));
      }
      return vue.h("span", {
        class: "vxe-pager--jump"
      }, [
        isFull ? vue.h("span", {
          class: "vxe-pager--goto-text"
        }, i18n.getI18n("vxe.pager.goto")) : null,
        vue.h(numberInput.default, {
          class: "vxe-pager--goto",
          modelValue: reactData.inpCurrPage,
          placeholder: i18n.getI18n("vxe.pager.gotoTitle"),
          align: "center",
          type: "integer",
          max: pageCount,
          min: 1,
          controls: false,
          onKeydown: jumpKeydownEvent,
          onBlur: triggerJumpEvent,
          "onUpdate:modelValue"(val) {
            reactData.inpCurrPage = val;
          }
        }),
        isFull ? vue.h("span", {
          class: "vxe-pager--classifier-text"
        }, i18n.getI18n("vxe.pager.pageClassifier")) : null
      ]);
    };
    const renderFullJump = () => {
      return renderJump(true);
    };
    const renderPageCount = () => {
      const { currentPage, total } = props;
      const pageCountSlot = slots.pageCount || slots["page-count"];
      const pageCount = computePageCount.value;
      if (pageCountSlot) {
        return vue.h("span", {
          class: "vxe-pager--custom-count"
        }, pageCountSlot({ $pager: $xePager, total, currentPage, pageCount }));
      }
      return vue.h("span", {
        class: "vxe-pager--count"
      }, [
        vue.h("span", {
          class: "vxe-pager--separator"
        }),
        vue.h("span", pageCount)
      ]);
    };
    const renderTotal = () => {
      const { currentPage, total } = props;
      const totalSlot = slots.total;
      const pageCount = computePageCount.value;
      if (totalSlot) {
        return vue.h("span", {
          class: "vxe-pager--custom-total"
        }, totalSlot({ $pager: $xePager, total, currentPage, pageCount }));
      }
      return vue.h("span", {
        class: "vxe-pager--total"
      }, i18n.getI18n("vxe.pager.total", [total]));
    };
    const pagerMethods = {
      dispatchEvent,
      setPageSize(num) {
        pageSizeEvent({ value: num });
        return vue.nextTick();
      },
      setPageSizeByEvent(evnt, num) {
        pageSizeEvent({ value: num, $event: evnt });
      },
      homePage() {
        handleHomePage();
        return vue.nextTick();
      },
      homePageByEvent(evnt) {
        handleHomePage(evnt);
      },
      endPage() {
        handleEndPage();
        return vue.nextTick();
      },
      endPageByEvent(evnt) {
        handleEndPage(evnt);
      },
      prevPage() {
        handlePrevPage();
        return vue.nextTick();
      },
      prevPageByEvent(evnt) {
        handlePrevPage(evnt);
      },
      nextPage() {
        handleNextPage();
        return vue.nextTick();
      },
      nextPageByEvent(evnt) {
        handleNextPage(evnt);
      },
      prevJump() {
        handlePrevJump();
        return vue.nextTick();
      },
      prevJumpByEvent(evnt) {
        handlePrevJump(evnt);
      },
      nextJump() {
        handleNextJump();
        return vue.nextTick();
      },
      nextJumpByEvent(evnt) {
        handleNextJump(evnt);
      },
      setCurrentPage(currentPage) {
        const current = XEUtils.toNumber(currentPage) || 1;
        reactData.inpCurrPage = current;
        handleChangeCurrentPage(current);
        return vue.nextTick();
      },
      setCurrentPageByEvent(evnt, currentPage) {
        const current = XEUtils.toNumber(currentPage) || 1;
        reactData.inpCurrPage = current;
        handleChangeCurrentPage(current, evnt);
      },
      /**
       * 已废弃，被 setCurrentPage 替换
       * @deprecated
       */
      jumpPage(currentPage) {
        log.warnLog("vxe.error.delFunc", ["[pager] jumpPage", "setCurrentPage"]);
        return $xePager.setCurrentPage(currentPage);
      }
    };
    const pagerPrivateMethods = {
      handlePrevPage,
      handleNextPage,
      handlePrevJump,
      handleNextJump
    };
    Object.assign($xePager, pagerMethods, pagerPrivateMethods);
    vue.watch(() => props.currentPage, (value) => {
      reactData.inpCurrPage = value;
    });
    const renderVN = () => {
      const { align, layouts, className } = props;
      const childNodes = [];
      const vSize = computeSize.value;
      const pageCount = computePageCount.value;
      if (slots.left) {
        childNodes.push(vue.h("span", {
          class: "vxe-pager--left-wrapper"
        }, slots.left({ $grid: $xeGrid })));
      }
      layouts.forEach((name) => {
        let renderFn;
        switch (name) {
          case "Home":
            renderFn = renderHomePage;
            break;
          case "PrevJump":
            renderFn = renderPrevJump;
            break;
          case "PrevPage":
            renderFn = renderPrevPage;
            break;
          case "Number":
            renderFn = renderNumber;
            break;
          case "JumpNumber":
            renderFn = renderJumpNumber;
            break;
          case "NextPage":
            renderFn = renderNextPage;
            break;
          case "NextJump":
            renderFn = renderNextJump;
            break;
          case "End":
            renderFn = renderEndPage;
            break;
          case "Sizes":
            renderFn = renderSizes;
            break;
          case "FullJump":
            renderFn = renderFullJump;
            break;
          case "Jump":
            renderFn = renderJump;
            break;
          case "PageCount":
            renderFn = renderPageCount;
            break;
          case "Total":
            renderFn = renderTotal;
            break;
        }
        if (renderFn) {
          childNodes.push(renderFn());
        } else {
          log.errLog("vxe.error.notProp", [`[pager] layouts -> ${name}`]);
        }
      });
      if (slots.right) {
        childNodes.push(vue.h("span", {
          class: "vxe-pager--right-wrapper"
        }, slots.right({ $grid: $xeGrid })));
      }
      return vue.h("div", {
        ref: refElem,
        class: ["vxe-pager", className ? XEUtils.isFunction(className) ? className({ $pager: $xePager }) : className : "", {
          [`size--${vSize}`]: vSize,
          [`align--${align}`]: align,
          "is--border": props.border,
          "is--background": props.background,
          "is--perfect": props.perfect,
          "is--hidden": props.autoHidden && pageCount === 1,
          "is--loading": props.loading
        }]
      }, [
        vue.h("div", {
          class: "vxe-pager--wrapper"
        }, childNodes)
      ]);
    };
    $xePager.renderVN = renderVN;
    return $xePager;
  },
  render() {
    return this.renderVN();
  }
});
exports.default = VxePagerComponent;
