/**
 * ldesign v0.2.14
 * (c) 2022/11/2 16:16 built by swimly
 * @license MIT
 */

import { toRefs, ref, watch } from '@vue/composition-api';
import { useConfig } from '../../config-provider/useConfig.js';
import { Pagination } from '../../pagination/index.js';
import '../../config-provider/context.js';
import 'lodash';
import '../../../chunks/zh_CN.js';
import '../../config-provider/type.js';
import '../../../utils/with-install.js';
import 'vue';
import '../../../config.js';
import '../../../utils/map-props.js';
import '../../pagination/pagination.js';
import '../../../chunks/helper.js';
import '../../../chunks/_commonjsHelpers.js';
import '@ldesign/icons-vue';
import '../../../utils/mixins.js';
import '../../config-provider/config-receiver.js';
import 'lodash/mergeWith';
import '../../input-number/index.js';
import '../../input-number/input-number.js';
import '../../button/index.js';
import '../../button/button.js';
import '../../loading/index.js';
import '../../loading/loading.js';
import '../../../utils/dom.js';
import 'raf';
import 'lodash/isString';
import '../../../chunks/index.js';
import '../../../chunks/log.js';
import '../../../chunks/_commonjsHelpers-2e6815bd.js';
import '../../../utils/easing.js';
import '../../../utils/render-node.js';
import '../../../utils/transfer-dom.js';
import '../../loading/props.js';
import '../../loading/icon/gradient.js';
import '../../../chunks/defineProperty-838e4192.js';
import '../../../chunks/set-style.js';
import '../../../chunks/helper2.js';
import '../../loading/style';
import '../../loading/type.js';
import '../../loading/plugin.js';
import '../../../utils/ripple.js';
import '../../button/props.js';
import '../../button/style';
import '../../button/type.js';
import '../../input/index.js';
import '../../input/input.js';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import '../../../utils/helper.js';
import '../../../utils/event.js';
import '../../input/props.js';
import '../../input/group.js';
import '../../input/style';
import '../../input/type.js';
import '../../../hooks/use-global-icon.js';
import '../../../hooks/use-config.js';
import '../../input-number/props.js';
import '../../input-number/hooks/use-input-number.js';
import '../../../chunks/isObjectLike-c22c7e3b.js';
import '../../../chunks/isArray-cd7357b9.js';
import '../../../chunks/isNumber-c4eb560e.js';
import '../../../hooks/use-common-classname.js';
import '../../../hooks/use-vmodel.js';
import '../../../hooks/use-form-disabled.js';
import '../../input-number/style';
import '../../input-number/type.js';
import '../../select/index.js';
import '../../select/select.js';
import 'lodash/isFunction';
import 'lodash/debounce';
import 'lodash/get';
import 'lodash/cloneDeep';
import 'lodash/isArray';
import 'lodash/isObject';
import '../../../hooks/use-default-value.js';
import '../../../hooks/lnode.js';
import 'lodash/isEmpty';
import '../../../hooks/render-lnode.js';
import '../../popup/index.js';
import '../../popup/popup.js';
import '@popperjs/core';
import '../../popup/props.js';
import '../../popup/container.js';
import '../../popup/style';
import '../../popup/type.js';
import '../../tag/index.js';
import '../../tag/tag.js';
import '../../tag/props.js';
import '../../tag/check-tag.js';
import '../../tag/style';
import '../../tag/type.js';
import '../../select-input/index.js';
import '../../select-input/select-input.js';
import '../../select-input/props.js';
import '../../select-input/hooks/use-single.js';
import 'lodash/pick';
import '../../select-input/hooks/use-multiple.js';
import '../../tag-input/index.js';
import '../../tag-input/tag-input.js';
import '../../tag-input/props.js';
import '../../tag-input/hooks/use-tag-scroll.js';
import '../../tag-input/hooks/use-tag-list.js';
import '../../tag-input/hooks/use-hover.js';
import '../../tag-input/hooks/use-drag-sorter.js';
import '../../tag-input/style';
import '../../tag-input/type.js';
import '../../select-input/hooks/use-overlay-innerstyle.js';
import '../../select-input/style';
import '../../select-input/type.js';
import '../../common/fake-arrow.js';
import '../../select/option.js';
import '../../checkbox/index.js';
import '../../checkbox/group.js';
import 'lodash/intersection';
import '../../checkbox/checkbox.js';
import '../../checkbox/props.js';
import '../../checkbox/style';
import '../../checkbox/type.js';
import '../../../hooks/use-lazy-load.js';
import '../../../chunks/observe.js';
import '../../select/utils.js';
import '../../select/props.js';
import '../../select/select-panel.js';
import '../../../hooks/use-virtual-scroll.js';
import '../../select/hooks/use-select-options.js';
import '../../select/option-group.js';
import '../../select/style';
import '../../select/type.js';
import '../../input-adornment/index.js';
import '../../input-adornment/input-adornment.js';
import '../../input-adornment/props.js';
import '../../input-adornment/style';
import '../../input-adornment/type.js';
import '../../pagination/props.js';
import '../../pagination/style';
import '../../pagination/type.js';

function usePagination(props, context) {
  var _pagination$value, _pagination$value2, _props$data;
  const {
    pagination,
    data,
    disableDataPage
  } = toRefs(props);
  const {
    classPrefix
  } = useConfig();
  const pageSize = ((_pagination$value = pagination.value) === null || _pagination$value === void 0 ? void 0 : _pagination$value.pageSize) || ((_pagination$value2 = pagination.value) === null || _pagination$value2 === void 0 ? void 0 : _pagination$value2.defaultPageSize) || 10;
  const dataSource = ref(((_props$data = props.data) === null || _props$data === void 0 ? void 0 : _props$data.slice(0, pageSize)) || []);
  const isPaginateData = ref(false);
  const updateDataSourceAndPaginate = function () {
    let current = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
    let pageSize2 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 10;
    const {
      data: data2
    } = props;
    const t = Boolean(!disableDataPage.value && data2.length > pageSize2);
    isPaginateData.value = t;
    if (t) {
      const start = (current - 1) * pageSize2;
      const end = current * pageSize2;
      dataSource.value = data2.slice(start, end);
    } else {
      dataSource.value = data2;
    }
  };
  watch(() => {
    var _pagination$value3, _pagination$value4;
    return [(_pagination$value3 = pagination.value) === null || _pagination$value3 === void 0 ? void 0 : _pagination$value3.current, (_pagination$value4 = pagination.value) === null || _pagination$value4 === void 0 ? void 0 : _pagination$value4.pageSize, data.value.length, disableDataPage];
  }, () => {
    if (!pagination.value || !pagination.value.current) return;
    updateDataSourceAndPaginate(pagination.value.current, pagination.value.pageSize);
  }, {
    immediate: true
  });
  watch([data], () => {
    if (!pagination.value || !pagination.value.defaultCurrent) return;
    updateDataSourceAndPaginate(pagination.value.defaultCurrent, pagination.value.defaultPageSize);
  }, {
    immediate: true
  });
  const renderPagination = h => {
    if (!pagination.value) return null;
    return h("div", {
      "class": `${classPrefix.value}-table__pagination`
    }, [h(Pagination, {
      "props": {
        ...pagination.value
      },
      "on": {
        ...{
          change: pageInfo => {
            context.emit("page-change", pageInfo, dataSource.value);
            if (pagination.value && !pagination.value.current && pagination.value.defaultCurrent) updateDataSourceAndPaginate(pageInfo.current, pageInfo.pageSize);
          }
        }
      }
    })]);
  };
  return {
    isPaginateData,
    dataSource,
    renderPagination
  };
}

export { usePagination as default };
//# sourceMappingURL=usePagination.js.map
