import { j as a } from "./jsx-runtime-OVHDjVDe.js";
import { useCallback as S, useEffect as _, useRef as P, createContext as W, useMemo as L, useContext as Ce, useSyncExternalStore as re, useState as H, use as ne, memo as Ge } from "react";
import { m as k, h as M, c as Ke, C as I, D as Ue, s as q, y as Ze } from "./css-D6ZBcSwh.js";
import { D as qe } from "./Dialog-CO4iLhHf.js";
import { u as ce } from "./Animated-M9ynNzcI.js";
import { B as Oe } from "./Button-BFoGC4WW.js";
import { o as U, F as We, y as Xe, S as Ye, L as ze, D as Je } from "./Text-BzTkWLeU.js";
import { d as Qe, u as oe, F as et, a as tt, I as st } from "./Items-G25zb57I.js";
import { c as w, d as rt, b as nt, f as ct, T as $ } from "./Editable-CxgdRDPb.js";
import { c as ot, C as Z } from "./types-BNX_Jj6k.js";
import { c as lt, C as ut } from "./Provider--RxgAoVG.js";
import { m as it, n as at, A as dt, o as ft } from "./provider-C8In649C.js";
import { E as mt, a as X } from "./Box-D5Qv__kr.js";
import "react-dom";
import { u as xt, b as se } from "./hooks-15yB7IKE.js";
const le = ["ArrowUp"], ue = ["ArrowDown"], ie = ["Enter"], ht = [le, ue, ie], pt = k.milliseconds(200).milliseconds, vt = k.milliseconds(100).milliseconds, ae = ({
  data: e,
  initialHover: s = -1,
  onSelect: t
}) => {
  const r = w(e), [l, o, c] = rt(s), { visible: n } = ce(), d = w(n), { scrollToIndex: f } = Qe(), m = S(
    (u) => {
      const i = c.current, h = ot(u, i);
      o(h), f(h, i > h ? "bottom" : "top");
    },
    [f]
  );
  _(() => {
    c.current >= e.length && m(0);
  }, [e.length]);
  const x = P(null), v = S(
    ({ triggers: u, stage: i }) => {
      if (!d.current || (x.current != null && (clearInterval(x.current), x.current = null), i !== "start")) return;
      const h = r.current;
      if (U(u, [ie])) {
        if (c.current === -1) return;
        t?.(h[c.current]);
        return;
      }
      const E = () => {
        const p = r.current;
        U(u, [le], { loose: !0 }) ? m((j) => j <= 0 ? p.length - 1 : j - 1) : U(u, [ue], { loose: !0 }) && m((j) => j >= p.length - 1 ? 0 : j + 1);
      };
      E(), x.current = setTimeout(() => {
        x.current = setInterval(E, vt);
      }, pt);
    },
    [t]
  );
  return xt({
    triggers: ht,
    callback: v,
    loose: !0
  }), { hover: e[l] };
}, gt = ({
  allowNone: e = !1,
  onChange: s,
  value: t,
  closeDialogOnSelect: r = !1,
  initialHover: l,
  autoSelectOnNone: o = !1
}) => {
  const c = w(t), { data: n } = oe(), { close: d } = ce(), f = w(n);
  _(() => {
    const i = t != null && n.includes(t);
    o && n.length > 0 && !i && s(n[0], { clicked: n[0], clickedIndex: 0 });
  }, [o, s, t, n.length, n]);
  const m = S(
    (i) => {
      if (c.current === i) {
        e && s(null, { clicked: null, clickedIndex: null }), r && d();
        return;
      }
      const h = f.current.findIndex((E) => E === i);
      s(i, { clicked: i, clickedIndex: h }), r && d();
    },
    [f, s, d]
  ), x = S(() => {
    e && s(null, { clicked: null, clickedIndex: null });
  }, [s, e]), v = S(
    (i) => s(i[0], { clicked: null, clickedIndex: null }),
    [s]
  ), u = ae({ data: n, onSelect: m, initialHover: l });
  return { onSelect: m, setSelected: v, clear: x, ...u };
}, St = ({
  value: e = [],
  replaceOnSingle: s = !1,
  onChange: t,
  initialHover: r,
  allowNone: l = !0,
  closeDialogOnSelect: o = !1,
  autoSelectOnNone: c = !1
}) => {
  const { data: n } = oe(), d = P(null), f = se({ triggers: [["Shift"]], loose: !0 }), m = se({ triggers: [["Control"]], loose: !0 }), x = w(e), v = w(n);
  _(() => {
    const p = n.some((j) => e.includes(j));
    c && n.length > 0 && !p && t([n[0]], { clicked: n[0], clickedIndex: 0 });
  }, [c, t, e, n.length, n]);
  const u = S(
    (p) => {
      const j = d.current, D = v.current;
      let T = [];
      const g = M.toArray(x.current).filter((A) => A != null);
      if (m.current.held && s)
        g.includes(p) ? T = g.filter((A) => A !== p) : T = [...g, p];
      else if (f.current.held && j !== null) {
        const [A, V] = [
          D.findIndex((b) => b === p),
          D.findIndex((b) => b === j)
        ].sort((b, B) => b - B), N = D.slice(A, V + 1).map((b) => b);
        N.slice(1, N.length - 1).every((b) => g.includes(b)) && g.includes(p) ? T = g.filter((b) => !N.includes(b)) : T = [...g, ...N], d.current = null;
      } else
        d.current = p, s ? T = g.includes(p) && g.length === 1 ? [] : [p] : g.includes(p) ? T = g.filter((A) => A !== p) : T = [...g, p];
      const F = Ke.unique(T);
      if (F.length === 0) {
        if (!l) return;
        d.current = null;
      }
      t(F, {
        clicked: p,
        clickedIndex: D.findIndex((A) => A === p)
      }), o && close();
    },
    [x, v, t]
  ), i = S(
    () => t([], { clicked: null, clickedIndex: 0 }),
    [t]
  ), h = S(
    (p) => t(p, { clicked: null, clickedIndex: 0 }),
    [t]
  ), E = ae({ data: n, onSelect: u, initialHover: r });
  return { onSelect: u, setSelected: h, clear: i, ...E };
}, Et = () => {
  const e = nt(() => /* @__PURE__ */ new Map()), s = S((r) => {
    e.current.forEach((l, o) => {
      (l == null || r.includes(l)) && o();
    });
  }, []), t = S((r, l) => {
    if (e.current.has(r)) {
      const o = e.current.get(r);
      console.warn(
        `[store] attempted to subscribe listener with key ${o} to key ${l} without being unsubscribed first`
      );
    }
    return e.current.set(r, l), () => e.current.delete(r);
  }, []);
  return { notifyListeners: s, subscribe: t };
}, de = W({
  getState: () => ({ value: void 0, hover: void 0 }),
  onSelect: () => {
  },
  setSelected: () => {
  },
  clear: () => {
  },
  subscribe: () => () => {
  }
}), Rt = (e, s) => e == null ? !1 : Array.isArray(e) ? e.includes(s) : e === s, It = ({
  children: e,
  ...s
}) => {
  const { value: t } = s, r = St(s);
  return /* @__PURE__ */ a.jsx(fe, { value: t, ...r, children: e });
}, bt = ({
  children: e,
  ...s
}) => {
  const { value: t } = s, r = gt(s);
  return /* @__PURE__ */ a.jsx(fe, { value: t, ...r, children: e });
}, fe = ({
  value: e,
  onSelect: s,
  clear: t,
  setSelected: r,
  children: l,
  hover: o
}) => {
  const c = P(M.toArray(e)), n = w(o), { notifyListeners: d, subscribe: f } = Et(), m = S(
    () => ({ value: c.current, hover: n.current }),
    []
  ), x = L(
    () => ({
      onSelect: s,
      setSelected: r,
      clear: t,
      hover: o,
      subscribe: f,
      getState: m
    }),
    [m, s, r, t, o, f]
  );
  return _(() => {
    const v = c.current, u = M.toArray(e), i = [...v, ...u];
    c.current = u, d(i);
  }, [e, d]), /* @__PURE__ */ a.jsx(de.Provider, { value: x, children: l });
}, Y = () => Ce(de), jt = (e) => {
  const { getState: s, onSelect: t, subscribe: r } = Y(), l = S(() => t(e), [e, t]), o = re(
    S((c) => r(c, e), [e, r]),
    S(() => {
      const c = s(), n = Rt(c.value, e), d = c.hover === e;
      return n && d ? "selected-hovered" : n ? "selected" : d ? "hovered" : "none";
    }, [e, s])
  );
  return L(
    () => ({
      selected: o === "selected" || o === "selected-hovered",
      hovered: o === "hovered" || o === "selected-hovered",
      onSelect: l
    }),
    [o, l]
  );
}, Wt = () => {
  const { getState: e, subscribe: s } = Y(), t = re(s, () => e().value);
  return L(() => t == null ? [] : M.toArray(t), [t]);
}, Xt = () => Y().clear, At = ({
  data: e,
  getItem: s,
  subscribe: t,
  itemHeight: r,
  multiple: l,
  onFetchMore: o,
  virtual: c = !1,
  value: n,
  onChange: d,
  ...f
}) => /* @__PURE__ */ a.jsx(
  et,
  {
    data: e,
    getItem: s,
    subscribe: t,
    onFetchMore: o,
    itemHeight: r,
    virtual: c,
    children: l ? /* @__PURE__ */ a.jsx(It, { value: n, onChange: d, ...f }) : /* @__PURE__ */ a.jsx(bt, { value: n, onChange: d, ...f })
  }
);
At.displayName = "Select.Frame";
const Tt = ({
  size: e = "medium",
  ref: s,
  value: t,
  onChange: r,
  className: l,
  onFocus: o,
  onKeyDown: c,
  selectOnFocus: n = !1,
  centerPlaceholder: d = !1,
  placeholder: f,
  variant: m = "outlined",
  level: x,
  onBlur: v,
  disabled: u,
  resetOnBlurIfEmpty: i = !1,
  status: h,
  weight: E,
  style: p,
  contrast: j,
  color: D,
  sharp: T,
  onlyChangeOnBlur: g = !1,
  endContent: F,
  full: A,
  children: V,
  grow: N,
  shrink: b,
  borderColor: B,
  borderWidth: ve,
  bordered: ge,
  rounded: Se,
  tabIndex: C,
  trigger: Ee,
  triggerIndicator: Re,
  textColor: Ie,
  textVariant: be,
  preventClick: je,
  onClickDelay: Ae,
  startContent: z,
  tooltip: Te,
  tooltipDelay: we,
  tooltipLocation: Le,
  hideTooltip: De,
  ghost: Ne,
  area: Pe,
  ...$e
}) => {
  const J = P(t), [y, G] = H(null), Q = P(null), K = P(!1), Fe = (R) => {
    K.current = !1, i && R.target.value === "" ? r?.(J.current) : g && y != null && r?.(y), G(null), v?.(R);
  }, Ve = (R) => {
    u || (g ? G(R.target.value) : r?.(R.target.value));
  }, _e = (R) => {
    g && G(t), o?.(R), J.current = R.target.value;
  }, ye = () => {
    !n || K.current || (K.current = !0, Q.current?.select());
  }, He = (R) => {
    g && R.key === "Enter" ? R.currentTarget.blur() : R.key === "Escape" && (R.currentTarget.blur(), R.stopPropagation()), c?.(R);
  }, Me = ct(s, Q), ke = (t == null || t.length === 0) && y == null && f != null && typeof f != "string";
  C ??= m === "preview" ? -1 : void 0;
  const ee = {
    style: p,
    full: A,
    grow: N,
    shrink: b
  }, Be = V != null ? {} : ee, te = /* @__PURE__ */ a.jsxs(
    Oe,
    {
      el: "div",
      x: !0,
      empty: !0,
      align: "center",
      className: I(
        I.B("input"),
        I.disabled(u),
        h != null && I.M(h),
        l
      ),
      size: e,
      level: x,
      color: D,
      contrast: j,
      sharp: T,
      status: h,
      bordered: ge,
      borderColor: B,
      borderWidth: ve,
      pack: !0,
      variant: m,
      rounded: Se,
      tabIndex: C,
      trigger: Ee,
      triggerIndicator: Re,
      textColor: Ie,
      textVariant: be,
      preventClick: je,
      onClickDelay: Ae,
      tooltip: Te,
      tooltipDelay: we,
      tooltipLocation: Le,
      hideTooltip: De,
      ghost: Ne,
      ...Be,
      children: [
        ke && /* @__PURE__ */ a.jsx(
          $,
          {
            className: I(
              I.visible(!1),
              I.BE("input", "placeholder"),
              d && I.M("centered")
            ),
            level: x ?? Z[e],
            children: f
          }
        ),
        z != null && /* @__PURE__ */ a.jsx(
          $,
          {
            className: I.BE("input", "start-content"),
            level: x ?? Z[e],
            children: z
          }
        ),
        /* @__PURE__ */ a.jsx(
          mt,
          {
            el: Pe ? "textarea" : "input",
            ref: Me,
            value: y ?? t,
            role: "textbox",
            onChange: Ve,
            autoCapitalize: "off",
            autoComplete: "off",
            autoCorrect: "off",
            onFocus: _e,
            onKeyDown: He,
            tabIndex: C,
            onMouseUp: ye,
            onBlur: Fe,
            disabled: u,
            placeholder: typeof f == "string" ? f : void 0,
            style: { fontWeight: E },
            ...$e
          }
        ),
        F != null && /* @__PURE__ */ a.jsx(
          $,
          {
            className: I.BE("input", "end-content"),
            level: x ?? Z[e],
            children: F
          }
        )
      ]
    }
  );
  return V == null ? te : /* @__PURE__ */ a.jsxs(X, { x: !0, pack: !0, className: I.BE("input", "container"), ...ee, children: [
    te,
    V
  ] });
}, wt = ({
  searchPlaceholder: e = "Search...",
  onSearch: s,
  actions: t,
  dialogVariant: r = "floating"
}) => {
  const [l, o] = H(""), c = /* @__PURE__ */ a.jsx(
    Tt,
    {
      value: l,
      autoFocus: !0,
      placeholder: e,
      size: r === "modal" ? "large" : "medium",
      contrast: 3,
      rounded: !0,
      grow: !0,
      full: "x",
      onChange: (n) => {
        o(n), s?.(n);
      }
    }
  );
  return t == null ? c : /* @__PURE__ */ a.jsxs(X, { pack: !0, x: !0, children: [
    c,
    t
  ] });
}, me = W([]), xe = W(() => {
}), Lt = 0.9, Yt = ({ children: e, maxHistory: s = 500 }) => {
  const [{ path: t }, { statuses: r }, l] = lt({
    type: dt.TYPE,
    schema: at,
    initialState: { statuses: [] }
  });
  if (r.length > s) {
    const c = Math.floor(s * Lt);
    l((n) => ({ ...n, statuses: r.slice(0, c) }));
  }
  const o = S(
    (c) => {
      const n = { time: q.now(), key: Ue.create(), ...c };
      l((d) => ({
        ...d,
        statuses: [n, ...d.statuses.filter((f) => f.key != n.key)]
      }));
    },
    [l]
  );
  return /* @__PURE__ */ a.jsx(me, { value: r, children: /* @__PURE__ */ a.jsx(xe, { value: o, children: /* @__PURE__ */ a.jsx(ut, { path: t, children: e }) }) });
}, he = () => ne(xe), zt = () => {
  const e = he();
  return L(() => it(e), [e]);
}, Jt = () => {
  const e = he();
  return L(() => ft(e), [e]);
}, Dt = k.seconds(7), Nt = k.seconds(1), Qt = ({
  expiration: e = Dt,
  poll: s = Nt
} = {}) => {
  const t = ne(me), [r, l] = H(/* @__PURE__ */ new Set()), [o, c] = H(() => q.now());
  _(() => {
    const m = setInterval(() => c(q.now()), s.milliseconds);
    return () => clearInterval(m);
  }, [s.milliseconds]);
  const n = L(() => {
    const m = o.sub(e), v = t.filter(
      ({ key: u, time: i }) => i.afterEq(m) && !r.has(u)
    ).reduce((u, i) => {
      const h = `${i.variant}:${i.message}`;
      if (!u.has(h))
        return u.set(h, { ...i, count: 1 }), u;
      const E = u.get(h);
      return u.set(h, {
        ...E,
        count: E.count + 1,
        time: i.time.after(E.time) ? i.time : E.time
      }), u;
    }, /* @__PURE__ */ new Map());
    return Array.from(v.values());
  }, [t, e, r, o]), d = w(t), f = S((m) => {
    l((x) => {
      const v = /* @__PURE__ */ new Set(), u = d.current.find(({ key: i }) => i === m);
      return x.has(m) || v.add(m), u != null && d.current.filter(
        ({ message: h, variant: E }) => h === u.message && E === u.variant
      ).forEach((h) => v.add(h.key)), v.size == 0 ? x : /* @__PURE__ */ new Set([...v, ...x]);
    });
  }, []);
  return { statuses: n, silence: f };
}, es = We(Ye, { topRight: Xe }), O = ({
  level: e = "p",
  variant: s,
  description: t,
  hideIcon: r = !1,
  status: l,
  className: o,
  children: c,
  message: n,
  color: d,
  ...f
}) => {
  let m;
  if (l != null) {
    const { key: i, ...h } = l;
    return /* @__PURE__ */ a.jsx(O, { ...f, ...h });
  }
  r || (m = s === "loading" ? /* @__PURE__ */ a.jsx(ze, {}) : /* @__PURE__ */ a.jsx(Je, {}));
  const x = Ze.isNonZero(t);
  c ??= n;
  const v = /* @__PURE__ */ a.jsxs(
    $,
    {
      className: I(o, !x && I.BE("status", "text")),
      level: e,
      status: s,
      ...t == null ? f : {},
      children: [
        m,
        c
      ]
    }
  );
  if (!x) return v;
  const u = /* @__PURE__ */ a.jsx($, { level: "small", color: 8, children: t });
  return /* @__PURE__ */ a.jsxs(X, { y: !0, align: "start", gap: "small", center: !0, ...f, children: [
    v,
    u
  ] });
}, Pt = ({ resourceName: e = "result" }) => /* @__PURE__ */ a.jsxs($, { center: !0, status: "disabled", children: [
  "No ",
  e.toLowerCase(),
  "s found"
] }), pe = Ge(
  ({
    onSearch: e,
    children: s,
    emptyContent: t,
    status: r,
    resourceName: l = "result",
    actions: o,
    ...c
  }) => (t = L(() => r != null && r.variant !== "success" ? /* @__PURE__ */ a.jsx(
    O,
    {
      center: !0,
      variant: r?.variant,
      description: r?.description,
      children: r?.message
    }
  ) : typeof t == "string" ? /* @__PURE__ */ a.jsx(O, { center: !0, variant: "disabled", children: t }) : t ?? /* @__PURE__ */ a.jsx(Pt, { resourceName: l }), [r?.key, t]), /* @__PURE__ */ a.jsxs(
    qe,
    {
      ...c,
      className: I.BE("select", "dialog"),
      bordered: !1,
      children: [
        e != null && /* @__PURE__ */ a.jsx(
          wt,
          {
            dialogVariant: "floating",
            onSearch: e,
            searchPlaceholder: `Search ${l}s...`,
            actions: o
          }
        ),
        /* @__PURE__ */ a.jsx(
          tt,
          {
            emptyContent: t,
            bordered: !0,
            borderColor: 6,
            grow: !0,
            rounded: !0,
            full: "x",
            children: s
          }
        )
      ]
    }
  ))
);
pe.displayName = "Select.Dialog";
const ts = pe, ss = (e) => {
  const { itemKey: s } = e, t = jt(s);
  return /* @__PURE__ */ a.jsx(st, { ...t, ...e });
};
export {
  Yt as A,
  pe as C,
  ts as D,
  At as F,
  ss as L,
  O as S,
  Tt as T,
  jt as a,
  Wt as b,
  Xt as c,
  ae as d,
  St as e,
  gt as f,
  zt as g,
  he as h,
  Jt as i,
  es as j,
  Qt as k,
  Y as u
};
