"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const types = require("../../../../utils/types.js");
const error = require("../../../../utils/error.js");
const index = require("../../../../hooks/use-namespace/index.js");
const util = require("../util.js");
const config = require("../config.js");
const Vue = require("vue");
const shared_esmBundler = require("../../../../../../@vue/shared/dist/shared.esm-bundler.js");
function useRender(props, slots, owner) {
  const instance = Vue.getCurrentInstance();
  const columnId = Vue.ref("");
  const isSubColumn = Vue.ref(false);
  const realAlign = Vue.ref();
  const realHeaderAlign = Vue.ref();
  const ns = index.useNamespace("table");
  Vue.watchEffect(() => {
    realAlign.value = props.align ? `is-${props.align}` : null;
    realAlign.value;
  });
  Vue.watchEffect(() => {
    realHeaderAlign.value = props.headerAlign ? `is-${props.headerAlign}` : realAlign.value;
    realHeaderAlign.value;
  });
  const columnOrTableParent = Vue.computed(() => {
    let parent = instance.vnode.vParent || instance.parent;
    while (parent && !parent.tableId && !parent.columnId) parent = parent.vnode.vParent || parent.parent;
    return parent;
  });
  const hasTreeColumn = Vue.computed(() => {
    const { store } = instance.parent;
    if (!store) return false;
    const { treeData } = store.states;
    const treeDataValue = treeData.value;
    return treeDataValue && Object.keys(treeDataValue).length > 0;
  });
  const realWidth = Vue.ref(util.parseWidth(props.width));
  const realMinWidth = Vue.ref(util.parseMinWidth(props.minWidth));
  const setColumnWidth = (column) => {
    if (realWidth.value) column.width = realWidth.value;
    if (realMinWidth.value) column.minWidth = realMinWidth.value;
    if (!realWidth.value && realMinWidth.value) column.width = void 0;
    if (!column.minWidth) column.minWidth = 80;
    column.realWidth = Number(types.isUndefined(column.width) ? column.minWidth : column.width);
    return column;
  };
  const setColumnForcedProps = (column) => {
    const type = column.type;
    const source = config.cellForced[type] || {};
    Object.keys(source).forEach((prop) => {
      const value = source[prop];
      if (prop !== "className" && !types.isUndefined(value)) column[prop] = value;
    });
    const className = config.getDefaultClassName(type);
    if (className) {
      const forceClass = `${Vue.unref(ns.namespace)}-${className}`;
      column.className = column.className ? `${column.className} ${forceClass}` : forceClass;
    }
    return column;
  };
  const checkSubColumn = (children) => {
    if (shared_esmBundler.isArray(children)) children.forEach((child) => check(child));
    else check(children);
    function check(item) {
      var _a;
      if (((_a = item == null ? void 0 : item.type) == null ? void 0 : _a.name) === "ElTableColumn") item.vParent = instance;
    }
  };
  const setColumnRenders = (column) => {
    if (props.renderHeader) error.debugWarn("TableColumn", "Comparing to render-header, scoped-slot header is easier to use. We recommend users to use scoped-slot header.");
    else if (column.type !== "selection") column.renderHeader = (scope) => {
      instance.columnConfig.value["label"];
      if (slots.header) {
        const slotResult = slots.header(scope);
        if (util.ensureValidVNode(slotResult)) return Vue.h(Vue.Fragment, slotResult);
      }
      return Vue.createTextVNode(column.label);
    };
    if (slots["filter-icon"]) column.renderFilterIcon = (scope) => {
      return Vue.renderSlot(slots, "filter-icon", scope);
    };
    if (slots.expand) column.renderExpand = (scope) => {
      return Vue.renderSlot(slots, "expand", scope);
    };
    let originRenderCell = column.renderCell;
    if (column.type === "expand") {
      column.renderCell = (data) => Vue.h("div", { class: "cell" }, [originRenderCell(data)]);
      owner.value.renderExpanded = (row) => {
        return slots.default ? slots.default(row) : slots.default;
      };
    } else {
      originRenderCell = originRenderCell || config.defaultRenderCell;
      column.renderCell = (data) => {
        let children = null;
        if (slots.default) {
          const vnodes = slots.default(data);
          children = vnodes.some((v) => v.type !== Vue.Comment) ? vnodes : originRenderCell(data);
        } else children = originRenderCell(data);
        const { columns } = owner.value.store.states;
        const firstUserColumnIndex = columns.value.findIndex((item) => item.type === "default");
        const prefix = config.treeCellPrefix(data, hasTreeColumn.value && data.cellIndex === firstUserColumnIndex);
        const props2 = {
          class: "cell",
          style: {}
        };
        if (column.showOverflowTooltip) {
          props2.class = `${props2.class} ${Vue.unref(ns.namespace)}-tooltip`;
          props2.style = { width: `${(data.column.realWidth || Number(data.column.width)) - 1}px` };
        }
        checkSubColumn(children);
        return Vue.h("div", props2, [prefix, children]);
      };
    }
    return column;
  };
  const getPropsData = (...propsKey) => {
    return propsKey.reduce((prev, cur) => {
      if (shared_esmBundler.isArray(cur)) cur.forEach((key) => {
        prev[key] = props[key];
      });
      return prev;
    }, {});
  };
  const getColumnElIndex = (children, child) => {
    return Array.prototype.indexOf.call(children, child);
  };
  const updateColumnOrder = () => {
    owner.value.store.commit("updateColumnOrder", instance.columnConfig.value);
  };
  return {
    columnId,
    realAlign,
    isSubColumn,
    realHeaderAlign,
    columnOrTableParent,
    setColumnWidth,
    setColumnForcedProps,
    setColumnRenders,
    getPropsData,
    getColumnElIndex,
    updateColumnOrder
  };
}
exports.default = useRender;
