import { defineComponent as X, useSlots as Y, ref as w, watch as g, unref as l, openBlock as Z, createElementBlock as ee, Fragment as oe, createVNode as f, mergeProps as h, withCtx as C, createSlots as te, renderList as ae, renderSlot as ne, normalizeProps as le, guardReactiveProps as re } from "vue";
import { isEqual as b, cloneDeep as ie, omit as se } from "lodash-unified";
import { ApForm as R } from "../../ap-form/index.mjs";
import { useInjectForm as me } from "../../ap-form/context.mjs";
import x from "../../ap-form/hooks/use-watch.mjs";
import { getModelValue as ue } from "../../ap-form/utils/get.mjs";
import { transformValues as de } from "../../ap-form/utils/transform.mjs";
import "../../config-provider/index.mjs";
import pe from "../hooks/use-editable-columns.mjs";
import { useEditableFormItemApi as ce } from "../hooks/use-editable-form-item-api.mjs";
import "../index.vue2.mjs";
import fe from "../style/editable.mjs";
import { getShadowName as ye } from "../utils.mjs";
import { getColumnTransformOptions as we, arrayToObject as ge, getRowKey as he } from "./utils.mjs";
import { useNamespace as Ce } from "../../config-provider/hooks/use-namespace.mjs";
import { useGlobalConfig as be } from "../../config-provider/hooks/use-global-config.mjs";
import Re from "../index.vue.mjs";
const Ue = /* @__PURE__ */ X({
  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: {},
    columns: {},
    defaultValue: {},
    onChange: {},
    maxLength: {},
    onFieldChange: {},
    supressForceRowUpdate: {
      type: Boolean,
      default: !1
    },
    label: {},
    name: {},
    formItem: {}
  },
  setup(u, {
    expose: B
  }) {
    const v = Y(), a = u, {
      b: T
    } = Ce("editable-aggrid"), S = fe("editable-aggrid"), r = w(), {
      internalInstance: i
    } = me();
    function I(e) {
      const o = we(a.columns || []);
      if (!Object.keys(o).length || !e?.length)
        return e;
      let t = [];
      for (const m of e)
        t.push(de(m, o));
      return t;
    }
    const s = ye(a.name);
    function F() {
      return a.formItem?.initialValue ?? ue(i?.getInitialValues() ?? {}, a.name) ?? [];
    }
    let V = F();
    const n = w([]), E = x(a.name), D = x(s);
    g(() => E.value, (e) => {
      if (b(e, n.value))
        return;
      const o = e ?? ie(V);
      n.value = [...o];
    }, {
      deep: !0,
      flush: "post",
      immediate: !0
    });
    let d;
    g(n, (e) => {
      const o = ge(e, a.rowKey);
      b(o, l(D)) || (d = () => {
        i?.setFieldValue(a.name, [...e]), i?.setFieldValue(s, o);
      });
    }, {
      deep: !0,
      immediate: !0
    });
    const K = be("valueTypeMap"), N = pe(a, K, s), {
      add: A,
      addByKey: j,
      addMultiple: k,
      remove: z,
      removeByKey: O,
      getRowData: U,
      getRowsData: H,
      getRowDataByKey: L,
      setRowData: P,
      setRowDataByKey: G,
      clear: M,
      refreshRowIds: p
    } = ce(a, i, n, s);
    function W() {
      if (a.onUpdate?.(), d && (d(), d = void 0), a.supressForceRowUpdate)
        return;
      const e = r.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 y(e, o) {
      const t = e === "end" ? n.value.length - 1 : e;
      r.value?.scrollToRow(t, o);
    }
    function $(e, o) {
      r.value?.scrollToColumn(e, o);
    }
    function q(e, o) {
      const t = [s, String(e), o].flat();
      return i?.validateFields([t]);
    }
    function J(e) {
      const t = l(n).map((m, c) => [s, String(he(m, c, a.rowKey)), e].flat());
      return i?.validateFields(t);
    }
    function Q(e, o = !0) {
      const t = Array.isArray(e) ? e : [e];
      r.value?._internalGridApi?.refreshCells({
        columns: t,
        force: o
      });
    }
    return B({
      add: A,
      addByKey: j,
      addMultiple: k,
      remove: z,
      removeByKey: O,
      getRowData: U,
      getRowsData: H,
      getRowDataByKey: L,
      setRowData: P,
      clear: M,
      setTableData: _,
      scrollTo: y,
      scrollToRow: y,
      scrollToColumn: $,
      validateCell: q,
      validateColumn: J,
      setRowDataByKey: G,
      table: r,
      refreshColumns: Q
    }), (e, o) => (Z(), ee(oe, null, [f(l(R).FormItem, {
      hidden: "",
      name: u.name,
      "initial-value": u.formItem?.initialValue,
      transform: I
    }, null, 8, ["name", "initial-value"]), f(l(R).FormItem, h({
      label: u.label
    }, u.formItem), {
      default: C(() => [f(Re, h(l(se)(a, ["name", "maxLength", "onChange", "defaultValue", "formItem", "onUpdate"]), {
        ref_key: "tableRef",
        ref: r,
        class: [l(T)(), l(S)],
        size: "mini",
        columns: l(N),
        "data-source": n.value,
        "search-form": !1,
        onUpdate: W
      }), te({
        _: 2
      }, [ae(v, (t, m) => ({
        name: m,
        fn: C((c) => [ne(e.$slots, m, le(re(c || {})))])
      }))]), 1040, ["class", "columns", "data-source"])]),
      _: 3
    }, 16, ["label"])], 64));
  }
});
export {
  Ue as default
};
