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