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

'use strict';

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

var VueCompositionAPI = require('@vue/composition-api');
var components_configProvider_useConfig = require('../../config-provider/useConfig.js');
var components_pagination_index = require('../../pagination/index.js');
require('../../config-provider/context.js');
require('lodash');
require('../../../chunks/zh_CN.js');
require('../../../utils/with-install.js');
require('vue');
require('../../../config.js');
require('../../../utils/map-props.js');
require('../../pagination/pagination.js');
require('../../../chunks/helper.js');
require('@ldesign/icons-vue');
require('../../../utils/mixins.js');
require('../../config-provider/config-receiver.js');
require('lodash/mergeWith');
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/loading.js');
require('../../../utils/dom.js');
require('raf');
require('lodash/isString');
require('../../../chunks/log.js');
require('../../../chunks/_commonjsHelpers-2e6815bd.js');
require('../../../utils/easing.js');
require('../../../utils/render-node.js');
require('../../../utils/transfer-dom.js');
require('../../loading/props.js');
require('../../loading/icon/gradient.js');
require('../../../chunks/defineProperty-838e4192.js');
require('../../../chunks/set-style.js');
require('../../../chunks/helper2.js');
require('../../loading/plugin.js');
require('../../../utils/ripple.js');
require('../../button/props.js');
require('../../input/index.js');
require('../../input/input.js');
require('lodash/camelCase');
require('lodash/kebabCase');
require('../../../utils/helper.js');
require('../../../utils/event.js');
require('../../input/props.js');
require('../../input/group.js');
require('../../../hooks/use-global-icon.js');
require('../../input-number/props.js');
require('../../input-number/hooks/use-input-number.js');
require('../../../chunks/isObjectLike-c22c7e3b.js');
require('../../../chunks/isArray-cd7357b9.js');
require('../../../chunks/isNumber-c4eb560e.js');
require('../../../hooks/use-common-classname.js');
require('../../../hooks/use-vmodel.js');
require('../../../hooks/use-form-disabled.js');
require('../../select/index.js');
require('../../select/select.js');
require('lodash/isFunction');
require('lodash/debounce');
require('lodash/get');
require('lodash/cloneDeep');
require('lodash/isArray');
require('lodash/isObject');
require('../../../hooks/use-default-value.js');
require('../../../hooks/lnode.js');
require('lodash/isEmpty');
require('../../../hooks/render-lnode.js');
require('../../popup/index.js');
require('../../popup/popup.js');
require('@popperjs/core');
require('../../popup/props.js');
require('../../popup/container.js');
require('../../tag/index.js');
require('../../tag/tag.js');
require('../../tag/props.js');
require('../../tag/check-tag.js');
require('../../select-input/index.js');
require('../../select-input/select-input.js');
require('../../select-input/props.js');
require('../../select-input/hooks/use-single.js');
require('lodash/pick');
require('../../../hooks/use-config.js');
require('../../select-input/hooks/use-multiple.js');
require('../../tag-input/index.js');
require('../../tag-input/tag-input.js');
require('../../tag-input/props.js');
require('../../tag-input/hooks/use-tag-scroll.js');
require('../../tag-input/hooks/use-tag-list.js');
require('../../tag-input/hooks/use-hover.js');
require('../../tag-input/hooks/use-drag-sorter.js');
require('../../select-input/hooks/use-overlay-innerstyle.js');
require('../../common/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('../../../hooks/use-lazy-load.js');
require('../../../chunks/observe.js');
require('../../select/utils.js');
require('../../select/props.js');
require('../../select/select-panel.js');
require('../../../hooks/use-virtual-scroll.js');
require('../../select/hooks/use-select-options.js');
require('../../select/option-group.js');
require('../../input-adornment/index.js');
require('../../input-adornment/input-adornment.js');
require('../../input-adornment/props.js');
require('../../pagination/props.js');

function usePagination(props, context) {
  var _pagination$value, _pagination$value2, _props$data;
  const {
    pagination,
    data,
    disableDataPage
  } = VueCompositionAPI.toRefs(props);
  const {
    classPrefix
  } = components_configProvider_useConfig.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 = VueCompositionAPI.ref(((_props$data = props.data) === null || _props$data === void 0 ? void 0 : _props$data.slice(0, pageSize)) || []);
  const isPaginateData = VueCompositionAPI.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;
    }
  };
  VueCompositionAPI.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
  });
  VueCompositionAPI.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(components_pagination_index.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
  };
}

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