'use strict';

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

var vue = require('vue');
var index$1 = require('../pagination/index.js');
var classNames = require('../_util/classNames.js');
var index = require('../_util/vue-types/index.js');
var type = require('../_util/type.js');
var ListItem = require('./ListItem.js');

const transferListBodyProps = {
  prefixCls: String,
  filteredRenderItems: index.default.array.def([]),
  selectedKeys: index.default.array,
  disabled: type.booleanType(),
  showRemove: type.booleanType(),
  pagination: index.default.any,
  onItemSelect: Function,
  onScroll: Function,
  onItemRemove: Function
};
function parsePagination(pagination) {
  if (!pagination)
    return null;
  const defaultPagination = {
    pageSize: 10,
    simple: true,
    showSizeChanger: false,
    showLessItems: false
  };
  if (typeof pagination === "object") {
    return {
      ...defaultPagination,
      ...pagination
    };
  }
  return defaultPagination;
}
const ListBody = /* @__PURE__ */ vue.defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "ListBody",
  inheritAttrs: false,
  props: transferListBodyProps,
  emits: ["itemSelect", "itemRemove", "scroll"],
  setup(props, {
    emit,
    expose
  }) {
    const current = vue.ref(1);
    const handleItemSelect = (item) => {
      const {
        selectedKeys
      } = props;
      const checked = selectedKeys.includes(item.key);
      emit("itemSelect", item.key, !checked);
    };
    const handleItemRemove = (item) => {
      emit("itemRemove", [item.key]);
    };
    const handleScroll = (e) => {
      emit("scroll", e);
    };
    const mergedPagination = vue.computed(() => parsePagination(props.pagination));
    vue.watch([mergedPagination, () => props.filteredRenderItems], () => {
      if (mergedPagination.value) {
        const maxPageCount = Math.ceil(props.filteredRenderItems.length / mergedPagination.value.pageSize);
        current.value = Math.min(current.value, maxPageCount);
      }
    }, {
      immediate: true
    });
    const items = vue.computed(() => {
      const {
        filteredRenderItems
      } = props;
      let displayItems = filteredRenderItems;
      if (mergedPagination.value) {
        displayItems = filteredRenderItems.slice((current.value - 1) * mergedPagination.value.pageSize, current.value * mergedPagination.value.pageSize);
      }
      return displayItems;
    });
    const onPageChange = (cur) => {
      current.value = cur;
    };
    expose({
      items
    });
    return () => {
      const {
        prefixCls,
        filteredRenderItems,
        selectedKeys,
        disabled: globalDisabled,
        showRemove
      } = props;
      let paginationNode = null;
      if (mergedPagination.value) {
        paginationNode = vue.createVNode(index$1.default, {
          "simple": mergedPagination.value.simple,
          "showSizeChanger": mergedPagination.value.showSizeChanger,
          "showLessItems": mergedPagination.value.showLessItems,
          "size": "small",
          "disabled": globalDisabled,
          "class": `${prefixCls}-pagination`,
          "total": filteredRenderItems.length,
          "pageSize": mergedPagination.value.pageSize,
          "current": current.value,
          "onChange": onPageChange
        }, null);
      }
      const itemsList = items.value.map(({
        renderedEl,
        renderedText,
        item
      }) => {
        const {
          disabled
        } = item;
        const checked = selectedKeys.includes(item.key);
        return vue.createVNode(ListItem.default, {
          "disabled": globalDisabled || disabled,
          "key": item.key,
          "item": item,
          "renderedText": renderedText,
          "renderedEl": renderedEl,
          "checked": checked,
          "prefixCls": prefixCls,
          "onClick": handleItemSelect,
          "onRemove": handleItemRemove,
          "showRemove": showRemove
        }, null);
      });
      return vue.createVNode(vue.Fragment, null, [vue.createVNode("ul", {
        "class": classNames.default(`${prefixCls}-content`, {
          [`${prefixCls}-content-show-remove`]: showRemove
        }),
        "onScroll": handleScroll
      }, [itemsList]), paginationNode]);
    };
  }
});

exports.default = ListBody;
exports.transferListBodyProps = transferListBodyProps;
//# sourceMappingURL=ListBody.js.map
