import { jsxs as P, jsx as t } from "react/jsx-runtime";
import { useButton as Y } from "@react-aria/button";
import { useFocusRing as Z, FocusScope as $ } from "@react-aria/focus";
import { useState as p, useRef as O, useId as I, useEffect as h, useLayoutEffect as K } from "react";
import { usePopper as ee } from "react-popper";
import { CheckboxState as te } from "../Checkbox/Checkbox.es.js";
import { Checklist as oe, ChecklistDirection as re } from "../Checklist/Checklist.es.js";
import { Tag as z, TagSize as d, TagType as F } from "../Tag/Tag.es.js";
import { Trigger as le, TriggerEmphasis as se } from "../Trigger/Trigger.es.js";
import { useDropdownAutoHeight as ne, DEFAULT_DROPDOWN_MAX_HEIGHT as ae } from "../../hooks/useDropdownAutoHeight.es.js";
import { EnablePortalWrapper as ie } from "../../utilities/dialogs/EnablePortalWrapper.es.js";
import { merge as ce } from "../../utilities/merge.es.js";
import { Validation as ue } from "../../utilities/validation.es.js";
import { useClickOutside as pe, getPaddingClasses as de } from "./helpers.es.js";
import { Text as R } from "../../typography/Text/Text.es.js";
var fe = /* @__PURE__ */ ((o) => (o.Default = "Default", o.Summarized = "Summarized", o))(fe || {}), we = /* @__PURE__ */ ((o) => (o.Small = "Small", o.Medium = "Medium", o))(we || {});
const me = ({
  items: o,
  activeItemKeys: l,
  onSelectionChange: f,
  ariaLabel: g = "Select list",
  disabled: x = !1,
  placeholder: S,
  label: b,
  type: v = "Default",
  size: w = "Medium",
  validation: A = ue.Default,
  summarizedLabel: D,
  indeterminateItemKeys: s,
  flip: V = !1,
  emphasis: H = se.Default,
  enablePortal: L = !0
}) => {
  const [a, c] = p(!1), [j, B] = p([]), n = O(null), [k, T] = p(null), [i, W] = p(null), _ = O(null), { isFocusVisible: y, focusProps: G } = Z(), { maxHeight: C } = ne({ current: i }, { isOpen: a, autoResize: !0 }), E = l.length > 0, U = D ?? [l.length, "selected"].join(" ");
  pe(null, () => c(!1), [n == null ? void 0 : n.current, k]);
  const X = C !== ae, M = I(), m = () => c((e) => !e), { buttonProps: q } = Y(
    {
      onPress: m,
      onKeyDown: (e) => {
        e.key === "Escape" && c(!1);
      },
      elementType: "div"
    },
    { current: i }
  ), J = (e) => {
    const r = new Set(l);
    r.has(e) ? r.delete(e) : r.add(e), f(Array.from(r));
  }, Q = (e) => {
    (e.code === "Space" || e.code === "Enter") && m();
  }, N = () => a ? F.SelectedWithFocus : F.Selected;
  h(() => {
    T(n == null ? void 0 : n.current);
  }, []), h(() => {
    B(
      o.map((e) => {
        const r = { ...e, label: e.value };
        return s != null && s.includes(e.value) ? { ...r, state: te.Mixed } : r;
      })
    );
  }, [o, s]);
  const u = ee(i, k, {
    placement: "bottom-start",
    strategy: "fixed",
    modifiers: [
      {
        name: "offset",
        options: {
          offset: [0, 8]
        }
      },
      {
        name: "flip",
        enabled: V
      }
    ]
  });
  return h(() => {
    (async () => {
      u.update && await u.update();
    })().catch(console.error);
  }, [l]), K(() => {
    const e = (r) => {
      r.key === "Escape" && c(!1);
    };
    return window.addEventListener("keyup", e), () => {
      window.removeEventListener("keyup", e);
    };
  }, []), /* @__PURE__ */ P("div", { className: "tw-relative", ref: n, children: [
    /* @__PURE__ */ t(
      le,
      {
        disabled: x,
        buttonProps: q,
        isFocusVisible: y,
        isOpen: a,
        validation: A,
        emphasis: H,
        children: /* @__PURE__ */ t("div", { className: ce(["tw-flex tw-flex-1 tw-gap-2", de(w)]), ref: W, children: /* @__PURE__ */ t(
          "div",
          {
            className: "tw-flex tw-flex-1 tw-gap-2 focus:tw-outline-0",
            onClick: (e) => {
              e.target === _.current && a || m();
            },
            role: "combobox",
            "aria-expanded": a,
            "aria-controls": M,
            "aria-label": g,
            ...G,
            tabIndex: 0,
            onKeyDown: Q,
            children: /* @__PURE__ */ P("div", { className: "tw-flex tw-flex-wrap tw-gap-2 tw-outline-none tw-items-center tw-min-h-[28px]", children: [
              b && E && /* @__PURE__ */ t(R, { weight: "strong", children: b }),
              v === "Default" && l.map((e) => /* @__PURE__ */ t(
                z,
                {
                  type: N(),
                  label: e.toString(),
                  size: w === "Small" ? d.Small : d.Medium,
                  onClick: x ? void 0 : () => J(e)
                },
                e
              )),
              v === "Summarized" && (E || D) && /* @__PURE__ */ t(
                z,
                {
                  type: N(),
                  label: U,
                  size: w === "Small" ? d.Small : d.Medium,
                  onClick: (s == null ? void 0 : s.length) === 0 ? () => f([]) : void 0
                }
              ),
              l.length === 0 && S && /* @__PURE__ */ t(R, { color: "weak", children: S })
            ] })
          }
        ) })
      }
    ),
    a && X && /* @__PURE__ */ t(ie, { enablePortal: L, children: /* @__PURE__ */ t(
      "div",
      {
        id: M,
        ref: T,
        className: "tw-absolute tw-left-0 tw-w-full tw-overflow-hidden tw-p-0 tw-shadow-mid tw-list-none tw-m-0 tw-mt-2 tw-z-[120000] tw-bg-base tw-min-w-72",
        style: {
          ...u.styles.popper,
          width: i == null ? void 0 : i.getBoundingClientRect().width
        },
        ...u.attributes.popper,
        children: /* @__PURE__ */ t($, { restoreFocus: !0, children: /* @__PURE__ */ t(
          "div",
          {
            className: "tw-p-4 tw-overflow-y-auto tw-overflow-x-hidden tw-w-full tw-relative",
            style: { maxHeight: C },
            children: /* @__PURE__ */ t(
              oe,
              {
                activeValues: l.map((e) => e.toString()),
                setActiveValues: f,
                checkboxes: j.filter((e) => !e.isDivider && !e.isCategory),
                direction: re.Vertical,
                ariaLabel: g
              }
            )
          }
        ) })
      },
      "content"
    ) })
  ] });
};
me.displayName = "FondueMultiSelect";
export {
  me as MultiSelect,
  we as MultiSelectSize,
  fe as MultiSelectType
};
//# sourceMappingURL=MultiSelect.es.js.map
