/**
 * tdesign v0.46.4
 * (c) 2022 tdesign
 * @license MIT
 */

import _defineProperty from '@babel/runtime/helpers/defineProperty';
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/merge';
import 'lodash/mergeWith';
import '../../_common/js/global-config/default-config.js';
import '../../_common/js/global-config/locale/zh_CN.js';
import '../../config-provider/type.js';
import '../../pagination/pagination.js';
import '@babel/runtime/helpers/typeof';
import 'tdesign-icons-vue';
import '../../utils/mixins.js';
import 'vue';
import '../../config-provider/config-receiver.js';
import '../../input-number/index.js';
import '../../input-number/input-number.js';
import '../../_chunks/dep-a4bd011a.js';
import '../../button/index.js';
import '../../button/button.js';
import '../../loading/index.js';
import '../../loading/loading.js';
import '../../loading/icon/gradient.js';
import '../../_common/js/loading/circle-adapter.js';
import '../../_common/js/utils/set-style.js';
import '../../_common/js/utils/helper.js';
import '@babel/runtime/helpers/toConsumableArray';
import '@babel/runtime/helpers/objectWithoutProperties';
import '@babel/runtime/helpers/slicedToArray';
import '../../utils/dom.js';
import 'raf';
import 'lodash/isString';
import '../../utils/easing.js';
import '../../utils/render-tnode.js';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import '../../utils/transfer-dom.js';
import '../../loading/props.js';
import '../../utils/withInstall.js';
import 'lodash/capitalize';
import './style/index.js';
import '../../loading/type.js';
import '../../loading/plugin.js';
import '../../config.js';
import '../../button/props.js';
import '../../utils/ripple.js';
import '../../button/type.js';
import '../../input/index.js';
import '../../input/input.js';
import '../../utils/helper.js';
import '../../utils/event.js';
import '../../input/props.js';
import '../../input/input-group.js';
import '../../utils/map-props.js';
import '../../input/type.js';
import '../../input-number/props.js';
import '../../input-number/useInputNumber.js';
import '../../hooks/useCommonClassName.js';
import '../../hooks/useVModel.js';
import '../../_common/js/input-number/number.js';
import 'lodash/isNumber';
import '../../_common/js/input-number/large-number.js';
import '../../_common/js/log/log.js';
import '../../_common/js/log/index.js';
import '../../hooks/useFormDisabled.js';
import '../../hooks/useGlobalIcon.js';
import '../../hooks/useConfig.js';
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 '../../hooks/useDefaultValue.js';
import '../../hooks/tnode.js';
import 'lodash/isEmpty';
import '../../hooks/render-tnode.js';
import 'lodash/isObject';
import '../../select/props.js';
import '../../popup/index.js';
import '../../popup/popup.js';
import '@popperjs/core';
import '../../popup/props.js';
import '../../popup/container.js';
import '../../popup/type.js';
import '../../tag/index.js';
import '../../tag/tag.js';
import '../../tag/props.js';
import '../../tag/check-tag.js';
import '../../tag/check-tag-props.js';
import '../../tag/type.js';
import '../../select-input/index.js';
import '../../select-input/select-input.js';
import '../../select-input/props.js';
import '../../select-input/useSingle.js';
import 'lodash/pick';
import '../../select-input/useMultiple.js';
import '../../tag-input/index.js';
import '../../tag-input/tag-input.js';
import '../../tag-input/props.js';
import '../../tag-input/hooks/useTagScroll.js';
import '../../tag-input/useTagList.js';
import '../../tag-input/hooks/useHover.js';
import '../../tag-input/hooks/useDragSorter.js';
import '../../tag-input/type.js';
import '../../select-input/useOverlayInnerStyle.js';
import '../../select-input/type.js';
import '../../common-components/fake-arrow.js';
import '../../select/option.js';
import '../../select/option-props.js';
import '../../checkbox/index.js';
import '../../checkbox/group.js';
import 'lodash/intersection';
import '../../checkbox/checkbox.js';
import '../../checkbox/props.js';
import '../../checkbox/checkbox-group-props.js';
import '../../checkbox/type.js';
import '../../hooks/useLazyLoad.js';
import '../../_common/js/utils/observe.js';
import '../../select/util.js';
import '../../select/select-panel.js';
import '../../hooks/useVirtualScroll.js';
import '../../select/hooks/useSelectOptions.js';
import '../../select/optionGroup.js';
import '../../select/option-group-props.js';
import '../../select/type.js';
import '../../input-adornment/index.js';
import '../../input-adornment/input-adornment.js';
import '../../input-adornment/props.js';
import '../../input-adornment/type.js';
import '../../pagination/props.js';
import '../../pagination/type.js';

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function usePagination(props, context) {
  var _pagination$value, _pagination$value2, _props$data;

  var _toRefs = toRefs(props),
      pagination = _toRefs.pagination,
      data = _toRefs.data,
      disableDataPage = _toRefs.disableDataPage;

  var _useConfig = useConfig(),
      classPrefix = _useConfig.classPrefix;

  var 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;
  var dataSource = ref(((_props$data = props.data) === null || _props$data === void 0 ? void 0 : _props$data.slice(0, pageSize)) || []);
  var isPaginateData = ref(false);

  var updateDataSourceAndPaginate = function updateDataSourceAndPaginate() {
    var current = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
    var pageSize2 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 10;
    var data2 = props.data;
    var t = Boolean(!disableDataPage.value && data2.length > pageSize2);
    isPaginateData.value = t;

    if (t) {
      var start = (current - 1) * pageSize2;
      var end = current * pageSize2;
      dataSource.value = data2.slice(start, end);
    } else {
      dataSource.value = data2;
    }
  };

  watch(function () {
    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];
  }, function () {
    if (!pagination.value || !pagination.value.current) return;
    updateDataSourceAndPaginate(pagination.value.current, pagination.value.pageSize);
  }, {
    immediate: true
  });
  watch([data], function () {
    if (!pagination.value || !pagination.value.defaultCurrent) return;
    updateDataSourceAndPaginate(pagination.value.defaultCurrent, pagination.value.defaultPageSize);
  }, {
    immediate: true
  });

  var renderPagination = function renderPagination(h) {
    if (!pagination.value) return null;
    return h("div", {
      "class": "".concat(classPrefix.value, "-table__pagination")
    }, [h(Pagination, {
      "props": _objectSpread({}, pagination.value),
      "on": _objectSpread({}, {
        change: function change(pageInfo) {
          var _props$onPageChange;

          (_props$onPageChange = props.onPageChange) === null || _props$onPageChange === void 0 ? void 0 : _props$onPageChange.call(props, pageInfo, dataSource.value);
          context.emit("page-change", pageInfo, dataSource.value);

          if (pagination.value && !pagination.value.current && pagination.value.defaultCurrent) {
            updateDataSourceAndPaginate(pageInfo.current, pageInfo.pageSize);
          }
        }
      })
    })]);
  };

  return {
    isPaginateData: isPaginateData,
    dataSource: dataSource,
    renderPagination: renderPagination
  };
}

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