import { jsx as o } from "@emotion/react/jsx-runtime";
import { useState as a, useEffect as h } from "react";
import { useFirstRender as E, useLocales as v } from "@m4l/graphics";
import { useEnvironment as R, useNetwork as k } from "@m4l/core";
import { getPropDataTestId as y } from "../../test/getNameDataTestId.js";
import { getComponentSlotRoot as z } from "../../utils/getComponentSlotRoot.js";
import { useComponentSize as A } from "../../hooks/useComponentSize/index.js";
import { Image as l } from "../Image/Image.js";
import { LanguagePopoverRootStyled as M, PaperStyled as N } from "./slots/LanguagePopoverSlots.js";
import { LANGUAGE_POPOVER_KEY_COMPONENT as c } from "./constants.js";
import { LanguagePopoverSlots as T } from "./slots/LanguagePopoverEnum.js";
function q(i) {
  const {
    disabled: m,
    size: u
  } = i, { currentSize: t } = A(u), p = E(), { currentLocale: f, onChangeLocale: d } = v(), { domainToken: g } = R(), { networkOperation: O } = k(), [n, L] = a([]), [r, I] = a(!1), P = (e) => {
    e && I(!0);
  };
  h(() => {
    let e = !0;
    return r && O({
      method: "GET",
      endPoint: "na/locales",
      parms: { domain_token: g },
      responseToCamelCase: !0
    }).then((s) => {
      if (e)
        try {
          L(s.data);
        } catch (S) {
          console.error(S);
        }
    }), function() {
      e = !1;
    };
  }, [r, p]);
  const b = () => n?.length === 0 ? [{
    type: "loader"
  }] : n?.map((e) => ({
    type: "menuItem",
    label: e?.name,
    startIcon: /* @__PURE__ */ o(l, { src: e?.urlIcon, enableIntersectionObserver: !1 }),
    onClick: () => d(e)
  })), C = z(c);
  return /* @__PURE__ */ o(
    M,
    {
      ownerState: {
        size: t
      },
      role: "button",
      "aria-label": "Language selector",
      size: t,
      className: C,
      ...y(c, T.root),
      disabled: m,
      icon: /* @__PURE__ */ o(l, { src: f?.urlIcon, enableIntersectionObserver: !1 }),
      menuActions: b(),
      onOpen: P,
      slots: {
        paper: N
      }
    }
  );
}
export {
  q as LanguagePopover
};
