'use strict';

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

var vue = require('vue');
require('../../../utils/index.cjs');
require('../../../locale/index.cjs');
require('../../../hooks/index.cjs');
require('../../../vue-components/index.cjs');
var Table = require('../../../vue-components/vc-table/src/Table.cjs');
var index$3 = require('../../spin/index.cjs');
var index$2 = require('../../pagination/index.cjs');
var useConfigInject = require('../../config-provider/src/hooks/useConfigInject.cjs');
var useLocaleReceiver = require('../../locale-provider/src/useLocaleReceiver.cjs');
var index$1 = require('../style/index.cjs');
var usePagination = require('./hooks/usePagination.cjs');
var useLazyKVMap = require('./hooks/useLazyKVMap.cjs');
var useSelection = require('./hooks/useSelection.cjs');
var useSorter = require('./hooks/useSorter.cjs');
var useFilter = require('./hooks/useFilter.cjs');
var useTitleColumns = require('./hooks/useTitleColumns.cjs');
var ExpandIcon = require('./ExpandIcon.cjs');
var context = require('./context.cjs');
var useColumns = require('./hooks/useColumns.cjs');
var props = require('./props.cjs');
var initDefaultProps = require('../../../utils/vue/initDefaultProps.cjs');
var propType = require('../../../utils/vue/propType.cjs');
var log = require('../../../utils/log.cjs');
var index = require('../../../hooks/use-breakpoint/index.cjs');
var en_US = require('../../../locale/lang/en_US.cjs');
var scrollTo = require('../../../utils/scroll/scrollTo.cjs');
var _class = require('../../../utils/dom/class.cjs');
var omit = require('../../../utils/lang/omit.cjs');

const EMPTY_LIST = [];
var InternalTable = /* @__PURE__ */ vue.defineComponent({
  name: "InternalTable",
  inheritAttrs: false,
  props: initDefaultProps.initDefaultProps({
    ...props.tableProps(),
    contextSlots: propType.objectType()
  }, {
    rowKey: "key"
  }),
  setup(props, {
    attrs,
    slots,
    expose,
    emit
  }) {
    log.devWarning(!(typeof props.rowKey === "function" && props.rowKey.length > 1), "Table", "`index` parameter of `rowKey` function is deprecated. There is no guarantee that it will work as expected.");
    context.useProvideSlots(vue.computed(() => props.contextSlots));
    context.useProvideTableContext({
      onResizeColumn: (w, col) => {
        emit("resizeColumn", w, col);
      }
    });
    const screens = index.useBreakpoint();
    const mergedColumns = vue.computed(() => {
      const matched = new Set(Object.keys(screens.value).filter((m) => screens.value[m]));
      return props.columns.filter((c) => !c.responsive || c.responsive.some((r) => matched.has(r)));
    });
    const {
      size: mergedSize,
      renderEmpty,
      direction,
      prefixCls,
      configProvider
    } = useConfigInject.default("table", props);
    const [wrapSSR, hashId] = index$1.default(prefixCls);
    const transformCellText = vue.computed(() => {
      var _a;
      return props.transformCellText || ((_a = configProvider.transformCellText) == null ? void 0 : _a.value);
    });
    const [tableLocale] = useLocaleReceiver.useLocaleReceiver("Table", en_US.default.Table, vue.toRef(props, "locale"));
    const rawData = vue.computed(() => props.dataSource || EMPTY_LIST);
    const dropdownPrefixCls = vue.computed(() => configProvider.getPrefixCls("dropdown", props.dropdownPrefixCls));
    const childrenColumnName = vue.computed(() => props.childrenColumnName || "children");
    const expandType = vue.computed(() => {
      if (rawData.value.some((item) => item == null ? void 0 : item[childrenColumnName.value]))
        return "nest";
      if (props.expandedRowRender)
        return "row";
      return null;
    });
    const internalRefs = vue.reactive({
      body: null
    });
    const updateInternalRefs = (refs) => {
      Object.assign(internalRefs, refs);
    };
    const getRowKey = vue.computed(() => {
      if (typeof props.rowKey === "function")
        return props.rowKey;
      return (record) => record == null ? void 0 : record[props.rowKey];
    });
    const [getRecordByKey] = useLazyKVMap.default(rawData, childrenColumnName, getRowKey);
    const changeEventInfo = {};
    const triggerOnChange = (info, action, reset = false) => {
      const {
        pagination,
        scroll,
        onChange
      } = props;
      const changeInfo = {
        ...changeEventInfo,
        ...info
      };
      if (reset) {
        changeEventInfo.resetPagination();
        if (changeInfo.pagination.current)
          changeInfo.pagination.current = 1;
        if (pagination && pagination.onChange)
          pagination.onChange(1, changeInfo.pagination.pageSize);
      }
      if (scroll && scroll.scrollToFirstRowOnChange !== false && internalRefs.body) {
        scrollTo.scrollTo(0, {
          getContainer: () => internalRefs.body
        });
      }
      onChange == null ? void 0 : onChange(changeInfo.pagination, changeInfo.filters, changeInfo.sorter, {
        currentDataSource: useFilter.getFilterData(useSorter.getSortData(rawData.value, changeInfo.sorterStates, childrenColumnName.value), changeInfo.filterStates),
        action
      });
    };
    const onSorterChange = (sorter, sorterStates) => {
      triggerOnChange({
        sorter,
        sorterStates
      }, "sort", false);
    };
    const [transformSorterColumns, sortStates, sorterTitleProps, sorters] = useSorter.default({
      prefixCls,
      mergedColumns,
      onSorterChange,
      sortDirections: vue.computed(() => props.sortDirections || ["ascend", "descend"]),
      tableLocale,
      showSorterTooltip: vue.toRef(props, "showSorterTooltip")
    });
    const sortedData = vue.computed(() => useSorter.getSortData(rawData.value, sortStates.value, childrenColumnName.value));
    const onFilterChange = (filters2, filterStates2) => {
      triggerOnChange({
        filters: filters2,
        filterStates: filterStates2
      }, "filter", true);
    };
    const [transformFilterColumns, filterStates, filters] = useFilter.default({
      prefixCls,
      locale: tableLocale,
      dropdownPrefixCls,
      mergedColumns,
      onFilterChange,
      getPopupContainer: vue.toRef(props, "getPopupContainer")
    });
    const mergedData = vue.computed(() => useFilter.getFilterData(sortedData.value, filterStates.value));
    const [transformBasicColumns] = useColumns.default(vue.toRef(props, "contextSlots"));
    const columnTitleProps = vue.computed(() => {
      const mergedFilters = {};
      const filtersValue = filters.value;
      Object.keys(filtersValue).forEach((filterKey) => {
        if (filtersValue[filterKey] !== null)
          mergedFilters[filterKey] = filtersValue[filterKey];
      });
      return {
        ...sorterTitleProps.value,
        filters: mergedFilters
      };
    });
    const [transformTitleColumns] = useTitleColumns.default(columnTitleProps);
    const onPaginationChange = (current, pageSize) => {
      triggerOnChange({
        pagination: {
          ...changeEventInfo.pagination,
          current,
          pageSize
        }
      }, "paginate");
    };
    const [mergedPagination, resetPagination] = usePagination.default(vue.computed(() => mergedData.value.length), vue.toRef(props, "pagination"), onPaginationChange);
    vue.watchEffect(() => {
      changeEventInfo.sorter = sorters.value;
      changeEventInfo.sorterStates = sortStates.value;
      changeEventInfo.filters = filters.value;
      changeEventInfo.filterStates = filterStates.value;
      changeEventInfo.pagination = props.pagination === false ? {} : usePagination.getPaginationParam(mergedPagination.value, props.pagination);
      changeEventInfo.resetPagination = resetPagination;
    });
    const pageData = vue.computed(() => {
      if (props.pagination === false || !mergedPagination.value.pageSize)
        return mergedData.value;
      const {
        current = 1,
        total,
        pageSize = usePagination.DEFAULT_PAGE_SIZE
      } = mergedPagination.value;
      log.devWarning(current > 0, "Table", "`current` should be positive number.");
      if (mergedData.value.length < total) {
        if (mergedData.value.length > pageSize)
          return mergedData.value.slice((current - 1) * pageSize, current * pageSize);
        return mergedData.value;
      }
      return mergedData.value.slice((current - 1) * pageSize, current * pageSize);
    });
    vue.watchEffect(() => {
      vue.nextTick(() => {
        const {
          total,
          pageSize = usePagination.DEFAULT_PAGE_SIZE
        } = mergedPagination.value;
        if (mergedData.value.length < total) {
          if (mergedData.value.length > pageSize) {
            log.devWarning(false, "Table", "`dataSource` length is less than `pagination.total` but large than `pagination.pageSize`. Please make sure your config correct data with async mode.");
          }
        }
      });
    }, {
      flush: "post"
    });
    const expandIconColumnIndex = vue.computed(() => {
      if (props.showExpandColumn === false)
        return -1;
      if (expandType.value === "nest" && props.expandIconColumnIndex === void 0)
        return props.rowSelection ? 1 : 0;
      else if (props.expandIconColumnIndex > 0 && props.rowSelection)
        return props.expandIconColumnIndex - 1;
      return props.expandIconColumnIndex;
    });
    const rowSelection = vue.ref();
    vue.watch(() => props.rowSelection, () => {
      rowSelection.value = props.rowSelection ? {
        ...props.rowSelection
      } : props.rowSelection;
    }, {
      deep: true,
      immediate: true
    });
    const [transformSelectionColumns, selectedKeySet] = useSelection.default(rowSelection, {
      prefixCls,
      data: mergedData,
      pageData,
      getRowKey,
      getRecordByKey,
      expandType,
      childrenColumnName,
      locale: tableLocale,
      getPopupContainer: vue.computed(() => props.getPopupContainer)
    });
    const internalRowClassName = (record, index, indent) => {
      let mergedRowClassName;
      const {
        rowClassName
      } = props;
      if (typeof rowClassName === "function")
        mergedRowClassName = _class.classNames(rowClassName(record, index, indent));
      else
        mergedRowClassName = _class.classNames(rowClassName);
      return _class.classNames({
        [`${prefixCls.value}-row-selected`]: selectedKeySet.value.has(getRowKey.value(record, index))
      }, mergedRowClassName);
    };
    expose({
      selectedKeySet
    });
    const indentSize = vue.computed(() => {
      return typeof props.indentSize === "number" ? props.indentSize : 15;
    });
    const transformColumns = (innerColumns) => {
      const res = transformTitleColumns(transformSelectionColumns(transformFilterColumns(transformSorterColumns(transformBasicColumns(innerColumns)))));
      return res;
    };
    return () => {
      var _a;
      const {
        expandIcon = slots.expandIcon || ExpandIcon.default(tableLocale.value),
        pagination,
        loading,
        bordered
      } = props;
      let topPaginationNode;
      let bottomPaginationNode;
      if (pagination !== false && ((_a = mergedPagination.value) == null ? void 0 : _a.total)) {
        let paginationSize;
        if (mergedPagination.value.size) {
          paginationSize = mergedPagination.value.size;
        } else {
          paginationSize = mergedSize.value === "small" || mergedSize.value === "middle" ? "small" : void 0;
        }
        const renderPagination = (position2) => vue.createVNode(index$2.Pagination, {
          ...mergedPagination.value,
          "class": [`${prefixCls.value}-pagination ${prefixCls.value}-pagination-${position2}`, mergedPagination.value.class],
          "size": paginationSize
        }, null);
        const defaultPosition = direction.value === "rtl" ? "left" : "right";
        const {
          position
        } = mergedPagination.value;
        if (position !== null && Array.isArray(position)) {
          const topPos = position.find((p) => p.includes("top"));
          const bottomPos = position.find((p) => p.includes("bottom"));
          const isDisable = position.every((p) => `${p}` === "none");
          if (!topPos && !bottomPos && !isDisable)
            bottomPaginationNode = renderPagination(defaultPosition);
          if (topPos)
            topPaginationNode = renderPagination(topPos.toLowerCase().replace("top", ""));
          if (bottomPos)
            bottomPaginationNode = renderPagination(bottomPos.toLowerCase().replace("bottom", ""));
        } else {
          bottomPaginationNode = renderPagination(defaultPosition);
        }
      }
      let spinProps;
      if (typeof loading === "boolean") {
        spinProps = {
          spinning: loading
        };
      } else if (typeof loading === "object") {
        spinProps = {
          spinning: true,
          ...loading
        };
      }
      const wrapperClassNames = _class.classNames(`${prefixCls.value}-wrapper`, {
        [`${prefixCls.value}-wrapper-rtl`]: direction.value === "rtl"
      }, attrs.class, hashId.value);
      const getTableProps = omit.omit(props, ["columns"]);
      return wrapSSR(vue.createVNode("div", {
        "class": wrapperClassNames,
        "style": attrs.style
      }, [vue.createVNode(index$3.Spin, {
        "spinning": false,
        ...spinProps
      }, {
        default: () => [topPaginationNode, vue.createVNode(Table.default, {
          ...attrs,
          ...getTableProps,
          "expandedRowKeys": props.expandedRowKeys,
          "defaultExpandedRowKeys": props.defaultExpandedRowKeys,
          "expandIconColumnIndex": expandIconColumnIndex.value,
          "indentSize": indentSize.value,
          "expandIcon": expandIcon,
          "columns": mergedColumns.value,
          "direction": direction.value,
          "prefixCls": prefixCls.value,
          "class": _class.classNames({
            [`${prefixCls.value}-middle`]: mergedSize.value === "middle",
            [`${prefixCls.value}-small`]: mergedSize.value === "small",
            [`${prefixCls.value}-bordered`]: bordered,
            [`${prefixCls.value}-empty`]: rawData.value.length === 0
          }),
          "data": pageData.value,
          "rowKey": getRowKey.value,
          "rowClassName": internalRowClassName,
          "internalHooks": Table.INTERNAL_HOOKS,
          "internalRefs": internalRefs,
          "onUpdateInternalRefs": updateInternalRefs,
          "transformColumns": transformColumns,
          "transformCellText": transformCellText.value
        }, {
          ...slots,
          emptyText: () => {
            var _a2, _b;
            return ((_a2 = slots.emptyText) == null ? void 0 : _a2.call(slots)) || ((_b = props.locale) == null ? void 0 : _b.emptyText) || renderEmpty("Table");
          }
        }), bottomPaginationNode]
      })]));
    };
  }
});

exports.default = InternalTable;
//# sourceMappingURL=InternalTable.cjs.map
