import { j as L } from "./jsx-runtime-tc70JA_2.js";
import { record as P, xy as T } from "@synnaxlabs/x";
import h, { memo as H, useRef as x, useCallback as i, useMemo as _, useId as O } from "react";
import { z as d } from "zod";
import { c as Z } from "./Text-2MISZsc5.js";
const b = d.object({
  key: d.string().or(d.number()),
  type: d.string(),
  elementID: d.string().optional(),
  data: P.unknownZ.optional()
}), w = d.object({ source: b, items: d.array(b) }), F = { key: "", type: "" }, p = { source: F, items: [] }, R = "file", G = { key: "file", type: R }, M = /* @__PURE__ */ new Set(["all", "copyLink"]), N = (t, e) => M.has(t.dataTransfer.effectAllowed) && e.items.length === 0, [Y, m] = Z({
  defaultValue: null,
  displayName: "Haul.Context"
}), S = {
  ...p,
  onSuccessfulDrop: () => {
  }
}, A = H(
  ({
    children: t,
    useState: e = h.useState,
    onDropOutside: n
  }) => {
    const o = m(), [l, r] = e(p), c = x(S), a = x(/* @__PURE__ */ new Set()), f = i(
      (s, u, D) => {
        c.current = { source: s, items: u, onSuccessfulDrop: D }, r({ source: s, items: u });
      },
      [r, n]
    ), g = i(
      ({ target: s, dropped: u }) => {
        const D = c.current.items;
        c.current.onSuccessfulDrop?.({ target: s, dropped: u, hauled: D }), c.current = S, r(p);
      },
      [r]
    ), y = i(
      (s) => {
        let u = null;
        a.current.forEach((D) => {
          u == null && (u = D(c.current, s));
        }), u != null && g(u), c.current = S, r(p);
      },
      [r]
    ), E = i((s) => (a.current.add(s), () => a.current.delete(s)), []), k = _(
      () => o ?? { state: l, start: f, end: y, drop: g, bind: E },
      [l, f, y, g, o]
    );
    return /* @__PURE__ */ L.jsx(Y, { value: k, children: t });
  }
);
A.displayName = "HaulProvider";
const j = () => {
  const t = x(p), e = m();
  return e == null || (t.current = e.state), t;
}, z = () => {
  const t = m();
  return t == null ? p : t.state;
}, v = ({ type: t, key: e }) => {
  const n = e ?? O(), o = _(() => ({ key: n, type: t }), [n, t]), l = m();
  if (l == null) return { startDrag: () => {
  }, onDragEnd: () => {
  } };
  const { start: r, end: c } = l;
  return {
    startDrag: i((a, f) => r(o, a, f), [r, o]),
    onDragEnd: (a) => c(T.construct({ x: a.screenX, y: a.screenY }))
  };
}, C = ({
  type: t,
  key: e,
  canDrop: n,
  onDrop: o,
  onDragOver: l
}) => {
  const r = j(), c = m();
  if (c == null) return { onDragOver: () => {
  }, onDrop: () => {
  } };
  const { drop: a } = c, f = e ?? O(), g = _(() => ({ key: f, type: t }), [f, t]), y = x({ x: -1, y: -1 }), E = i(
    (s) => {
      s.preventDefault();
      const u = T.construct({ x: s.screenX, y: s.screenY });
      T.equals(u, y.current) || (y.current = u, n(r.current) && l?.({ event: s, ...r.current }));
    },
    [r, n]
  ), k = i(
    (s) => {
      n(r.current) && (s.preventDefault(), a({ target: g, dropped: o({ ...r.current, event: s }) }));
    },
    [r, o, n, a, g]
  );
  return { onDragOver: E, onDrop: k };
}, B = ({
  type: t,
  key: e,
  ...n
}) => {
  const o = e ?? O(), l = _(() => ({ key: o, type: t }), [o, t]), r = v(l), c = C({ ...n, ...l });
  return { ...r, ...c };
}, X = (t) => ({ items: e }) => e.some((n) => n.type === t), I = (t, e) => e.filter((n) => n.type === t), q = (t, e, n) => i(
  (o) => e({ ...o, items: I(t, o.items) }),
  n
), Q = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
  __proto__: null,
  FILE: G,
  FILE_TYPE: R,
  Provider: A,
  ZERO_DRAGGING_STATE: p,
  ZERO_ITEM: F,
  canDropOfType: X,
  draggingStateZ: w,
  filterByType: I,
  isFileDrag: N,
  itemZ: b,
  useContext: m,
  useDrag: v,
  useDragAndDrop: B,
  useDraggingRef: j,
  useDraggingState: z,
  useDrop: C,
  useFilterByTypeCallback: q
}, Symbol.toStringTag, { value: "Module" }));
export {
  R as F,
  Q as H,
  A as P,
  B as a,
  q as b,
  C as c,
  X as d,
  I as f,
  z as u
};
