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