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