import { defineComponent, ref, computed, watch, createVNode, Fragment } from 'vue';
import '../../../utils/index.mjs';
import { Pagination } from '../../pagination/index.mjs';
import ListItem from './ListItem.mjs';
import { transferListBodyProps } from './props.mjs';
import { classNames } from '../../../utils/dom/class.mjs';

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;
}
var ListBody = /* @__PURE__ */ defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "ListBody",
  inheritAttrs: false,
  props: transferListBodyProps(),
  emits: ["itemSelect", "itemRemove", "scroll"],
  setup(props, {
    emit,
    expose
  }) {
    const current = 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 = computed(() => parsePagination(props.pagination));
    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 = 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 = createVNode(Pagination, {
          "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 createVNode(ListItem, {
          "disabled": globalDisabled || disabled,
          "key": item.key,
          "item": item,
          "renderedText": renderedText,
          "renderedEl": renderedEl,
          "checked": checked,
          "prefixCls": prefixCls,
          "onClick": handleItemSelect,
          "onRemove": handleItemRemove,
          "showRemove": showRemove
        }, null);
      });
      return createVNode(Fragment, null, [createVNode("ul", {
        "class": classNames(`${prefixCls}-content`, {
          [`${prefixCls}-content-show-remove`]: showRemove
        }),
        "onScroll": handleScroll
      }, [itemsList]), paginationNode]);
    };
  }
});

export { ListBody as default };
//# sourceMappingURL=ListBody.mjs.map
