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

import { h as helper } from '../../../chunks/helper.js';
import { defineComponent, toRefs, ref, computed, onMounted, watch, h } from '@vue/composition-api';
import get from 'lodash/get';
import omit from 'lodash/omit';
import { useLNodeJSX } from '../../../hooks/lnode.js';
import { BaseTableProps, PrimaryTableProps } from '../props.js';
import useColumnController from '../hooks/useColumnController.js';
import useRowExpand from '../hooks/useRowExpand.js';
import useTableHeader, { renderTitle } from '../hooks/useTableHeader.js';
import useRowSelect from '../hooks/useRowSelect.js';
import useSorter from '../hooks/useSorter.js';
import useFilter from '../hooks/useFilter.js';
import useDragSort from '../hooks/useDragSort.js';
import useAsyncLoading from '../hooks/useAsyncLoading.js';
import useClassName from '../hooks/useClassName.js';
import useEditableCell from '../hooks/useEditableCell.js';
import useRowEdit from '../hooks/useEditableRow.js';
import useStyle from '../hooks/useStyle.js';
import _BaseTable, { BASE_TABLE_ALL_EVENTS } from './base-table.js';
export { BASE_TABLE_ALL_EVENTS } from './base-table.js';
import '../../../chunks/_commonjsHelpers.js';
import 'lodash/isEmpty';
import 'lodash/isFunction';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import '../../../hooks/render-lnode.js';
import 'lodash';
import '@ldesign/icons-vue';
import 'lodash/intersection';
import '../../checkbox/index.js';
import '../../../utils/map-props.js';
import 'vue';
import '../../../utils/with-install.js';
import '../../../config.js';
import '../../checkbox/group.js';
import '../../../chunks/index.js';
import '../../../chunks/log.js';
import '../../../chunks/_commonjsHelpers-2e6815bd.js';
import '../../../utils/event.js';
import '../../../utils/helper.js';
import '../../config-provider/config-receiver.js';
import 'lodash/mergeWith';
import '../../config-provider/context.js';
import '../../../chunks/zh_CN.js';
import '../../config-provider/type.js';
import '../../../utils/mixins.js';
import '../../checkbox/checkbox.js';
import '../../../utils/render-node.js';
import '../../checkbox/props.js';
import '../../checkbox/style/css.js';
import '../../checkbox/type.js';
import '../../dialog/plugin.js';
import '../../../utils/dom.js';
import 'raf';
import 'lodash/isString';
import '../../../utils/easing.js';
import '../../dialog/dialog.js';
import 'lodash/isNumber';
import 'lodash/throttle';
import '../../button/index.js';
import '../../button/button.js';
import '../../loading/index.js';
import '../../loading/loading.js';
import '../../../utils/transfer-dom.js';
import '../../loading/props.js';
import '../../loading/icon/gradient.js';
import '../../../chunks/defineProperty-838e4192.js';
import '../../../chunks/set-style.js';
import '../../../chunks/helper2.js';
import '../../loading/style/css.js';
import '../../loading/type.js';
import '../../loading/plugin.js';
import '../../../utils/ripple.js';
import '../../button/props.js';
import '../../button/style/css.js';
import '../../button/type.js';
import '../../../hooks/use-destroy-onclose.js';
import '../../dialog/props.js';
import '../../dialog/mixins/action.js';
import 'lodash/isObject';
import '../../dialog/stack.js';
import '../../config-provider/useConfig.js';
import '../../../hooks/use-default-value.js';
import '../../../hooks/use-global-icon.js';
import '../../../hooks/use-config.js';
import '../utils.js';
import './ellipsis.js';
import 'lodash/debounce';
import '../../tooltip/index.js';
import '../../tooltip/tooltip.js';
import '../../popup/props.js';
import '../../popup/popup.js';
import '@popperjs/core';
import '../../popup/container.js';
import '../../tooltip/props.js';
import '../../tooltip/style/css.js';
import '../../tooltip/type.js';
import '../hooks/useMultiHeader.js';
import '../../radio/index.js';
import '../../radio/radio.js';
import '../../radio/props.js';
import '../../radio/group.js';
import '../../radio/radio-button.js';
import '../../radio/style/css.js';
import '../../radio/type.js';
import './sorter-button.js';
import './filter-controller.js';
import 'lodash/lowerFirst';
import '../../popup/index.js';
import '../../popup/style/css.js';
import '../../popup/type.js';
import '../../input/index.js';
import '../../input/input.js';
import '../../input/props.js';
import '../../input/group.js';
import '../../input/style/css.js';
import '../../input/type.js';
import 'sortablejs';
import './editable-cell.js';
import 'lodash/set';
import '../../form/model.js';
import './tr.js';
import 'lodash/upperFirst';
import 'lodash/pick';
import '../../../hooks/use-lazy-load.js';
import '../../../chunks/observe.js';
import '../hooks/useFixed.js';
import '../hooks/useRowspanAndColspan.js';
import '../../../hooks/use-common-classname.js';
import '../../../hooks/use-virtual-scroll.js';
import '../../affix/index.js';
import '../../affix/affix.js';
import '../../affix/props.js';
import '../../affix/style/css.js';
import '../../affix/type.js';
import '../hooks/useColumnResize.js';
import '../../../chunks/utils3.js';
import '../../../chunks/isFunction-91acade7.js';
import '../../../chunks/isObjectLike-c22c7e3b.js';
import '../../../chunks/isNumber-c4eb560e.js';
import '../hooks/usePagination.js';
import '../../pagination/index.js';
import '../../pagination/pagination.js';
import '../../input-number/index.js';
import '../../input-number/input-number.js';
import '../../input-number/props.js';
import '../../input-number/hooks/use-input-number.js';
import '../../../chunks/isArray-cd7357b9.js';
import '../../../hooks/use-vmodel.js';
import '../../../hooks/use-form-disabled.js';
import '../../input-number/style/css.js';
import '../../input-number/type.js';
import '../../select/index.js';
import '../../select/select.js';
import 'lodash/cloneDeep';
import 'lodash/isArray';
import '../../tag/index.js';
import '../../tag/tag.js';
import '../../tag/props.js';
import '../../tag/check-tag.js';
import '../../tag/style/css.js';
import '../../tag/type.js';
import '../../select-input/index.js';
import '../../select-input/select-input.js';
import '../../select-input/props.js';
import '../../select-input/hooks/use-single.js';
import '../../select-input/hooks/use-multiple.js';
import '../../tag-input/index.js';
import '../../tag-input/tag-input.js';
import '../../tag-input/props.js';
import '../../tag-input/hooks/use-tag-scroll.js';
import '../../tag-input/hooks/use-tag-list.js';
import '../../tag-input/hooks/use-hover.js';
import '../../tag-input/hooks/use-drag-sorter.js';
import '../../tag-input/style/css.js';
import '../../tag-input/type.js';
import '../../select-input/hooks/use-overlay-innerstyle.js';
import '../../select-input/style/css.js';
import '../../select-input/type.js';
import '../../common/fake-arrow.js';
import '../../select/option.js';
import '../../select/utils.js';
import '../../select/props.js';
import '../../select/select-panel.js';
import '../../select/hooks/use-select-options.js';
import '../../select/option-group.js';
import '../../select/style/css.js';
import '../../select/type.js';
import '../../input-adornment/index.js';
import '../../input-adornment/input-adornment.js';
import '../../input-adornment/props.js';
import '../../input-adornment/style/css.js';
import '../../input-adornment/type.js';
import '../../pagination/props.js';
import '../../pagination/style/css.js';
import '../../pagination/type.js';
import '../hooks/useAffix.js';
import './tbody.js';
import './thead.js';
import './tfoot.js';

const OMIT_PROPS = ["hideSortTips", "dragSort", "defaultExpandedRowKeys", "defaultSelectedRowKeys", "columnController", "filterRow", "sortOnRowDraggable", "expandOnRowClick", "multipleSort", "expandIcon", "onChange", "onAsyncLoadingClick", "onChange", "onColumnChange", "onColumnControllerVisibleChange", "onDataChange", "onDisplayColumnsChange", "onDragSort", "onExpandChange", "onFilterChange", "onSelectChange", "onSortChange"];
var _PrimaryTable = defineComponent({
  name: "TPrimaryTable",
  props: {
    ...BaseTableProps,
    ...PrimaryTableProps
  },
  setup(props, context) {
    const renderLNode = useLNodeJSX();
    const {
      columns
    } = toRefs(props);
    const primaryTableRef = ref(null);
    const {
      classPrefix,
      tableDraggableClasses,
      tableBaseClass,
      tableSelectedClasses,
      tableSortClasses
    } = useClassName();
    const {
      sizeClassNames
    } = useStyle(props);
    const {
      tDisplayColumns,
      renderColumnController
    } = useColumnController(props, context);
    const {
      showExpandedRow,
      showExpandIconColumn,
      getExpandColumn,
      renderExpandedRow,
      onInnerExpandRowClick
    } = useRowExpand(props, context);
    const {
      renderSortIcon
    } = useSorter(props, context);
    const {
      formatToRowSelectColumn,
      selectedRowClassNames
    } = useRowSelect(props, tableSelectedClasses);
    const {
      hasEmptyCondition,
      isTableOverflowHidden,
      renderFilterIcon,
      renderFirstFilterRow,
      setFilterPrimaryTableRef
    } = useFilter(props, context);
    const {
      isRowHandlerDraggable,
      isRowDraggable,
      isColDraggable,
      setDragSortPrimaryTableRef,
      setDragSortColumns
    } = useDragSort(props, context);
    const {
      renderTitleWidthIcon
    } = useTableHeader(props);
    const {
      renderAsyncLoading
    } = useAsyncLoading(props, context);
    const {
      renderEditableCell
    } = useEditableCell(props, context);
    const {
      errorListMap,
      editableKeysMap,
      validateRowData,
      validateTableData,
      onRuleChange,
      clearValidateData,
      onPrimaryTableRowValidate,
      onPrimaryTableRowEdit
    } = useRowEdit(props, context);
    const primaryTableClasses = computed(() => ({
      [tableDraggableClasses.colDraggable]: isColDraggable.value,
      [tableDraggableClasses.rowHandlerDraggable]: isRowHandlerDraggable.value,
      [tableDraggableClasses.rowDraggable]: isRowDraggable.value,
      [tableBaseClass.overflowVisible]: isTableOverflowHidden.value === false,
      [tableBaseClass.tableRowEdit]: props.editableRowKeys
    }));
    const tRowClassNames = computed(() => {
      const tClassNames = [props.rowClassName, selectedRowClassNames.value];
      return tClassNames.filter(v => v);
    });
    const tRowAttributes = computed(() => {
      const tAttributes = [props.rowAttributes];
      if (isRowHandlerDraggable.value || isRowDraggable.value) tAttributes.push(_ref => {
        let {
          row
        } = _ref;
        return {
          "data-id": get(row, props.rowKey || "id")
        };
      });
      return tAttributes.filter(v => v);
    });
    onMounted(() => {
      setFilterPrimaryTableRef(primaryTableRef.value);
      setDragSortPrimaryTableRef(primaryTableRef.value);
    });
    watch(primaryTableRef, () => {
      setFilterPrimaryTableRef(primaryTableRef.value);
      setDragSortPrimaryTableRef(primaryTableRef.value);
    });
    const getColumns = columns2 => {
      const arr = [];
      for (let i = 0, len = columns2.length; i < len; i++) {
        var _item$children, _tDisplayColumns$valu, _item$edit, _item$children2, _item$children3;
        let item = {
          ...columns2[i]
        };
        const isDisplayColumn = ((_item$children = item.children) === null || _item$children === void 0 ? void 0 : _item$children.length) || ((_tDisplayColumns$valu = tDisplayColumns.value) === null || _tDisplayColumns$valu === void 0 ? void 0 : _tDisplayColumns$valu.includes(item.colKey));
        if (!isDisplayColumn && props.columnController) continue;
        item = formatToRowSelectColumn(item);
        const {
          sort
        } = props;
        if (item.sorter && props.showSortColumnBgColor) {
          const sorts = Array.isArray(sort) ? sort : [sort];
          const sortedColumn = sorts.find(sort2 => sort2 && sort2.sortBy === item.colKey && sort2.descending !== void 0);
          if (sortedColumn) {
            item.className = Array.isArray(item.className) ? item.className.concat(tableSortClasses.sortColumn) : [item.className, tableSortClasses.sortColumn];
          }
        }
        if (item.sorter || item.filter) {
          const titleContent = renderTitle(h, context.slots, item, i);
          const {
            ellipsisTitle
          } = item;
          item.title = (h2, p) => {
            var _primaryTableRef$valu, _primaryTableRef$valu2;
            const sortIcon = item.sorter ? renderSortIcon(h2, p) : null;
            const filterIcon = item.filter ? renderFilterIcon(h2, p) : null;
            const attach = (_primaryTableRef$valu = primaryTableRef.value) === null || _primaryTableRef$valu === void 0 ? void 0 : (_primaryTableRef$valu2 = _primaryTableRef$valu.$refs) === null || _primaryTableRef$valu2 === void 0 ? void 0 : _primaryTableRef$valu2.tableContentRef;
            return renderTitleWidthIcon(h2, [titleContent, sortIcon, filterIcon], p.col, p.colIndex, ellipsisTitle, attach, {
              classPrefix,
              ellipsisOverlayClassName: props.size !== "medium" ? sizeClassNames[props.size] : ""
            });
          };
          item.ellipsisTitle = false;
        }
        if ((_item$edit = item.edit) !== null && _item$edit !== void 0 && _item$edit.component) {
          const oldCell = item.cell;
          item.cell = (h2, p) => {
            const cellProps = {
              ...p,
              oldCell,
              tableBaseClass,
              cellEmptyContent: props.cellEmptyContent,
              onChange: onPrimaryTableRowEdit,
              onValidate: onPrimaryTableRowValidate,
              onRuleChange
            };
            if (props.editableRowKeys) {
              var _errorListMap$value;
              const rowValue = get(p.row, props.rowKey || "id");
              cellProps.editable = editableKeysMap.value[rowValue] || false;
              const key = [rowValue, p.col.colKey].join("__");
              const errorList = (_errorListMap$value = errorListMap.value) === null || _errorListMap$value === void 0 ? void 0 : _errorListMap$value[key];
              errorList && (cellProps.errors = errorList);
            }
            if (props.editableCellState) cellProps.readonly = !props.editableCellState(p);
            return renderEditableCell(h2, cellProps);
          };
        }
        if ((_item$children2 = item.children) !== null && _item$children2 !== void 0 && _item$children2.length) item.children = getColumns(item.children);
        if (!item.children || (_item$children3 = item.children) !== null && _item$children3 !== void 0 && _item$children3.length) arr.push(item);
      }
      return arr;
    };
    const tColumns = computed(() => {
      const cols = getColumns(columns.value);
      if (showExpandIconColumn.value) cols.unshift(getExpandColumn(h));
      return cols;
    });
    const onInnerPageChange = (pageInfo, newData) => {
      var _props$onPageChange, _props$onChange;
      (_props$onPageChange = props.onPageChange) === null || _props$onPageChange === void 0 ? void 0 : _props$onPageChange.call(props, pageInfo, newData);
      context.emit("page-change", pageInfo, newData);
      const changeParams = [{
        pagination: pageInfo
      }, {
        trigger: "pagination",
        currentData: newData
      }];
      (_props$onChange = props.onChange) === null || _props$onChange === void 0 ? void 0 : _props$onChange.call(props, ...changeParams);
      context.emit("change", ...changeParams);
    };
    return {
      tColumns,
      showExpandedRow,
      tRowClassNames,
      hasEmptyCondition,
      primaryTableRef,
      tRowAttributes,
      primaryTableClasses,
      errorListMap,
      validateRowData,
      validateTableData,
      clearValidateData,
      renderLNode,
      renderColumnController,
      renderExpandedRow,
      onInnerExpandRowClick,
      renderFirstFilterRow,
      renderAsyncLoading,
      onInnerPageChange,
      setDragSortColumns
    };
  },
  methods: {
    getListener() {
      var _this = this;
      const listener = {};
      BASE_TABLE_ALL_EVENTS.forEach(key => {
        listener[key] = function () {
          for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
            args[_key] = arguments[_key];
          }
          _this.$emit(key, ...args);
        };
      });
      return listener;
    },
    formatNode(api, renderInnerNode, condition, extra) {
      const h = this.$createElement;
      if (!condition) return this[api];
      const innerNode = renderInnerNode(h);
      const propsNode = this.renderLNode(api);
      if (innerNode && !propsNode) return () => innerNode;
      if (propsNode && !innerNode) return () => propsNode;
      if (innerNode && propsNode) {
        return () => extra !== null && extra !== void 0 && extra.reverse ? h("div", [innerNode, propsNode]) : h("div", [propsNode, innerNode]);
      }
      return null;
    }
  },
  render() {
    const h = arguments[0];
    const isColumnController = !!(this.columnController && Object.keys(this.columnController).length);
    const placement = isColumnController ? this.columnController.placement || "top-right" : "";
    const isBottomController = isColumnController && (placement === null || placement === void 0 ? void 0 : placement.indexOf("bottom")) !== -1;
    const topContent = this.formatNode("topContent", this.renderColumnController, isColumnController && !isBottomController);
    const bottomContent = this.formatNode("bottomContent", this.renderColumnController, isBottomController, {
      reverse: true
    });
    const firstFullRow = this.formatNode("firstFullRow", this.renderFirstFilterRow, !this.hasEmptyCondition);
    const lastFullRow = this.formatNode("lastFullRow", this.renderAsyncLoading, !!this.asyncLoading);
    const props = {
      ...omit(this.$props, OMIT_PROPS),
      rowClassName: this.tRowClassNames,
      rowAttributes: this.tRowAttributes,
      columns: this.tColumns,
      topContent,
      bottomContent,
      firstFullRow,
      lastFullRow,
      renderExpandedRow: this.showExpandedRow ? this.renderExpandedRow : void 0
    };
    const on = {
      ...this.getListener(),
      "page-change": this.onInnerPageChange
    };
    if (this.expandOnRowClick) on["row-click"] = this.onInnerExpandRowClick;
    on.LeafColumnsChange = this.setDragSortColumns;
    return h(_BaseTable, helper([{
      "ref": "primaryTableRef",
      "scopedSlots": this.$scopedSlots
    }, {
      "props": props
    }, {}, {
      "on": on
    }, {}, this.$attrs, {
      "class": this.primaryTableClasses
    }]));
  }
});

export { _PrimaryTable as default };
//# sourceMappingURL=primary-table.js.map
