import { jsx as d, jsxs as D } from "@emotion/react/jsx-runtime";
import M, { useRef as x, useEffect as z, useMemo as c } from "react";
import { renderAdornment as S } from "../subcomponents/RenderAdornment/RenderAdornment.js";
import { ContainerMultipleValuesStyled as L, ContainerWrapperStyled as j, ChipStyled as q, StartAdornmentRootStyled as w, WrapperStartAdornmentStyled as g } from "../slots/AutocompleteSlots.js";
function W(E) {
  const {
    selectedValue: e,
    multiple: t,
    adjustedSize: o,
    getOptionLabelLocal: a,
    ownerState: l,
    handleDelete: m,
    disabled: O,
    startAdornment: p,
    getOptionStartAdornment: u,
    getOptionEndAdornment: R,
    getOptionDescription: A
  } = E, b = x(null);
  z(() => {
    if (!t || !Array.isArray(e))
      return;
    const r = b.current;
    if (!r)
      return;
    const n = requestAnimationFrame(() => {
      r.scrollTop = r.scrollHeight;
    });
    return () => {
      cancelAnimationFrame(n);
    };
  }, [t, e]);
  const s = c(() => {
    if (!p)
      return null;
    try {
      return M.cloneElement(p, {
        size: o
      });
    } catch (r) {
      return console.warn("Error cloning startAdornment:", r), null;
    }
  }, [p, o]), f = c(() => {
    if (!Array.isArray(e) || !t || e.length === 0)
      return null;
    const r = /* @__PURE__ */ new Map();
    return e.forEach((n) => {
      const i = a(n), C = u?.(n), $ = R?.(n);
      C && r.set(`${i}-start`, S(C, o)), $ && r.set(`${i}-end`, S($, o));
    }), r;
  }, [
    e,
    t,
    a,
    u,
    R,
    o
  ]), h = c(() => {
    if (t || !e || Array.isArray(e) && e.length === 0)
      return null;
    const r = Array.isArray(e) ? e[0] : e, n = u?.(r);
    return n ? S(n, o) : null;
  }, [
    t,
    e,
    u,
    o
  ]), y = c(() => Array.isArray(e) && t && e.length > 0 ? /* @__PURE__ */ d(L, { ownerState: l, ref: b, role: "list", "aria-label": "Selected options", children: /* @__PURE__ */ d(j, { children: e.map((r, n) => {
    const i = a(r);
    return /* @__PURE__ */ d(
      q,
      {
        size: o,
        label: i,
        opacity: !0,
        onDeleted: () => m(r),
        disabledDeleteButton: O,
        ownerState: l,
        startIcon: f?.get(`${i}-start`),
        endIcon: f?.get(`${i}-end`),
        description: A ? A(r) : void 0
      },
      `${i}-${n}`
    );
  }) }) }) : null, [
    e,
    t,
    o,
    a,
    m,
    O,
    l,
    f,
    A
  ]);
  return s && y ? /* @__PURE__ */ D(w, { children: [
    /* @__PURE__ */ d(g, { role: "img", "aria-hidden": "true", ownerState: l, children: s }),
    y
  ] }) : s ? /* @__PURE__ */ D(g, { role: "img", "aria-hidden": "true", ownerState: l, children: [
    s,
    !t && h
  ] }) : !t && h && !s ? /* @__PURE__ */ d(g, { role: "img", "aria-hidden": "true", ownerState: l, children: h }) : a ? m ? y : (console.error("useStartAdornments: handleDelete is required"), null) : (console.error("useStartAdornments: getOptionLabelLocal is required"), null);
}
export {
  W as useMultipleChips
};
