import { defineComponent as X, useSlots as Y, ref as C, watch as Z, unref as l, openBlock as ee, createBlock as oe, withCtx as c, createVNode as b, mergeProps as ae, createSlots as te, renderList as ne, renderSlot as le, normalizeProps as re, guardReactiveProps as se } from "vue";
import { isEqual as ie, isArray as v, omit as de } from "lodash-unified";
import { ApForm as f } from "../../ap-form/index.mjs";
import "../../config-provider/index.mjs";
import "../../hooks/index.mjs";
import { useEditableApi as ue } from "../hooks/use-editable-api.mjs";
import pe from "../hooks/use-editable-columns.mjs";
import "../index.vue2.mjs";
import me from "../style/editable.mjs";
import { arrayToObject as x, getRowKey as ce } from "./utils.mjs";
import { useNamespace as fe } from "../../config-provider/hooks/use-namespace.mjs";
import { useControllableValue as ye } from "../../hooks/useControllableValue.mjs";
import { useGlobalConfig as we } from "../../config-provider/hooks/use-global-config.mjs";
import ge from "../index.vue.mjs";
const ke = /* @__PURE__ */ X({
  name: "EditableGrid",
  __name: "index",
  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: {},
    value: {},
    defaultValue: {},
    "onUpdate:value": {},
    onChange: {},
    maxLength: {},
    name: {
      default: "ap-editable-table-inner-name"
    },
    onFieldChange: {},
    supressForceRowUpdate: {
      type: Boolean,
      default: !1
    }
  },
  emits: ["update:value"],
  setup(y, {
    expose: B,
    emit: S
  }) {
    const T = Y(), t = y, {
      b: F
    } = fe("editable-aggrid"), A = me("editable-aggrid"), _ = S, s = C(), {
      value: i,
      updateValue: w
    } = ye(t, _), r = C(), E = f.useWatch(t.name, r);
    let d;
    Z(i, (e) => {
      const o = x(e, t.rowKey);
      ie(o, l(E)) || (d = () => {
        r.value?.setFieldValue(t.name, o);
      });
    }, {
      deep: !0
    });
    const K = we("valueTypeMap"), k = pe(t, K), {
      add: D,
      addByKey: N,
      addMultiple: U,
      remove: I,
      removeByKey: j,
      getRowData: z,
      getRowsData: V,
      getRowDataByKey: H,
      setRowData: O,
      setRowDataByKey: G,
      clear: L,
      refreshRowIds: u
    } = ue(t, r, i, w);
    function P() {
      if (t.onUpdate?.(), d && (d(), d = void 0), t.supressForceRowUpdate)
        return;
      const e = s.value?._internalGridApi;
      if (!u.value?.length || !e)
        return;
      const o = u.value.map((a) => e.getRowNode(a)).filter((a) => !!a);
      e.refreshCells({
        rowNodes: o,
        force: !0
      }), u.value = [];
    }
    function M() {
      r.value?.resetFields();
    }
    function g(e) {
      return e ? (v(e) ? e : [e]).map((a) => v(a) ? [t.name, ...a] : [t.name, a]) : void 0;
    }
    function R(e) {
      try {
        const o = Object.keys(e), a = i.value.map((n) => ce(n, void 0, t.rowKey));
        return o.sort((n, p) => a.findIndex((m) => String(m) === n) - a.findIndex((m) => String(m) === p)), o.map((n) => e[n]);
      } catch {
        return e;
      }
    }
    async function W(e, o) {
      const n = (await r.value?.validateFields(g(e), o))?.[t.name];
      return R(n);
    }
    async function $(e, o) {
      const n = (await r.value?.validateFieldsReturnTransformed(g(e), o))?.[t.name];
      return R(n);
    }
    function q(e) {
      w(e);
    }
    function h(e, o) {
      const a = e === "end" ? i.value.length - 1 : e;
      s.value?.scrollToRow(a, o);
    }
    function J(e, o) {
      s.value?.scrollToColumn(e, o);
    }
    function Q(e, o = !0) {
      const a = Array.isArray(e) ? e : [e];
      s.value?._internalGridApi?.refreshCells({
        columns: a,
        force: o
      });
    }
    return B({
      resetFields: M,
      validateFields: W,
      validateFieldsReturnTransformed: $,
      add: D,
      addByKey: N,
      addMultiple: U,
      remove: I,
      removeByKey: j,
      getRowData: z,
      getRowsData: V,
      setRowData: O,
      clear: L,
      setTableData: q,
      scrollTo: h,
      scrollToRow: h,
      scrollToColumn: J,
      table: s,
      getRowDataByKey: H,
      setRowDataByKey: G,
      refreshColumns: Q
    }), (e, o) => (ee(), oe(l(f), {
      ref_key: "formRef",
      ref: r,
      "initial-values": {
        [t.name]: l(x)(l(i), t.rowKey)
      },
      style: {
        height: "100%"
      }
    }, {
      default: c(() => [b(l(f).FormItem, {
        name: y.name,
        "no-style": ""
      }, {
        default: c(() => [b(ge, ae(l(de)(t, ["name", "value", "onUpdate:value", "maxLength", "onChange", "defaultValue", "onUpdate"]), {
          ref_key: "tableRef",
          ref: s,
          class: [l(F)(), l(A)],
          size: "mini",
          columns: l(k),
          "data-source": l(i),
          "search-form": !1,
          onUpdate: P
        }), te({
          _: 2
        }, [ne(T, (a, n) => ({
          name: n,
          fn: c((p) => [le(e.$slots, n, re(se(p || {})))])
        }))]), 1040, ["class", "columns", "data-source"])]),
        _: 3
      }, 8, ["name"])]),
      _: 3
    }, 8, ["initial-values"]));
  }
});
export {
  ke as default
};
