import { defineComponent as Y, useSlots as Z, ref as g, watch as y, unref as l, computed as ee, openBlock as oe, createElementBlock as te, Fragment as ae, createVNode as f, mergeProps as h, withCtx as C, createSlots as ne, renderList as re, renderSlot as le, normalizeProps as ie, guardReactiveProps as se } from "vue";
import { isEqual as R, cloneDeep as ue, omit as de } from "lodash-unified";
import { ApForm as b } from "../../ap-form/index.mjs";
import { useInjectForm as me } from "../../ap-form/context.mjs";
import v from "../../ap-form/hooks/use-watch.mjs";
import { getModelValue as pe } from "../../ap-form/utils/get.mjs";
import { transformValues as ce } from "../../ap-form/utils/transform.mjs";
import "../../config-provider/index.mjs";
import fe from "../hooks/use-editable-columns.mjs";
import { useEditableFormItemApi as we } from "../hooks/use-editable-form-item-api.mjs";
import "../index.vue2.mjs";
import ge from "../style/editable.mjs";
import { getShadowName as ye } from "../utils.mjs";
import { getColumnTransformOptions as he, arrayToObject as Ce, getRowKey as Re } from "./utils.mjs";
import { useNamespace as be } from "../../config-provider/hooks/use-namespace.mjs";
import { useGlobalConfig as ve } from "../../config-provider/hooks/use-global-config.mjs";
import xe from "../index.vue.mjs";
const He = /* @__PURE__ */ Y({
  name: "EditableGrid",
  __name: "form-item",
  props: {
    dropdownPrefixCls: {},
    bordered: {
      type: Boolean
    },
    locale: {},
    onResizeColumn: {},
    getPopupContainer: {},
    scroll: {},
    sortDirections: {},
    showSorterTooltip: {
      type: [Boolean, Object]
    },
    prefixCls: {},
    rowKey: {
      type: [String, Function],
      default: "key"
    },
    tableLayout: {},
    title: {},
    id: {},
    showHeader: {
      type: Boolean
    },
    components: {},
    customHeaderRow: {},
    direction: {},
    expandFixed: {
      type: [String, Boolean]
    },
    expandColumnWidth: {},
    expandedRowKeys: {},
    defaultExpandedRowKeys: {},
    expandedRowRender: {},
    expandRowByClick: {
      type: Boolean
    },
    expandIcon: {},
    onExpand: {},
    onExpandedRowsChange: {},
    defaultExpandAllRows: {
      type: Boolean
    },
    indentSize: {},
    expandIconColumnIndex: {},
    showExpandColumn: {
      type: Boolean
    },
    expandedRowClassName: {},
    childrenColumnName: {},
    rowExpandable: {},
    transformCellText: {},
    rowHoverable: {
      type: Boolean
    },
    rowSelection: {
      type: [Boolean, Object]
    },
    onLoadingChange: {},
    onShownColumnsChange: {},
    pagination: {
      type: [Boolean, Object],
      default: !1
    },
    searchFormWrapperStyle: {},
    tableWrapperStyle: {},
    wrapperStyle: {},
    tableStyle: {},
    adaptive: {
      type: Boolean
    },
    columnResizable: {
      type: Boolean,
      default: !1
    },
    loading: {
      type: Boolean,
      default: void 0
    },
    summary: {},
    rowHeight: {
      default: 66
    },
    getRowHeight: {},
    advanceRenderer: {
      type: Boolean
    },
    browserTooltips: {
      type: Boolean
    },
    virtual: {
      type: [Boolean, Object],
      default: void 0
    },
    onRowClicked: {},
    onScrollEnd: {},
    rowClassName: {},
    rowClassRules: {},
    autoHeight: {
      type: Boolean
    },
    onUpdate: {},
    onModelUpdated: {},
    customRow: {},
    theme: {},
    emptyComponent: {},
    emptyText: {},
    onColumnResized: {},
    animateRows: {
      type: Boolean
    },
    plugins: {},
    rowDrag: {},
    columns: {},
    defaultValue: {},
    onChange: {},
    maxLength: {},
    onFieldChange: {},
    supressForceRowUpdate: {
      type: Boolean,
      default: !1
    },
    label: {},
    name: {},
    formItem: {}
  },
  setup(d, {
    expose: x
  }) {
    const B = Z(), a = d, {
      b: D
    } = be("editable-aggrid"), S = ge("editable-aggrid"), i = g(), {
      internalInstance: s
    } = me();
    function T(e) {
      const o = he(a.columns || []);
      if (!Object.keys(o).length || !e?.length)
        return e;
      let t = [];
      for (const r of e)
        t.push(ce(r, o));
      return t;
    }
    const u = ye(a.name);
    function F() {
      return a.formItem?.initialValue ?? pe(s?.getInitialValues() ?? {}, a.name) ?? [];
    }
    let I = F();
    const n = g([]), E = v(a.name), V = v(u);
    y(() => E.value, (e) => {
      if (R(e, n.value))
        return;
      const o = e ?? ue(I);
      n.value = [...o];
    }, {
      deep: !0,
      flush: "post",
      immediate: !0
    });
    let m;
    y(n, (e) => {
      s?.setFieldValue(a.name, [...e]);
      const o = Ce(e, a.rowKey);
      R(o, l(V)) || (m = () => {
        s?.setFieldValue(u, o);
      });
    }, {
      deep: !0,
      immediate: !0
    });
    const A = ee(() => {
      if (a.rowDrag)
        return {
          ...a.rowDrag,
          onRowDragEnd(e) {
            const o = i.value?._internalGridApi;
            if (o) {
              const t = [];
              o.forEachNodeAfterFilterAndSort((r) => {
                r.data && t.push(r.data);
              }), n.value = t;
            }
            a.rowDrag?.onRowDragEnd?.(e);
          }
        };
    }), N = ve("valueTypeMap"), K = fe(a, N, u), {
      add: j,
      addByKey: k,
      addMultiple: z,
      remove: O,
      removeByKey: U,
      getRowData: G,
      getRowsData: H,
      getRowDataByKey: L,
      setRowData: P,
      setRowDataByKey: _,
      clear: M,
      refreshRowIds: p
    } = we(a, s, n, u);
    function W() {
      if (a.onUpdate?.(), m && (m(), m = void 0), a.supressForceRowUpdate)
        return;
      const e = i.value?._internalGridApi;
      if (!p.value?.length || !e)
        return;
      const o = p.value.map((t) => e.getRowNode(t)).filter((t) => !!t);
      e.refreshCells({
        rowNodes: o,
        force: !0
      }), p.value = [];
    }
    function $(e) {
      n.value = [...e];
    }
    function w(e, o) {
      const t = e === "end" ? n.value.length - 1 : e;
      i.value?.scrollToRow(t, o);
    }
    function q(e, o) {
      i.value?.scrollToColumn(e, o);
    }
    function J(e, o) {
      const t = [u, String(e), o].flat();
      return s?.validateFields([t]);
    }
    function Q(e) {
      const t = l(n).map((r, c) => [u, String(Re(r, c, a.rowKey)), e].flat());
      return s?.validateFields(t);
    }
    function X(e, o = !0) {
      const t = Array.isArray(e) ? e : [e];
      i.value?._internalGridApi?.refreshCells({
        columns: t,
        force: o
      });
    }
    return x({
      add: j,
      addByKey: k,
      addMultiple: z,
      remove: O,
      removeByKey: U,
      getRowData: G,
      getRowsData: H,
      getRowDataByKey: L,
      setRowData: P,
      clear: M,
      setTableData: $,
      scrollTo: w,
      scrollToRow: w,
      scrollToColumn: q,
      validateCell: J,
      validateColumn: Q,
      setRowDataByKey: _,
      table: i,
      refreshColumns: X
    }), (e, o) => (oe(), te(ae, null, [f(l(b).FormItem, {
      hidden: "",
      name: d.name,
      "initial-value": d.formItem?.initialValue,
      transform: T
    }, null, 8, ["name", "initial-value"]), f(l(b).FormItem, h({
      label: d.label
    }, d.formItem), {
      default: C(() => [f(xe, h(l(de)(a, ["name", "maxLength", "onChange", "defaultValue", "formItem", "onUpdate", "rowDrag"]), {
        ref_key: "tableRef",
        ref: i,
        class: [l(D)(), l(S)],
        size: "mini",
        columns: l(K),
        "data-source": n.value,
        "search-form": !1,
        "row-drag": A.value,
        onUpdate: W
      }), ne({
        _: 2
      }, [re(B, (t, r) => ({
        name: r,
        fn: C((c) => [le(e.$slots, r, ie(se(c || {})))])
      }))]), 1040, ["class", "columns", "data-source", "row-drag"])]),
      _: 3
    }, 16, ["label"])], 64));
  }
});
export {
  He as default
};
