import { c as x, j as m } from "./css-D6ZBcSwh.js";
import { useCallback as b, useEffect as q, useState as C } from "react";
import { u as H } from "./Editable-CxgdRDPb.js";
import { p as k, u as M, a as j, f as d, d as y } from "./Text-BzTkWLeU.js";
import { q as v } from "./deep-DRVLw7iw.js";
const R = ({
  triggers: n,
  callback: u,
  region: a,
  loose: e,
  double: r,
  regionMustBeElement: l
}) => {
  const { listen: t } = M();
  let i;
  n != null && n?.length > 0 && typeof n[0] == "string" ? i = [n] : i = n;
  const g = j(
    () => i,
    ([s], [c]) => s == null && c == null ? !0 : s == null || c == null ? !1 : v.primitiveArrays(s.flat(), c.flat()) === v.EQUAL,
    [i]
  );
  q(() => {
    if (!(g == null || g.length === 0))
      return t((s) => {
        const c = d(g, s.prev, { loose: e, double: r }), T = d(g, s.next, { loose: e, double: r }), h = y(T, c);
        let f = h[0];
        const o = h[1];
        if (f.length === 0 && o.length === 0) return;
        f = A(s.target, s.cursor, f, a, l);
        const p = { target: s.target, cursor: s.cursor };
        f.length > 0 && u?.({ ...p, stage: "start", triggers: f, prevTriggers: s.prev }), o.length > 0 && u?.({ ...p, stage: "end", triggers: o, prevTriggers: s.prev });
      });
  }, [u, g, t, e, a, r, l]);
}, A = (n, u, a, e, r) => {
  if (e == null) return a;
  if (e.current == null) return [];
  const l = m.construct(e.current);
  return a.filter((t) => r ?? t.some((g) => g.includes("Mouse")) ? m.contains(l, u) && n === e.current : m.contains(l, u));
}, U = ({
  triggers: n,
  loose: u
}) => {
  const [a, e] = H({
    triggers: [],
    held: !1
  });
  return R({
    triggers: n,
    callback: b((r) => {
      e((l) => {
        let t;
        return r.stage === "start" ? t = x.unique([...l.triggers, ...r.triggers]) : t = k(l.triggers, r.triggers), { triggers: t, held: t.length > 0 };
      });
    }, []),
    loose: u
  }), a;
}, w = ({ triggers: n, loose: u }) => {
  const [a, e] = C({ triggers: [], held: !1 });
  return R({
    triggers: n,
    callback: b((r) => {
      e((l) => {
        let t;
        return r.stage === "start" ? t = x.unique([...l.triggers, ...r.triggers]) : t = k(l.triggers, r.triggers), { triggers: t, held: t.length > 0 };
      });
    }, []),
    loose: u
  }), a;
};
export {
  w as a,
  U as b,
  R as u
};
