/**
 * ldesign v0.4.6
 * (c) 2023 ldesign
 * @license MIT
 */

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var VueCompositionAPI = require('@vue/composition-api');
var configProvider_useConfig = require('../../config-provider/useConfig.js');
var pagination_index = require('../../pagination/index.js');
require('../../config-provider/context.js');
require('lodash/merge');
require('lodash/mergeWith');
require('../../_common/js/global-config/default-config.js');
require('../../_common/js/global-config/locale/zh_CN.js');
require('@babel/runtime/helpers/typeof');
require('../../_chunks/dep-7c884501.js');
require('dayjs');
require('../../utils/withInstall.js');
require('vue');
require('lodash/capitalize');
require('../../utils/map-props.js');
require('@babel/runtime/helpers/objectWithoutProperties');
require('../../pagination/pagination.js');
require('../../_chunks/dep-c3e1ffac.js');
require('@ldesign/icons-vue');
require('../../utils/mixins.js');
require('../../config-provider/config-receiver.js');
require('../../input-number/index.js');
require('../../input-number/input-number.js');
require('../../button/index.js');
require('../../button/button.js');
require('../../loading/index.js');
require('../../loading/directive.js');
require('lodash/isObject');
require('lodash/mapKeys');
require('lodash/isEqual');
require('../../loading/plugin.js');
require('../../config.js');
require('../../utils/dom.js');
require('raf');
require('lodash/isString');
require('../../utils/easing.js');
require('../../loading/loading.js');
require('../../utils/render-tnode.js');
require('lodash/camelCase');
require('lodash/kebabCase');
require('../../utils/transfer-dom.js');
require('../../loading/props.js');
require('../../loading/icon/gradient.js');
require('../../_common/js/loading/circle-adapter.js');
require('../../_common/js/utils/set-style.js');
require('../../_common/js/utils/helper.js');
require('@babel/runtime/helpers/toConsumableArray');
require('@babel/runtime/helpers/slicedToArray');
require('lodash/isNull');
require('lodash/isUndefined');
require('lodash/isNumber');
require('lodash/isArray');
require('../../utils/ripple.js');
require('../../button/props.js');
require('../../input/index.js');
require('../../input/input.js');
require('../../utils/helper.js');
require('../../utils/event.js');
require('../../_common/js/log/log.js');
require('../../input/props.js');
require('../../input/input-group.js');
require('../../hooks/useGlobalIcon.js');
require('../../input-number/props.js');
require('../../input-number/useInputNumber.js');
require('../../hooks/useCommonClassName.js');
require('../../hooks/useVModel.js');
require('../../_common/js/input-number/number.js');
require('lodash/isNil');
require('../../_common/js/input-number/large-number.js');
require('../../hooks/useFormDisabled.js');
require('../../select/index.js');
require('../../select/select.js');
require('lodash/isFunction');
require('lodash/debounce');
require('lodash/get');
require('lodash/cloneDeep');
require('../../hooks/useDefaultValue.js');
require('../../hooks/tnode.js');
require('lodash/isEmpty');
require('../../hooks/render-tnode.js');
require('../../popup/index.js');
require('../../popup/popup.js');
require('@popperjs/core');
require('../../popup/props.js');
require('../../popup/container.js');
require('../../popup/utils.js');
require('../../popup/trigger.js');
require('../../popup/plugin.js');
require('../../tag/index.js');
require('../../tag/tag.js');
require('../../tag/props.js');
require('../../tag/check-tag.js');
require('../../tag/check-tag-props.js');
require('../../select-input/index.js');
require('../../select-input/select-input.js');
require('../../select-input/props.js');
require('../../select-input/useSingle.js');
require('lodash/pick');
require('../../hooks/useConfig.js');
require('../../select-input/useMultiple.js');
require('../../tag-input/index.js');
require('../../tag-input/tag-input.js');
require('../../tag-input/props.js');
require('../../tag-input/hooks/useTagScroll.js');
require('../../tag-input/useTagList.js');
require('../../tag-input/hooks/useHover.js');
require('../../tag-input/hooks/useDragSorter.js');
require('../../select-input/useOverlayInnerStyle.js');
require('../../common-components/fake-arrow.js');
require('../../select/option.js');
require('../../checkbox/index.js');
require('../../checkbox/group.js');
require('lodash/intersection');
require('../../checkbox/checkbox.js');
require('../../checkbox/props.js');
require('../../checkbox/checkbox-group-props.js');
require('../../hooks/useLazyLoad.js');
require('../../_common/js/utils/observe.js');
require('../../select/util.js');
require('../../select/option-props.js');
require('../../select/select-panel.js');
require('../../hooks/useVirtualScroll.js');
require('../../select/hooks/useSelectOptions.js');
require('../../select/props.js');
require('../../select/optionGroup.js');
require('../../select/option-group-props.js');
require('../../input-adornment/index.js');
require('../../input-adornment/input-adornment.js');
require('../../input-adornment/props.js');
require('../../pagination/props.js');
require('../../pagination/pagination-mini.js');
require('../../pagination/pagination-mini-props.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);

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__default["default"](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 = VueCompositionAPI.toRefs(props),
    pagination = _toRefs.pagination,
    data = _toRefs.data,
    disableDataPage = _toRefs.disableDataPage;
  var _useConfig = configProvider_useConfig.useConfig(),
    classPrefix = _useConfig.classPrefix;
  var innerPagination = VueCompositionAPI.ref(props.pagination);
  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 = VueCompositionAPI.ref(((_props$data = props.data) === null || _props$data === void 0 ? void 0 : _props$data.slice(0, pageSize)) || []);
  var isPaginateData = VueCompositionAPI.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;
    }
    return dataSource.value;
  };
  VueCompositionAPI.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
  });
  VueCompositionAPI.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_index.Pagination, {
      "props": _objectSpread({}, pagination.value),
      "on": _objectSpread({}, {
        change: function change(pageInfo) {
          var _props$onPageChange;
          innerPagination.value = pageInfo;
          updateDataSourceAndPaginate(pageInfo.current, pageInfo.pageSize);
          context.emit("page-change", pageInfo, dataSource.value);
          (_props$onPageChange = props.onPageChange) === null || _props$onPageChange === void 0 ? void 0 : _props$onPageChange.call(props, pageInfo, dataSource.value);
        }
      }),
      "scopedSlots": {
        totalContent: context.slots.totalContent
      }
    })]);
  };
  return {
    isPaginateData: isPaginateData,
    dataSource: dataSource,
    innerPagination: innerPagination,
    renderPagination: renderPagination
  };
}

exports["default"] = usePagination;
//# sourceMappingURL=usePagination.js.map
