import { clsx as v } from "clsx";
import { isArray as g, isFunction as C } from "lodash-unified";
import { ref as E, watch as F, unref as y } from "vue";
import { getFinalEditableNode as q } from "../../ap-grid/utils/editable.mjs";
import { updateFormProps as A, getFieldProps as K } from "../../ap-table/utils.mjs";
import { getColumnIsRequired as M } from "../../editable-table/utils.mjs";
import "../../utils/index.mjs";
import { getRowKey as S } from "../editable/utils.mjs";
import { isDef as D } from "../../utils/is.mjs";
function I(t, d, r) {
  t.__skipNextRefresh ? t.__skipNextRefresh[d] = r : t.__skipNextRefresh = { [d]: r };
}
function G(t, d, r) {
  t.__prevEditable ? t.__prevEditable[d] = r : t.__prevEditable = { [d]: r };
}
const Q = (t, d, r) => {
  const _ = E([]);
  F(
    () => t.columns,
    (e) => {
      if (!e?.length) {
        _.value = [];
        return;
      }
      function c(a) {
        return a.map((n) => ({
          ...n,
          _requireMark: M(n),
          children: c(n.children || []),
          // 处理可编辑/不可编辑下的单元格渲染
          ...R(n)
        }));
      }
      _.value = c(e);
    },
    { immediate: !0, deep: !0 }
  );
  function R(e) {
    const c = e.editable, a = r ? [r] : g(t.name) ? t.name : [t.name];
    return !c || e.children?.length ? {
      _path: [...a]
    } : {
      customRender({ value: n, originalNode: p, params: o, ...i }) {
        const s = C(e.editable) ? e.editable(
          i.text,
          i.record,
          i.rowIndex
        ) : !!e.editable;
        let u = p;
        const w = g(e.dataIndex) ? e.dataIndex : [e.dataIndex], x = S(
          i.record,
          i.rowIndex,
          t.rowKey
        );
        if (s) {
          const h = A(
            e,
            K(e.fieldProps, {
              value: n,
              ...i
            })
          ), b = {};
          t.onFieldChange && (b[`onUpdate:${h.valuePropName || "value"}`] = (k) => t.onFieldChange?.(
            i.rowIndex,
            e.dataIndex,
            k,
            i.record
          ));
          const N = {
            name: [...a, String(x), ...w],
            ...h || {},
            field: {
              style: "width: 100%",
              ...h?.field || {},
              ...b,
              class: v("is-editable", h.class)
            }
          };
          u = q(
            e,
            N,
            i.record,
            y(d)
          );
        }
        e.customRender && (u = e.customRender({
          value: n,
          ...i,
          editable: s,
          originalNode: u,
          index: i.rowIndex,
          renderIndex: i.rowIndex,
          path: [...a, String(x)]
        }));
        const l = o.node, f = o.column.getColId(), m = l.__prevEditable?.[f];
        return !l.__skipNextRefresh?.[f] && D(m) && m !== s && (I(l, f, !0), setTimeout(() => {
          o?.api.refreshCells({
            rowNodes: [o.node],
            columns: [o.column.getColId()],
            force: !0
          }), I(l, f, !1);
        }, 0)), G(l, f, s), u;
      },
      _path: [...a]
    };
  }
  return _;
};
export {
  Q as default,
  Q as useGetEditableColumns
};
