import { defineComponent as G, useSlots as H, toRef as K, ref as g, unref as s, watch as P, nextTick as _, openBlock as A, createBlock as W, mergeProps as h, withCtx as y, createVNode as U, createSlots as $, renderList as q, renderSlot as J, normalizeProps as Q, guardReactiveProps as X } from "vue";
import { isUndefined as Y, isNumber as m, cloneDeep as Z, isArray as M, omit as ee } from "lodash-unified";
import { ApForm as C } from "../../ap-form/index.mjs";
import { useInjectForm as te } from "../../ap-form/context.mjs";
import { getModelValue as ae } from "../../ap-form/utils/get.mjs";
import "../../config-provider/index.mjs";
import "../../utils/index.mjs";
import { useDevWarning as oe } from "../../utils/warning.mjs";
import le from "../hooks/use-editable-columns.mjs";
import "../index.vue2.mjs";
import ne from "../style/editable.mjs";
import { useNamespace as re } from "../../config-provider/hooks/use-namespace.mjs";
import { useGlobalConfig as se } from "../../config-provider/hooks/use-global-config.mjs";
import { isDef as w } from "../../utils/is.mjs";
import ie from "../index.vue.mjs";
const ve = /* @__PURE__ */ G({
  name: "EditableGridFormItem",
  __name: "form-item",
  props: {
    dropdownPrefixCls: {},
    bordered: {
      type: Boolean,
      default: !1
    },
    locale: {},
    onResizeColumn: {},
    getPopupContainer: {},
    scroll: {},
    sortDirections: {
      default: () => ["ascend", "descend"]
    },
    showSorterTooltip: {
      type: [Boolean, Object],
      default: !0
    },
    prefixCls: {},
    tableLayout: {
      default: "fixed"
    },
    title: {},
    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],
      default: !1
    },
    transformCellText: {},
    rowHoverable: {
      type: Boolean
    },
    rowClassName: {},
    stripe: {
      type: Boolean
    },
    headerRowClassName: {},
    footerRowClassName: {},
    cellClassName: {},
    headerCellClassName: {},
    footerCellClassName: {},
    rowStyle: {},
    headerRowStyle: {},
    footerRowStyle: {},
    cellStyle: {},
    headerCellStyle: {},
    footerCellStyle: {},
    height: {},
    minHeight: {},
    maxHeight: {},
    mergeCells: {},
    mergeFooterItems: {},
    cellConfig: {},
    loading: {
      type: Boolean,
      default: void 0
    },
    loadingConfig: {},
    headerCellConfig: {},
    footerCellConfig: {},
    round: {
      type: Boolean
    },
    rowSelection: {
      type: [Boolean, Object],
      default: void 0
    },
    onLoadingChange: {},
    onShownColumnsChange: {},
    pagination: {
      type: [Boolean, Object],
      default: !1
    },
    searchFormWrapperStyle: {},
    tableWrapperStyle: {},
    wrapperStyle: {},
    size: {
      default: "mini"
    },
    adaptive: {
      type: Boolean,
      default: !1
    },
    columnResizable: {
      type: [Boolean, Object],
      default: !1
    },
    rowKey: {
      default: "key"
    },
    footer: {},
    virtual: {
      type: [Boolean, Object],
      default: void 0
    },
    expandable: {},
    value: {},
    rowHighlightable: {
      type: [Boolean, Function]
    },
    treeConfig: {},
    onReady: {},
    columns: {},
    defaultValue: {},
    "onUpdate:value": {},
    onChange: {},
    maxLength: {},
    onFieldChange: {},
    label: {},
    name: {},
    formItem: {
      default: () => ({})
    }
  },
  setup(d, {
    expose: b
  }) {
    const x = H(), o = d;
    oe("EditableGridFormItem")(!1, "breaking", "EditableGridFormItem has been EOL, Please use EditableAgGridFormItem(https://aplus.aplnk.com/aplus-ui/components/ag-grid/editable/) instead.");
    const {
      b: R
    } = re("editable-grid"), B = ne("editable-grid"), {
      internalInstance: i,
      model: S
    } = te(), p = K(o.name), f = g(ae(s(S), p.value) || []), v = C.useWatch(p);
    P(() => v.value, async (e) => {
      await _(), f.value = e, o.onChange?.(e);
    }, {
      deep: !0
    });
    const c = g(), F = se("valueTypeMap"), I = le(o, F);
    function N(e, l) {
      const r = s(f)?.length || 0;
      if (w(o.maxLength) && r >= o.maxLength)
        return;
      const t = {
        ...e || {}
      };
      i?.setFieldValue(o.name, t, !0, (a, n) => {
        if (!a[n]) {
          a[n] = [t];
          return;
        }
        Y(l) ? a[n].push(t) : a[n].splice(l, 0, t);
      });
    }
    function D(e, l = "suffix") {
      const r = s(f)?.length || 0, t = m(e) ? e : e.length;
      if (w(o.maxLength) && r + t > o.maxLength)
        return;
      const a = m(e) ? new Array(e).fill(0).map(() => ({})) : Z(e);
      i?.setFieldValue(o.name, a, !0, (n, u) => {
        if (!n[u]) {
          n[u] = [...a];
          return;
        }
        l === "suffix" ? n[u].push(...a) : n[u].unshift(...a);
      });
    }
    function E(e) {
      const l = new Set(m(e) ? [e] : e);
      i?.setFieldValue(o.name, void 0, !0, (r, t) => {
        r[t] = r[t].filter((a, n) => !l.has(n));
      });
    }
    function k(e) {
      const l = new Set(M(e) ? e : [e]);
      l.size !== 0 && i?.setFieldValue?.(o.name, void 0, !0, (r, t) => {
        r[t] = r[t].filter((a) => !l.has(a[o.rowKey]));
      });
    }
    function L() {
      return s(f);
    }
    function V(e) {
      return s(f)?.[e];
    }
    function T(e, l, r = !0) {
      i?.setFieldValue(o.name, void 0, !0, (t, a) => {
        if (!(!t[a] || !t[a][e])) {
          if (!r) {
            const n = Object.keys(t[a][e]);
            for (const u of n)
              u !== o.rowKey && delete t[a][e][u];
          }
          Object.assign(t[a][e], {
            ...l
          });
        }
      });
    }
    function j(e) {
      i?.setFieldValue(o.name, e);
    }
    function O(e) {
      const l = e === "end" ? f.value.length - 1 : e;
      c.value?.scrollToRow(l);
    }
    function z() {
      i?.setFieldValue?.(o.name, []);
    }
    return b({
      add: N,
      remove: E,
      getRowData: V,
      getRowsData: L,
      setRowData: T,
      addMultiple: D,
      scrollTo: O,
      setTableData: j,
      clear: z,
      removeByKey: k
    }), (e, l) => (A(), W(s(C).FormItem, h(d.formItem, {
      name: d.name,
      label: d.label
    }), {
      default: y(() => [U(ie, h(s(ee)(o, ["name", "value", "onUpdate:value", "maxLength", "onChange", "defaultValue", "formItem"]), {
        ref_key: "tableRef",
        ref: c,
        class: [s(R)(), s(B)],
        columns: s(I),
        "data-source": f.value,
        "search-form": !1
      }), $({
        _: 2
      }, [q(x, (r, t) => ({
        name: t,
        fn: y((a) => [J(e.$slots, t, Q(X(a || {})))])
      }))]), 1040, ["class", "columns", "data-source"])]),
      _: 3
    }, 16, ["name", "label"]));
  }
});
export {
  ve as default
};
