import { defineComponent as W, useSlots as U, ref as C, watch as w, unref as r, openBlock as $, createBlock as q, withCtx as p, createVNode as b, mergeProps as J, createSlots as Q, renderList as X, renderSlot as Y, normalizeProps as Z, guardReactiveProps as M } from "vue";
import { isEqual as ee, isArray as m, isUndefined as te, isNumber as c, cloneDeep as ae, omit as oe } from "lodash-unified";
import { ApForm as g } from "../../ap-form/index.mjs";
import "../../config-provider/index.mjs";
import "../../hooks/index.mjs";
import "../../utils/index.mjs";
import { useDevWarning as le } from "../../utils/warning.mjs";
import ne from "../hooks/use-editable-columns.mjs";
import "../index.vue2.mjs";
import re from "../style/editable.mjs";
import { useNamespace as se } from "../../config-provider/hooks/use-namespace.mjs";
import { useControllableValue as ie } from "../../hooks/useControllableValue.mjs";
import { useGlobalConfig as ue } from "../../config-provider/hooks/use-global-config.mjs";
import { isDef as v } from "../../utils/is.mjs";
import de from "../index.vue.mjs";
const Ee = /* @__PURE__ */ W({
  name: "EditableGrid",
  __name: "index",
  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: {},
    name: {
      default: "ap-editable-table-inner-name"
    },
    onFieldChange: {}
  },
  emits: ["update:value"],
  setup(h, {
    expose: x,
    emit: R
  }) {
    const B = U(), t = h, {
      b: S
    } = se("editable-grid"), F = re("editable-grid"), E = R;
    le("EditableGrid")(!1, "breaking", "EditableGrid has been EOL, Please use EditableAgGrid(https://aplus.aplnk.com/aplus-ui/components/ag-grid/editable/) instead.");
    const y = C(), {
      value: u,
      updateValue: N
    } = ie(t, E), i = C(), V = ue("valueTypeMap"), f = g.useWatch(t.name, i);
    w(() => f.value, (e) => {
      N(e), t.onChange?.(e);
    }, {
      deep: !0,
      flush: "post"
    }), w(u, (e) => {
      e !== r(f) && (ee(e, r(f)) || i.value?.setFieldValue(t.name, e));
    }, {
      deep: !0
    });
    const k = ne(t, V);
    function L() {
      i.value?.resetFields();
    }
    function D(e) {
      return e ? (m(e) ? e : [e]).map((n) => m(n) ? [t.name, ...n] : [t.name, n]) : void 0;
    }
    async function _(e, l) {
      return (await i.value?.validateFields(D(e), l))?.[t.name];
    }
    function O(e, l) {
      const n = r(u)?.length || 0;
      if (v(t.maxLength) && n >= t.maxLength)
        return;
      const a = {
        ...e || {}
      };
      i.value?.setFieldValue?.(t.name, a, !0, (o, s) => {
        if (!o[s]) {
          o[s] = [a];
          return;
        }
        te(l) ? o[s].push(a) : o[s].splice(l, 0, a);
      });
    }
    function T(e, l = "suffix") {
      const n = r(u)?.length || 0, a = c(e) ? e : e.length;
      if (v(t.maxLength) && n + a > t.maxLength)
        return;
      const o = c(e) ? new Array(e).fill(0).map(() => ({})) : ae(e);
      i.value?.setFieldValue?.(t.name, o, !0, (s, d) => {
        if (!s[d]) {
          s[d] = [...o];
          return;
        }
        l === "suffix" ? s[d].push(...o) : s[d].unshift(...o);
      });
    }
    function j(e) {
      const l = new Set(c(e) ? [e] : e);
      i.value?.setFieldValue?.(t.name, void 0, !0, (n, a) => {
        n[a] = n[a].filter((o, s) => !l.has(s));
      });
    }
    function A(e) {
      const l = new Set(m(e) ? e : [e]);
      l.size !== 0 && i.value?.setFieldValue?.(t.name, void 0, !0, (n, a) => {
        n[a] = n[a].filter((o) => !l.has(o[t.rowKey]));
      });
    }
    function z() {
      return r(u);
    }
    function G(e) {
      i.value?.setFieldValue(t.name, e);
    }
    function H(e) {
      return r(u)?.[e];
    }
    function I(e, l, n = !0) {
      i.value?.setFieldValue?.(t.name, void 0, !0, (a, o) => {
        if (!(!a[o] || !a[o][e])) {
          if (!n) {
            const s = Object.keys(a[o][e]);
            for (const d of s)
              d !== t.rowKey && delete a[o][e][d];
          }
          Object.assign(a[o][e], {
            ...l
          });
        }
      });
    }
    function K(e) {
      const l = e === "end" ? u.value.length - 1 : e;
      y.value?.scrollToRow(l);
    }
    function P() {
      i.value?.setFieldValue?.(t.name, []);
    }
    return x({
      resetFields: L,
      validateFields: _,
      add: O,
      remove: j,
      getRowData: H,
      getRowsData: z,
      setRowData: I,
      addMultiple: T,
      scrollTo: K,
      clear: P,
      setTableData: G,
      removeByKey: A
    }), (e, l) => ($(), q(r(g), {
      ref_key: "formRef",
      ref: i,
      "initial-values": {
        [t.name]: r(u)
      },
      style: {
        height: "100%"
      }
    }, {
      default: p(() => [b(r(g).FormItem, {
        name: h.name,
        "no-style": ""
      }, {
        default: p(() => [b(de, J(r(oe)(t, ["name", "value", "onUpdate:value", "maxLength", "onChange", "defaultValue"]), {
          ref_key: "tableRef",
          ref: y,
          class: [r(S)(), r(F)],
          columns: r(k),
          "data-source": r(u),
          "search-form": !1
        }), Q({
          _: 2
        }, [X(B, (n, a) => ({
          name: a,
          fn: p((o) => [Y(e.$slots, a, Z(M(o || {})))])
        }))]), 1040, ["class", "columns", "data-source"])]),
        _: 3
      }, 8, ["name"])]),
      _: 3
    }, 8, ["initial-values"]));
  }
});
export {
  Ee as default
};
