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