import { isNumber as c, isFunction as y } from "lodash-unified";
import { ref as R, unref as m, computed as b } from "vue";
import { dataIndexToField as k } from "../../ap-grid/utils/col.mjs";
import { getTableRenderProps as w, objectToString as P } from "../../ap-table/utils.mjs";
import "../../config-provider/index.mjs";
import "../../utils/index.mjs";
import "../components/body-cell/index.vue.mjs";
import "../components/header-cell/index.vue.mjs";
import "../components/tooltip/index.vue.mjs";
import { rendererMap as x } from "../renderer/index.mjs";
import { useGlobalConfig as G } from "../../config-provider/hooks/use-global-config.mjs";
import { useNamespace as V } from "../../config-provider/hooks/use-namespace.mjs";
import _ from "../components/body-cell/index.vue2.mjs";
import W from "../components/tooltip/index.vue2.mjs";
import { isDef as z } from "../../utils/is.mjs";
import h from "../components/header-cell/index.vue2.mjs";
function F(r) {
  return c(r) && r > 1;
}
function M(r, s) {
  return (n) => ({
    ...y(s) ? s(n) : s ?? {},
    ...r
  });
}
function j(r, s) {
  if (s.bodyCellTooltip)
    return y(s.bodyCellTooltip) ? s.bodyCellTooltip(r.value, r.data) : s.bodyCellTooltip;
  if (s.renderText)
    return s.renderText({
      value: r.value,
      record: r.data,
      column: s,
      text: r.value
    });
  const n = s.valueType || "text", d = x[n];
  if (d) {
    const u = w(
      { ...s, valueType: n },
      {
        value: r.value,
        text: r.value,
        record: r.data,
        column: r.column,
        rowIndex: r.node?.rowIndex,
        pinned: r.node?.rowPinned
      }
    );
    return d(u);
  }
  return P(r.value);
}
const ee = (r, s, n, d) => {
  const C = G("valueTypeMap"), u = R(), { em: T } = V("ag-grid"), S = (t) => {
    const i = {
      cellRenderer: _,
      cellRendererParams: {
        column: t,
        extraValueType: m(C),
        rowKey: s.rowKey
      }
    };
    if (t.customRender)
      return i;
    if (t.renderText)
      return {
        valueFormatter: (o) => {
          const l = o.colDef.colId, a = o.context.masks || {}, f = l ? a[l] : !1;
          return t.renderText({
            value: o.value,
            record: o.data,
            column: t,
            text: o.value,
            maskVisible: f
          });
        }
      };
    const p = t.valueType || "text", e = x[p];
    return e ? {
      valueFormatter: (o) => {
        const l = o.colDef.colId, a = o.context.masks || {};
        if (l ? a[l] : !1)
          return "****";
        const v = w(
          { ...t, valueType: p },
          {
            value: o.value,
            text: o.value,
            record: o.data,
            column: o.column,
            rowIndex: o.node?.rowIndex,
            pinned: o.node?.rowPinned
          }
        );
        return e(v);
      }
    } : i;
  }, g = (t) => {
    const i = t.valueType || "text", p = x[i], e = {}, o = t.bodyCellTooltip || !t.customRender && p && (t.ellipsis === !0 || t.ellipsis === "tooltip" || c(t.ellipsis)), l = c(t.ellipsis) && t.ellipsis > 1, a = c(t.ellipsis) ? t.ellipsis : 1;
    return e.tooltipValueGetter = o ? (f) => j(f, t) : void 0, e.wrapText = !t.ellipsis || l, e.cellStyle = M(
      {
        "--ag-grid-line-clamp": a
      },
      t.cellStyle
    ), e.tooltipComponent = W, e.tooltipComponentParams = {
      lineClamp: a,
      gridWrapperRef: u
    }, e;
  }, D = b(() => {
    const t = m(r);
    function i(p) {
      return p.map((e) => {
        const o = {
          field: k(e.dataIndex),
          colId: z(e.key) ? String(e.key) : void 0,
          sortable: !!e.sorter,
          comparator: e.sorter === !0 ? () => 0 : e.sorter,
          headerComponentParams: {
            column: e,
            headerCell: d
          },
          resizable: e.resizable,
          width: e.width,
          minWidth: e.minWidth,
          maxWidth: e.maxWidth,
          suppressKeyboardEvent: () => !0,
          // 避免单元格内元素事件被劫持
          flex: e.width ? void 0 : e.flex ?? 1,
          // 如果没有设置确切的宽度，则设置为可自由伸缩列
          pinned: e.fixed || null,
          // undefined to null
          colSpan: e.colSpan,
          spanRows: e.spanRows,
          cellClass: n((l) => {
            const a = e.cellClass || "", f = y(a) ? a(l.data, l.node) : a;
            return [
              `${T("cell", e.align || "left")}`,
              F(e.ellipsis) ? T("cell", "multiline") : null,
              f
            ].flat().filter(Boolean);
          }),
          cellStyle: e.cellStyle,
          headerStyle: e.headerStyle,
          headerClass: e.headerClass,
          columnGroupShow: e.columnGroupShow,
          autoHeight: !0,
          // 设置自动计算高度以自适应多行换行的场景
          ...g(e),
          ...S(e)
        };
        return e.children?.length ? {
          ...o,
          headerGroupComponent: h,
          headerGroupComponentParams: {
            column: e,
            headerCell: d
          },
          children: i(e.children)
        } : o;
      });
    }
    return i(t);
  }), I = b(
    () => ({
      headerComponent: h,
      resizable: s.columnResizable,
      suppressMovable: !0,
      cellDataType: !1
    })
  );
  return {
    columnDefs: D,
    defaultColDef: I,
    gridWrapperRef: u
  };
};
export {
  ee as useColumnsDef
};
