import { isUndefined } from "../../../../utils/types.mjs";
import { useGlobalConfig } from "../../../config-provider/src/hooks/use-global-config.mjs";
import { ElCheckbox } from "../../../checkbox/index.mjs";
import { mergeOptions, compose } from "../util.mjs";
import { cellStarts } from "../config.mjs";
import useWatcher from "./watcher-helper.mjs";
import useRender from "./render-helper.mjs";
import defaults_default from "./defaults.mjs";
import { defineComponent, Fragment, h, getCurrentInstance, ref, computed, onBeforeMount, onMounted, onBeforeUnmount } from "vue";
import { isArray, isString } from "../../../../../../@vue/shared/dist/shared.esm-bundler.mjs";
let columnIdSeed = 1;
var table_column_default = defineComponent({
  name: "ElTableColumn",
  components: { ElCheckbox },
  props: defaults_default,
  setup(props, { slots }) {
    const instance = getCurrentInstance();
    const globalConfig = useGlobalConfig("table");
    const columnConfig = ref({});
    const owner = computed(() => {
      let parent2 = instance.parent;
      while (parent2 && !parent2.tableId) parent2 = parent2.parent;
      return parent2;
    });
    const { registerNormalWatchers, registerComplexWatchers } = useWatcher(owner, props);
    const { columnId, isSubColumn, realHeaderAlign, columnOrTableParent, setColumnWidth, setColumnForcedProps, setColumnRenders, getPropsData, getColumnElIndex, realAlign, updateColumnOrder } = useRender(props, slots, owner);
    const parent = columnOrTableParent.value;
    columnId.value = `${"tableId" in parent && parent.tableId || "columnId" in parent && parent.columnId}_column_${columnIdSeed++}`;
    onBeforeMount(() => {
      var _a, _b;
      isSubColumn.value = owner.value !== parent;
      const type = props.type || "default";
      const sortable = props.sortable === "" ? true : props.sortable;
      const showOverflowTooltip = type === "selection" ? false : isUndefined(props.showOverflowTooltip) ? parent.props.showOverflowTooltip ?? ((_a = globalConfig.value) == null ? void 0 : _a.showOverflowTooltip) : props.showOverflowTooltip;
      const tooltipFormatter = isUndefined(props.tooltipFormatter) ? parent.props.tooltipFormatter ?? ((_b = globalConfig.value) == null ? void 0 : _b.tooltipFormatter) : props.tooltipFormatter;
      const defaults = {
        ...cellStarts[type],
        id: columnId.value,
        type,
        property: props.prop || props.property,
        align: realAlign,
        headerAlign: realHeaderAlign,
        showOverflowTooltip,
        tooltipFormatter,
        filterable: props.filters || props.filterMethod,
        filteredValue: [],
        filterPlacement: "",
        filterClassName: "",
        isColumnGroup: false,
        isSubColumn: false,
        filterOpened: false,
        sortable,
        index: props.index,
        rawColumnKey: instance.vnode.key
      };
      let column = getPropsData([
        "columnKey",
        "label",
        "className",
        "labelClassName",
        "type",
        "renderHeader",
        "formatter",
        "fixed",
        "resizable"
      ], [
        "sortMethod",
        "sortBy",
        "sortOrders"
      ], ["selectable", "reserveSelection"], [
        "filterMethod",
        "filters",
        "filterMultiple",
        "filterOpened",
        "filteredValue",
        "filterPlacement",
        "filterClassName"
      ]);
      column = mergeOptions(defaults, column);
      column = compose(setColumnRenders, setColumnWidth, setColumnForcedProps)(column);
      columnConfig.value = column;
      registerNormalWatchers();
      registerComplexWatchers();
    });
    onMounted(() => {
      var _a, _b;
      const parent2 = columnOrTableParent.value;
      const children = isSubColumn.value ? (_a = parent2.vnode.el) == null ? void 0 : _a.children : (_b = parent2.refs.hiddenColumns) == null ? void 0 : _b.children;
      const getColumnIndex = () => getColumnElIndex(children || [], instance.vnode.el);
      columnConfig.value.getColumnIndex = getColumnIndex;
      getColumnIndex() > -1 && owner.value.store.commit("insertColumn", columnConfig.value, isSubColumn.value ? "columnConfig" in parent2 && parent2.columnConfig.value : null, updateColumnOrder);
    });
    onBeforeUnmount(() => {
      const getColumnIndex = columnConfig.value.getColumnIndex;
      (getColumnIndex ? getColumnIndex() : -1) > -1 && owner.value.store.commit("removeColumn", columnConfig.value, isSubColumn.value ? "columnConfig" in parent && parent.columnConfig.value : null, updateColumnOrder);
    });
    instance.columnId = columnId.value;
    instance.columnConfig = columnConfig;
  },
  render() {
    var _a, _b, _c;
    try {
      const renderDefault = (_b = (_a = this.$slots).default) == null ? void 0 : _b.call(_a, {
        row: {},
        column: {},
        $index: -1
      });
      const children = [];
      if (isArray(renderDefault)) {
        for (const childNode of renderDefault) if (((_c = childNode.type) == null ? void 0 : _c.name) === "ElTableColumn" || childNode.shapeFlag & 2) children.push(childNode);
        else if (childNode.type === Fragment && isArray(childNode.children)) childNode.children.forEach((vnode) => {
          if ((vnode == null ? void 0 : vnode.patchFlag) !== 1024 && !isString(vnode == null ? void 0 : vnode.children)) children.push(vnode);
        });
      }
      return h("div", children);
    } catch {
      return h("div", []);
    }
  }
});
export {
  table_column_default as default
};
