import { isNumber as u, isFunction as C } from "lodash-unified";
import { ref as I, unref as m, computed as w } from "vue";
import { dataIndexToField as P } from "../../ap-grid/utils/col.mjs";
import { getTableRenderProps as h, objectToString as G } 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 { getRowDragConfig as V } from "../utils.mjs";
import { useGlobalConfig as _ } from "../../config-provider/hooks/use-global-config.mjs";
import { useNamespace as K } from "../../config-provider/hooks/use-namespace.mjs";
import W from "../components/body-cell/index.vue2.mjs";
import z from "../components/tooltip/index.vue2.mjs";
import { isDef as F } from "../../utils/is.mjs";
import b from "../components/header-cell/index.vue2.mjs";
function M(r) {
  return u(r) && r > 1;
}
function j(r, o) {
  return (p) => ({
    ...C(o) ? o(p) : o ?? {},
    ...r
  });
}
function E(r, o) {
  if (o.bodyCellTooltip)
    return C(o.bodyCellTooltip) ? o.bodyCellTooltip(r.value, r.data) : o.bodyCellTooltip;
  if (o.renderText)
    return o.renderText({
      value: r.value,
      record: r.data,
      column: o,
      text: r.value
    });
  const p = o.valueType || "text", f = x[p];
  if (f) {
    const c = h(
      { ...o, valueType: p },
      {
        value: r.value,
        text: r.value,
        record: r.data,
        column: r.column,
        rowIndex: r.node?.rowIndex,
        pinned: r.node?.rowPinned
      }
    );
    return f(c);
  }
  return G(r.value);
}
const se = (r, o, p, f) => {
  const T = _("valueTypeMap"), c = I(), { em: v } = K("ag-grid"), g = (t) => {
    const s = {
      cellRenderer: W,
      cellRendererParams: {
        column: t,
        extraValueType: m(T),
        rowKey: o.rowKey
      }
    };
    if (t.customRender)
      return s;
    if (t.renderText)
      return {
        valueFormatter: (e) => {
          const a = e.colDef.colId, n = e.context.masks || {}, d = a ? n[a] : !1;
          return t.renderText({
            value: e.value,
            record: e.data,
            column: t,
            text: e.value,
            maskVisible: d
          });
        }
      };
    const i = t.valueType || "text", l = x[i];
    return l ? {
      valueFormatter: (e) => {
        const a = e.colDef.colId, n = e.context.masks || {};
        if (a ? n[a] : !1)
          return "****";
        const y = h(
          { ...t, valueType: i },
          {
            value: e.value,
            text: e.value,
            record: e.data,
            column: e.column,
            rowIndex: e.node?.rowIndex,
            pinned: e.node?.rowPinned
          }
        );
        return l(y);
      }
    } : s;
  }, D = (t) => {
    const s = t.valueType || "text", i = x[s], l = {}, e = t.bodyCellTooltip || !t.customRender && i && (t.ellipsis === !0 || t.ellipsis === "tooltip" || u(t.ellipsis)), a = u(t.ellipsis) && t.ellipsis > 1, n = u(t.ellipsis) ? t.ellipsis : 1;
    return l.tooltipValueGetter = e ? (d) => E(d, t) : void 0, l.wrapText = !t.ellipsis || a, l.cellStyle = j(
      {
        "--ag-grid-line-clamp": n
      },
      t.cellStyle
    ), l.tooltipComponent = z, l.tooltipComponentParams = {
      lineClamp: n,
      gridWrapperRef: c
    }, l;
  }, S = (t, s) => {
    const i = s.colKey && t.key === s.colKey;
    if (!(s.rowDragEntireRow || !i))
      return s.rowDragCallback ?? !0;
  }, R = w(() => {
    const t = m(r), s = V(o.rowDrag);
    function i(l) {
      return l.map((e) => {
        const a = {
          field: P(e.dataIndex),
          colId: F(e.key) ? String(e.key) : void 0,
          sortable: !!e.sorter,
          comparator: e.sorter === !0 ? () => 0 : e.sorter,
          headerComponentParams: {
            column: e,
            headerCell: f
          },
          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: p((n) => {
            const d = e.cellClass || "", y = C(d) ? d(n.data, n.node) : d;
            return [
              `${v("cell", e.align || "left")}`,
              M(e.ellipsis) ? v("cell", "multiline") : null,
              y
            ].flat().filter(Boolean);
          }),
          cellStyle: e.cellStyle,
          headerStyle: e.headerStyle,
          headerClass: e.headerClass,
          columnGroupShow: e.columnGroupShow,
          autoHeight: !0,
          // 设置自动计算高度以自适应多行换行的场景
          rowDrag: S(e, s),
          ...D(e),
          ...g(e)
        };
        return e.children?.length ? {
          ...a,
          headerGroupComponent: b,
          headerGroupComponentParams: {
            column: e,
            headerCell: f
          },
          children: i(e.children)
        } : a;
      });
    }
    return i(t);
  }), k = w(
    () => ({
      headerComponent: b,
      resizable: o.columnResizable,
      suppressMovable: !0,
      cellDataType: !1
    })
  );
  return {
    columnDefs: R,
    defaultColDef: k,
    gridWrapperRef: c
  };
};
export {
  se as useColumnsDef
};
