import { defineComponent, inject, computed, resolveComponent, createElementBlock, openBlock, normalizeClass, createVNode, unref } from 'vue';
import { version } from 'element-plus';
import { TableCtxKey } from '../../../../../constants/table.mjs';
import { isVersionGreaterOrEqual } from '../../../../../utils/src/general.mjs';
import { tablePaginationProps } from './tablePagination.mjs';

var _sfc_main = /* @__PURE__ */ defineComponent({
  ...{
    name: "GmTablePagination"
  },
  __name: "TablePagination",
  props: tablePaginationProps,
  emits: ["update:page", "update:limit", "pagination"],
  setup(__props, { emit: __emit }) {
    const tableCtx = inject(TableCtxKey, {});
    const canUseSize = isVersionGreaterOrEqual(version, "2.7.6");
    const props = __props;
    const emit = __emit;
    const currentPage = computed({
      get() {
        return props.page;
      },
      set(val) {
        emit("update:page", val);
      }
    });
    const pageSize = computed({
      get() {
        return props.limit;
      },
      set(val) {
        emit("update:limit", val);
      }
    });
    function handleSizeChange(val) {
      if (currentPage.value * val > props.total) {
        currentPage.value = 1;
      }
      emit("pagination", { page: currentPage.value, limit: val });
      tableCtx?.tableRef?.value?.scrollTo({ top: 0, behavior: "smooth" });
      tableCtx.tableV2Ref?.value?.scrollTo({ scrollLeft: 0, scrollTop: 0 });
    }
    function handleCurrentChange(val) {
      emit("pagination", { page: val, limit: pageSize.value });
      tableCtx?.tableRef?.value?.scrollTo({ top: 0, behavior: "smooth" });
      tableCtx.tableV2Ref?.value?.scrollTo({ scrollLeft: 0, scrollTop: 0 });
    }
    return (_ctx, _cache) => {
      const _component_gm_pagination = resolveComponent("gm-pagination");
      return openBlock(), createElementBlock(
        "div",
        {
          class: normalizeClass([{ hidden: _ctx.hidden }, "pagination-container gm-flex gm-justify-end"])
        },
        [
          createVNode(_component_gm_pagination, {
            "current-page": currentPage.value,
            "onUpdate:currentPage": _cache[0] || (_cache[0] = ($event) => currentPage.value = $event),
            "page-size": pageSize.value,
            "onUpdate:pageSize": _cache[1] || (_cache[1] = ($event) => pageSize.value = $event),
            background: _ctx.background,
            layout: _ctx.layout,
            "page-sizes": _ctx.pageSizes,
            "pager-count": _ctx.pagerCount,
            total: _ctx.total,
            small: unref(canUseSize) ? void 0 : _ctx.small,
            size: unref(canUseSize) ? _ctx.size : void 0,
            onSizeChange: handleSizeChange,
            onCurrentChange: handleCurrentChange
          }, null, 8, ["current-page", "page-size", "background", "layout", "page-sizes", "pager-count", "total", "small", "size"])
        ],
        2
        /* CLASS */
      );
    };
  }
});

export { _sfc_main as default };
//# sourceMappingURL=TablePagination.vue2.mjs.map
