import { computed } from 'vue';
import '../../../../hooks/index.mjs';
import { useState } from '../../../../hooks/use-state/index.mjs';

const DEFAULT_PAGE_SIZE = 10;
function extendsObject(...list) {
  const result = { ...list[0] };
  for (let i = 1; i < list.length; i++) {
    const obj = list[i];
    if (obj) {
      Object.keys(obj).forEach((key) => {
        const val = obj[key];
        if (val !== void 0)
          result[key] = val;
      });
    }
  }
  return result;
}
function getPaginationParam(mergedPagination, pagination) {
  const param = {
    current: mergedPagination.current,
    pageSize: mergedPagination.pageSize
  };
  const paginationObj = pagination && typeof pagination === "object" ? pagination : {};
  Object.keys(paginationObj).forEach((pageProp) => {
    const value = mergedPagination[pageProp];
    if (typeof value !== "function")
      param[pageProp] = value;
  });
  return param;
}
function usePagination(totalRef, paginationRef, onChange) {
  const pagination = computed(
    () => paginationRef.value && typeof paginationRef.value === "object" ? paginationRef.value : {}
  );
  const paginationTotal = computed(() => pagination.value.total || 0);
  const [innerPagination, setInnerPagination] = useState(() => ({
    current: "defaultCurrent" in pagination.value ? pagination.value.defaultCurrent : 1,
    pageSize: "defaultPageSize" in pagination.value ? pagination.value.defaultPageSize : DEFAULT_PAGE_SIZE
  }));
  const mergedPagination = computed(() => {
    const mP = extendsObject(
      innerPagination.value,
      pagination.value,
      {
        total: paginationTotal.value > 0 ? paginationTotal.value : totalRef.value
      }
    );
    const maxPage = Math.ceil((paginationTotal.value || totalRef.value) / mP.pageSize);
    if (mP.current > maxPage) {
      mP.current = maxPage || 1;
    }
    return mP;
  });
  const refreshPagination = (current, pageSize) => {
    if (paginationRef.value === false)
      return;
    setInnerPagination({
      current: current != null ? current : 1,
      pageSize: pageSize || mergedPagination.value.pageSize
    });
  };
  const onInternalChange = (current, pageSize) => {
    var _a, _b;
    if (paginationRef.value)
      (_b = (_a = pagination.value).onChange) == null ? void 0 : _b.call(_a, current, pageSize);
    refreshPagination(current, pageSize);
    onChange(current, pageSize || mergedPagination.value.pageSize);
  };
  return [
    computed(() => {
      return paginationRef.value === false ? {} : { ...mergedPagination.value, onChange: onInternalChange };
    }),
    refreshPagination
  ];
}

export { DEFAULT_PAGE_SIZE, usePagination as default, extendsObject, getPaginationParam };
//# sourceMappingURL=usePagination.mjs.map
