import { isFunction as y, isUndefined as d, isNumber as h, cloneDeep as G, isArray as F } from "lodash-unified";
import { shallowRef as S, unref as w } from "vue";
import "../../utils/index.mjs";
import { useDevWarning as W } from "../../utils/warning.mjs";
import { getRowKey as t } from "../editable/utils.mjs";
import { isDef as m } from "../../utils/is.mjs";
let $ = 0;
function K() {
  return `_ag_grid${$++}`;
}
const Q = (u, R, e, x) => {
  const v = S([]), I = W("EditableAgGridFormItem");
  function B(n, r) {
    const s = w(e)?.length || 0;
    if (m(u.maxLength) && s >= u.maxLength || !n && y(u.rowKey))
      return;
    const o = {
      ...n || { [u.rowKey]: K() }
    };
    e.value ? d(r) ? e.value.push(o) : (v.value = e.value.map(
      (i, f) => f < r ? null : t(i, f, u.rowKey)
    ).filter(Boolean), e.value.splice(r, 0, o)) : e.value = [o];
  }
  function L(n, r) {
    const s = w(e)?.length || 0;
    if (m(u.maxLength) && s >= u.maxLength || !n && y(u.rowKey))
      return;
    const o = {
      ...n || { [u.rowKey]: K() }
    };
    if (!e.value) {
      e.value = [o];
      return;
    }
    const i = e.value.findIndex(
      (l, c) => t(l, c, u.rowKey) === r
    ), f = d(r) || i === -1 || i === e.value?.length - 1 ? void 0 : i + 1;
    d(f) ? e.value.push(o) : (v.value = e.value.map(
      (l, c) => c < f ? null : t(l, c, u.rowKey)
    ).filter(Boolean), e.value.splice(f, 0, o));
  }
  function A(n, r = "suffix") {
    if (!n)
      return;
    const s = w(e)?.length || 0, o = h(n) ? n : n.length;
    if (m(u.maxLength) && s + o > u.maxLength || h(n) && y(u.rowKey))
      return;
    const i = h(n) ? new Array(n).fill(0).map(() => ({ [u.rowKey]: K() })) : G(n);
    e.value ? r === "suffix" ? e.value.push(...i) : (v.value = e.value.map(
      (f, l) => t(f, l, u.rowKey)
    ), e.value.unshift(...i)) : e.value = [...i];
  }
  function U(n) {
    const r = F(n) ? n : [n], s = e.value.filter(
      (i) => !r.includes(i)
    ), o = e.value.findIndex(
      (i) => r.includes(i)
    );
    v.value = e.value.map(
      (i, f) => f <= o ? null : t(i, f, u.rowKey)
    ).filter(Boolean), s.length ? e.value = s : e.value.splice(0, e.value.length);
  }
  function j(n) {
    const r = new Set(F(n) ? n : [n]);
    if (r.size === 0)
      return;
    const s = e.value.filter(
      (i, f) => !r.has(t(i, f, u.rowKey))
    ), o = e.value.findIndex(
      (i, f) => r.has(t(i, f, u.rowKey))
    );
    v.value = e.value.map(
      (i, f) => f <= o ? null : t(i, f, u.rowKey)
    ).filter(Boolean), s.length ? e.value = s : e.value.splice(0, e.value.length);
  }
  function O(n) {
    return I(
      !u.supressForceRowUpdate,
      "usage",
      'Avoid using getRowData/setRowData methods while suppressForceRowUpdate is enabled."'
    ), w(e)?.[n];
  }
  function T(n) {
    return e.value?.find(
      (r, s) => t(r, s, u.rowKey) === n
    );
  }
  function _() {
    return w(e);
  }
  function k(n, r, s = !0) {
    I(
      !u.supressForceRowUpdate,
      "usage",
      'Avoid using getRowData/setRowData methods while suppressForceRowUpdate is enabled."'
    ), R?.setFieldValue?.(
      x,
      void 0,
      !1,
      (o, i) => {
        const f = e.value.findIndex(
          (c, g) => n === g
        );
        if (!~f)
          return;
        const l = t(
          e.value[f],
          f,
          u.rowKey
        );
        if (l && !(!o[i] || !o[i][l])) {
          if (!s) {
            const c = Object.keys(o[i][l]);
            for (const g of c)
              g !== u.rowKey && delete o[i][l][g];
          }
          Object.assign(o[i][l], { ...r });
        }
      }
    );
  }
  function E(n, r, s = !0) {
    R?.setFieldValue?.(
      x,
      void 0,
      !1,
      (o, i) => {
        if (!(!o[i] || !o[i][n])) {
          if (!s) {
            const f = Object.keys(o[i][n]);
            for (const l of f)
              l !== u.rowKey && delete o[i][n][l];
          }
          Object.assign(o[i][n], { ...r });
        }
      }
    );
  }
  function z() {
    e.value = [];
  }
  return {
    add: B,
    addByKey: L,
    addMultiple: A,
    remove: U,
    removeByKey: j,
    getRowData: O,
    getRowsData: _,
    setRowDataByKey: E,
    setRowData: k,
    clear: z,
    refreshRowIds: v,
    getRowDataByKey: T
  };
};
export {
  Q as useEditableFormItemApi
};
