import { defineComponent as A, ref as K, unref as r, useSlots as O, toRef as _, watch as G, nextTick as H, shallowRef as W, openBlock as U, createBlock as $, mergeProps as p, withCtx as h, createVNode as q, createSlots as J, renderList as Q, renderSlot as X, normalizeProps as Y, guardReactiveProps as Z } from "vue";
import { Table as M } from "@aplus-frontend/antdv";
import { isUndefined as ee, isNumber as m, cloneDeep as te, isArray as ae, omit as b } from "lodash-unified";
import { getModelValue as oe } from "../ap-form/utils/get.mjs";
import { useDevWarning as ne } from "../utils/warning.mjs";
import { ApForm as g } from "../ap-form/index.mjs";
import { useInjectForm as le } from "../ap-form/context.mjs";
import "../config-provider/index.mjs";
import "../utils/index.mjs";
import re from "./hooks/use-get-columns.mjs";
import se from "./style/index.mjs";
import { scrollToRowIndex as ie } from "./utils.mjs";
import { useNamespace as ue } from "../config-provider/hooks/use-namespace.mjs";
import { useGlobalConfig as fe } from "../config-provider/hooks/use-global-config.mjs";
import { isDef as w } from "../utils/is.mjs";
const Te = /* @__PURE__ */ A({
  name: "EditableTableFormItem",
  __name: "form-item",
  props: {
    dropdownPrefixCls: {},
    pagination: {
      type: [Boolean, Object],
      default: !1
    },
    loading: {
      type: [Boolean, Object],
      default: void 0
    },
    size: {
      default: "small"
    },
    bordered: {
      type: Boolean,
      default: !1
    },
    locale: {},
    onResizeColumn: {},
    rowSelection: {},
    getPopupContainer: {},
    scroll: {},
    sortDirections: {
      default: () => ["ascend", "descend"]
    },
    showSorterTooltip: {
      type: [Boolean, Object],
      default: !0
    },
    prefixCls: {},
    rowKey: {
      type: [String, Function],
      default: "key"
    },
    tableLayout: {
      default: "fixed"
    },
    rowClassName: {},
    title: {},
    footer: {},
    id: {},
    showHeader: {
      type: Boolean,
      default: !0
    },
    components: {},
    customRow: {},
    customHeaderRow: {},
    direction: {},
    expandFixed: {
      type: [String, Boolean],
      default: !1
    },
    expandColumnWidth: {},
    expandedRowKeys: {},
    defaultExpandedRowKeys: {},
    expandedRowRender: {},
    expandRowByClick: {
      type: Boolean,
      default: !1
    },
    expandIcon: {},
    onExpand: {},
    onExpandedRowsChange: {},
    defaultExpandAllRows: {
      type: Boolean,
      default: !1
    },
    indentSize: {
      default: 15
    },
    expandIconColumnIndex: {},
    showExpandColumn: {
      type: Boolean,
      default: !0
    },
    expandedRowClassName: {},
    childrenColumnName: {
      default: "children"
    },
    rowExpandable: {},
    sticky: {
      type: [Boolean, Object]
    },
    transformCellText: {},
    rowHoverable: {
      type: Boolean
    },
    columns: {},
    value: {},
    defaultValue: {},
    "onUpdate:value": {},
    onChange: {},
    maxLength: {},
    onFieldChange: {},
    label: {},
    name: {},
    formItem: {
      default: () => ({})
    }
  },
  setup(f, {
    expose: x
  }) {
    const o = f;
    ne("EditableTableFormItem")(!1, "breaking", "EditableTableFormItem has been EOL, Please use EditableAgGridFormItem(https://aplus.aplnk.com/aplus-ui/components/ag-grid/editable/) instead.");
    const {
      internalInstance: i,
      model: y
    } = le(), u = K(oe(r(y), o.name) || []), C = O(), R = _(o, "name"), F = g.useWatch(R);
    G(() => F.value, async (e) => {
      await H(), u.value = e, o.onChange?.(e);
    }, {
      deep: !0
    });
    const {
      b: B
    } = ue("editable-table"), v = se("editable-table"), c = W(), I = fe("valueTypeMap"), T = re(o, I);
    function D(e, n) {
      const l = r(u)?.length || 0;
      if (w(o.maxLength) && l >= o.maxLength)
        return;
      const t = {
        ...e || {}
      };
      i?.setFieldValue(o.name, t, !0, (a, s) => {
        if (!a[s]) {
          a[s] = [t];
          return;
        }
        ee(n) ? a[s].push(t) : a[s].splice(n, 0, t);
      });
    }
    function E(e, n = "suffix") {
      const l = r(u)?.length || 0, t = m(e) ? e : e.length;
      if (w(o.maxLength) && l + t > o.maxLength)
        return;
      const a = m(e) ? new Array(e).fill(0).map(() => ({})) : te(e);
      i?.setFieldValue(o.name, a, !0, (s, d) => {
        if (!s[d]) {
          s[d] = [...a];
          return;
        }
        n === "suffix" ? s[d].push(...a) : s[d].unshift(...a);
      });
    }
    function S(e) {
      const n = new Set(m(e) ? [e] : e);
      i?.setFieldValue(o.name, void 0, !0, (l, t) => {
        l[t] = l[t].filter((a, s) => !n.has(s));
      });
    }
    function L(e) {
      const n = new Set(ae(e) ? e : [e]);
      n.size !== 0 && i?.setFieldValue?.(o.name, void 0, !0, (l, t) => {
        l[t] = l[t].filter((a) => !n.has(a[o.rowKey]));
      });
    }
    function V() {
      return r(u);
    }
    function k(e) {
      return r(u)?.[e];
    }
    function N(e, n, l = !0) {
      i?.setFieldValue(o.name, void 0, !0, (t, a) => {
        !t[a] || !t[a][e] || (t[a][e] = {
          ...l ? t[a][e] : {},
          ...n
        });
      });
    }
    function P(e) {
      i?.setFieldValue(o.name, e);
    }
    function j(e, n) {
      const l = e === "end" ? u.value.length - 1 : e;
      ie(l, c.value?.$el, n);
    }
    function z() {
      i?.setFieldValue?.(o.name, []);
    }
    return x({
      add: D,
      remove: S,
      getRowData: k,
      getRowsData: V,
      setRowData: N,
      addMultiple: E,
      scrollTo: j,
      setTableData: P,
      clear: z,
      removeByKey: L
    }), (e, n) => (U(), $(r(g).FormItem, p(f.formItem, {
      name: f.name,
      label: f.label
    }), {
      default: h(() => [q(r(M), p(r(b)(o, ["name", "maxLength", "onChange"]), {
        ref_key: "tableRef",
        ref: c,
        class: [r(B)(), r(v)],
        columns: r(T),
        "data-source": u.value
      }), J({
        _: 2
      }, [Q(r(b)(C, ["headerCell"]), (l, t) => ({
        name: t,
        fn: h((a) => [X(e.$slots, t, Y(Z(a || {})))])
      }))]), 1040, ["class", "columns", "data-source"])]),
      _: 3
    }, 16, ["name", "label"]));
  }
});
export {
  Te as default
};
