import { jsxs as I, jsx as i } from "react/jsx-runtime";
import D, { useRef as L, useState as P, useCallback as H, useLayoutEffect as k, useEffect as j, useMemo as h } from "react";
import { ComboBoxSelectMode as a } from "../../types/ComboBoxMode.js";
import K from "../../utils/calculateTagOverflow.js";
import O from "../combo-box-tag/ComboBoxTag.js";
import B from "../../../box/Box.js";
import Q from "../../../text/Text.js";
import q from "../../../skeleton/Skeleton.js";
import { Popover as X, PopoverTrigger as Y, PopoverContent as Z } from "../../../popover/PopoverPrimitive.js";
import V from "../combo-box-trigger/ComboBoxTrigger.js";
import { c as R } from "../../../../index-BtlgVD4m.js";
const ut = ({
  handleOpenChange: f,
  value: e,
  placeholder: C,
  isDisabled: N,
  children: F,
  triggerId: G,
  isLoading: x,
  isOpen: E,
  contentId: S,
  handleRemoveValue: d,
  mode: n,
  triggerRef: s,
  groups: m
}) => {
  const T = D.useId(), U = G ?? `combo-box-default-${T.replace(/:/g, "")}`, _ = S ?? `combo-box-default-content-${T.replace(/:/g, "")}`, w = L(null), [c, p] = P(0), [z, M] = P(null), b = L(null), W = H(() => f == null ? void 0 : f(!0), [f]);
  k(() => {
    const t = typeof s == "object" && s !== null && "current" in s ? s.current : null;
    if (!t) {
      M(null);
      return;
    }
    const r = t.closest('[role="dialog"], [role="alertdialog"]');
    M(r);
  }, [s, E]), k(() => {
    if (n !== a.MULTI) return;
    const t = JSON.stringify(e);
    if (b.current !== t && (b.current = t, c !== 0)) {
      p(0);
      return;
    }
    if (c !== 0) return;
    const r = w.current;
    if (!r) return;
    const l = Array.from(r.querySelectorAll("[data-combo-tag]")), y = r.getBoundingClientRect().width, g = l.map((o) => o.getBoundingClientRect().width), u = K(y, g);
    u > 0 && p(u);
  }, [n, e, c]), j(() => {
    if (n !== a.MULTI) return;
    const t = w.current;
    if (!t) return;
    const r = new ResizeObserver(() => {
      b.current = null, p(0);
    });
    return r.observe(t), t.querySelectorAll("[data-combo-tag]").forEach((l) => r.observe(l)), () => r.disconnect();
  }, [n, e]), j(() => {
    var r;
    if (n !== a.MULTI || typeof document > "u" || !((r = document.fonts) != null && r.ready)) return;
    let t = !0;
    return document.fonts.ready.then(() => {
      t && (b.current = null, p(0));
    }), () => {
      t = !1;
    };
  }, [n]);
  const A = h(() => n === a.SINGLE ? !!(e != null && e.trim()) : Array.isArray(e) && e.some((t) => t && t.trim() !== ""), [e, n]), $ = h(() => {
    var u;
    if (!A) return C;
    const t = (m == null ? void 0 : m.flatMap((o) => o.items)) ?? [];
    if (n === a.SINGLE) return (u = t.find((o) => o.value === e)) == null ? void 0 : u.label;
    const r = e == null ? void 0 : e.map((o) => t.find((v) => v.value === o)).filter((o) => !!o && o.label.trim() !== ""), l = c > 0 ? Math.max(1, r.length - c) : r.length, y = r.slice(0, l), g = r.length - l;
    return /* @__PURE__ */ I("div", { ref: w, className: "flex items-center gap-[var(--spacing-1)] overflow-hidden w-full", children: [
      y.map((o) => o ? /* @__PURE__ */ i("span", { "data-combo-tag": !0, className: "shrink-0", children: /* @__PURE__ */ i(
        O,
        {
          label: o.label,
          onClose: (v) => {
            v.stopPropagation(), d == null || d(o.value);
          }
        }
      ) }, o.value) : null),
      g > 0 && /* @__PURE__ */ i(B, { className: "flex shrink-0 items-center rounded-[var(--border-radius-default)] bg-[var(--base-info-border)] px-[var(--spacing-1)] py-[var(--spacing-0-5)] text-[var(--base-info-text)]", children: /* @__PURE__ */ I(Q, { type: "small", color: "info", children: [
        "+",
        g
      ] }) })
    ] });
  }, [e, d, n, A, C, m, c]), J = h(() => n === a.SINGLE ? /* @__PURE__ */ i(q, { variant: "line" }) : Array.from({ length: 3 }).map((t, r) => /* @__PURE__ */ i(q, { variant: "line" }, r)), [n]);
  return /* @__PURE__ */ I(X, { open: E, onOpenChange: f, children: [
    /* @__PURE__ */ i(Y, { disabled: N, className: "w-full", asChild: !0, ref: s, children: /* @__PURE__ */ i(V, { id: U, onFocus: W, disabled: N || x, isLoading: x, children: x ? /* @__PURE__ */ i(B, { className: "flex items-center gap-[var(--spacing-2)] w-full", children: J }) : /* @__PURE__ */ i(B, { className: R("flex min-w-0 flex-1 items-center gap-[var(--spacing-1)] overflow-hidden"), children: $ }) }) }),
    /* @__PURE__ */ i(
      Z,
      {
        className: "w-[var(--radix-popover-trigger-width)] p-0",
        align: "start",
        id: _,
        "data-testid": S,
        collisionBoundary: z ?? void 0,
        collisionPadding: 8,
        children: F
      }
    )
  ] });
};
export {
  ut as default
};
//# sourceMappingURL=ComboBoxDefault.js.map
