import { unref as d, computed as b, ref as P } from "vue";
import { getPtg as T } from "./useSizes.mjs";
const J = (j, k, y, L, v, R) => {
  const a = d(j).map((e) => [e.min, e.max]), C = b(() => d(L) || 0), O = (e) => e * C.value;
  function l(e, f) {
    return typeof e == "string" ? O(T(e)) : e ?? f;
  }
  const I = P([]), E = [], m = P(null), U = () => d(y).map(O);
  return [
    (e) => {
      I.value = U(), m.value = {
        index: e,
        confirmed: !1
      };
    },
    (e, f) => {
      let n = null;
      const S = d(m), i = d(I), r = d(C);
      if ((!S || !S.confirmed) && f !== 0) {
        if (f > 0)
          n = e, m.value = {
            index: e,
            confirmed: !0
          };
        else
          for (let u = e; u >= 0; u -= 1)
            if (i[u] > 0 && d(k)[u].resizable) {
              n = u, m.value = {
                index: u,
                confirmed: !0
              };
              break;
            }
      }
      const o = n ?? S?.index ?? e, t = [...i], c = o + 1, z = l(a[o][0], 0), g = l(a[c][0], 0), p = l(
        a[o][1],
        r
      ), h = l(
        a[c][1],
        r
      );
      let s = f;
      return t[o] + s < z && (s = z - t[o]), t[c] - s < g && (s = t[c] - g), t[o] + s > p && (s = p - t[o]), t[c] - s > h && (s = t[c] - h), t[o] += s, t[c] -= s, v(t), t;
    },
    () => {
      m.value = null;
    },
    (e, f) => {
      const n = U(), S = R ? f === "start" ? "end" : "start" : f, i = S === "start" ? e : e + 1, r = S === "start" ? e + 1 : e, o = n[i], t = n[r];
      if (o !== 0 && t !== 0)
        n[i] = 0, n[r] += o, E[e] = o;
      else {
        const c = d(C), z = o + t, g = l(a[i][0], 0), p = l(
          a[i][1],
          c
        ), h = l(a[r][0], 0), s = l(
          a[r][1],
          c
        ), u = Math.max(g, z - s), _ = (Math.min(p, z - h) - u) / 2, x = E[e], M = z - x;
        x && x <= s && x >= h && M <= p && M >= g ? (n[r] = x, n[i] = M) : (n[i] -= _, n[r] += _);
      }
      return v(n), n;
    },
    b(() => m.value?.index)
  ];
};
export {
  J as useResize
};
