import { jsx as e, jsxs as H } from "@emotion/react/jsx-runtime";
import { useRef as R } from "react";
import _ from "clsx";
import { CHECKABLE_LIST_CLASSES as s } from "./constants.js";
import { useModuleDictionary as ue } from "@m4l/core";
import { DICCTIONARY as c } from "./dictionary.js";
import { useSizeContainer as he } from "../../hooks/useSizeContainer/index.js";
import { useStateRef as pe } from "../../hooks/useStateRef/index.js";
import { Icon as ge } from "@m4l/base";
import { useCheckableListState as Se } from "./hooks/useCheckableListState.js";
import { useCheckableListItems as fe } from "./hooks/useCheckableListItems.js";
import { useCheckableListSelection as be } from "./hooks/useCheckableListSelection.js";
import { useCheckableListHandlers as Ce } from "./hooks/useCheckableListHandlers.js";
import { useCheckableListVirtualization as Ie } from "./hooks/useCheckableListVirtualization.js";
import { useCheckableListRender as ye } from "./hooks/useCheckableListRender.js";
import { RootStyled as A, LoadingContainerStyled as Le, SearchContainerStyled as ke, SearchInputStyled as xe, SelectAllContainerStyled as ve, SelectAllCheckboxStyled as ze, ChipCountTypografyStyled as Ge, ListContainerStyled as He, VirtualizedListStyled as Re, EmptyMessageWrapperStyled as _e } from "./slots/styled.js";
import { Skeleton as Ae } from "../extended/mui/Skeleton/Skeleton.js";
import { ImageText as Te } from "../ImageText/ImageText.js";
const Xe = (T) => {
  const {
    value: r = [],
    onChange: w,
    items: E = [],
    groups: d,
    multiple: m = !0,
    selectAll: N = !1,
    groupable: B = !0,
    expandAllGroups: D = !0,
    searchable: V = !1,
    filterFn: j,
    size: l = "medium",
    variant: g = "standard",
    width: F = "100%",
    checkboxSize: S = "medium",
    showCheckboxOnGroup: M = !0,
    indeterminateBehavior: O = "indicate-some",
    className: f,
    "data-testid": b,
    ariaLabel: C,
    ariaLabelledBy: I,
    renderItem: P,
    renderGroup: Q,
    loading: K = !1,
    disabled: a = !1,
    itemRowHeight: U,
    groupRowHeight: W
  } = T, Y = R(null), [q, J] = pe(null), i = he(q, {
    // excludePadding: true,
  }), { getLabel: o } = ue(), n = R(i?.containerHeight ?? 0);
  n.current !== i?.containerHeight && i?.containerHeight !== 0 && (n.current = i?.containerHeight ?? 0);
  const {
    searchQuery: y,
    setSearchQuery: X,
    expandedGroups: L,
    itemHeights: Z,
    handleGroupExpandToggle: $
  } = Se({
    groups: d,
    expandAllGroups: D
  }), {
    isGrouped: u,
    normalizedItems: k,
    filteredItems: ee,
    filteredGroups: te,
    virtualizedItems: h
  } = fe({
    items: E,
    groups: d,
    searchQuery: y,
    filterFn: j,
    expandedGroups: L
  }), {
    getGroupItems: x,
    isItemSelected: v,
    isGroupFullySelected: z,
    isGroupPartiallySelected: re,
    areAllItemsSelected: p,
    areSomeItemsSelected: le
  } = be({
    value: r,
    normalizedItems: k,
    groups: d,
    isGrouped: u
  }), { handleItemToggle: ae, handleGroupToggle: ie, handleSelectAllToggle: oe } = Ce({
    value: r,
    onChange: w,
    multiple: m,
    disabled: a,
    normalizedItems: k,
    getGroupItems: x,
    isGroupFullySelected: z,
    isItemSelected: v,
    areAllItemsSelected: p
  }), { getItemSize: ne } = Ie({
    virtualizedItems: h,
    itemHeights: Z,
    defaultItemRowHeight: U,
    defaultGroupRowHeight: W
  }), { renderVirtualizedItem: se, urlIconSearch: ce, urlIconNoResults: de } = ye({
    isItemSelected: v,
    disabled: a,
    size: l,
    variant: g,
    checkboxSize: S,
    handleItemToggle: ae,
    renderItem: P,
    expandedGroups: L,
    isGroupFullySelected: z,
    isGroupPartiallySelected: re,
    getGroupItems: x,
    indeterminateBehavior: O,
    renderGroup: Q,
    groupable: B,
    showCheckboxOnGroup: M,
    handleGroupExpandToggle: $,
    handleGroupToggle: ie
  }), t = {
    size: l,
    variant: g,
    disabled: a,
    grouped: u,
    hasSelected: r.length > 0
  };
  if (K)
    return /* @__PURE__ */ e(
      A,
      {
        ownerState: t,
        className: _(f, s.root),
        "data-testid": b,
        "aria-label": C || "checkable list",
        "aria-labelledby": I,
        children: /* @__PURE__ */ e(Le, { ownerState: t, children: /* @__PURE__ */ e(
          Ae,
          {
            variant: "rectangular",
            width: "100%",
            height: 200,
            role: "status",
            "aria-label": "Cargando lista de elementos",
            "aria-live": "polite"
          }
        ) })
      }
    );
  const G = u ? te.length > 0 : ee.length > 0;
  return /* @__PURE__ */ H(
    A,
    {
      ownerState: t,
      className: _(f, s.root),
      "data-testid": b,
      role: "group",
      "aria-label": C || "checkable list",
      "aria-labelledby": I,
      children: [
        V && /* @__PURE__ */ e(ke, { ownerState: t, role: "search", children: /* @__PURE__ */ e(
          xe,
          {
            role: "searchbox",
            "aria-label": "search",
            ownerState: t,
            value: y,
            onChange: (me) => X(me.target.value),
            placeholder: o(c.label_search_placeholder),
            size: l,
            className: s.searchInput,
            variant: "contained",
            InputProps: {
              startAdornment: /* @__PURE__ */ e(ge, { src: ce })
            }
          }
        ) }),
        N && G && m && /* @__PURE__ */ H(ve, { ownerState: t, role: "group", "aria-label": "Seleccionar todo", children: [
          /* @__PURE__ */ e(
            ze,
            {
              checked: p,
              indeterminate: le && !p,
              disabled: a,
              size: S,
              onChange: oe,
              inlineText: o(c.label_select_all)
            }
          ),
          /* @__PURE__ */ e(
            Ge,
            {
              ownerState: t,
              variant: "bodyStandard",
              size: l,
              color: "text.disabled",
              children: r.length
            }
          )
        ] }),
        /* @__PURE__ */ e(
          He,
          {
            ref: J,
            ownerState: { ...t },
            "aria-multiselectable": m ? "true" : "false",
            children: G ? n.current > 0 ? /* @__PURE__ */ e(
              Re,
              {
                ref: Y,
                height: n.current,
                width: F,
                itemCount: h.length,
                itemSize: ne,
                itemData: h,
                layout: "vertical",
                className: s.virtualizedList,
                children: se
              }
            ) : null : /* @__PURE__ */ e(_e, { ownerState: t, role: "status", "aria-live": "polite", children: /* @__PURE__ */ e(
              Te,
              {
                title: o(c.label_no_results),
                message: o(c.label_no_results_description),
                imageUrl: de
              }
            ) })
          }
        )
      ]
    }
  );
};
export {
  Xe as CheckableList
};
