import { jsxs as N, Fragment as L, jsx as s } from "@emotion/react/jsx-runtime";
import { useNetwork as w } from "@m4l/core";
import { useState as c, useEffect as x, useCallback as u } from "react";
import { MenuPopover as D } from "./subcomponents/MenuPopover/MenuPopover.js";
import { ACCOUNT_KEY_POPOVER as M } from "./constants.js";
import { IconButtonRootStyled as R, AvatarStyled as T } from "./slots/AccountPopoverSlots.js";
import U from "./subcomponents/MyAvatar/MyAvatar.js";
import { getComponentClasses as _ } from "../../utils/getComponentSlotRoot.js";
import { AccountPopoverSlots as j } from "./slots/AccountPopoverEnum.js";
import { useComponentSize as B } from "../../hooks/useComponentSize/index.js";
const Q = (p) => {
  const {
    itemHandleClick: d,
    version: f,
    handleLogOut: h,
    userData: i,
    settingsHandleOpen: C,
    size: S
  } = p, { avatarUrl: v, firstName: A = "firstname", lastName: O = "lastname" } = i || {}, [l, n] = c("unLoad"), [t, o] = c(null), [g, m] = c([]), { networkOperation: z } = w(), { currentSize: r } = B(S), E = {
    size: r
  };
  x(() => {
    let e = !0;
    return l === "unLoad" && (n("loading"), z({
      method: "GET",
      endPoint: "menus",
      parms: {
        position: 1
      }
    }).then((a) => {
      e && (n("loaded"), m(a.data));
    }).catch((a) => {
      n("unLoad"), m([]);
    })), function() {
      e = !1;
    };
  }, [t]);
  const P = u(() => {
    o(null);
  }, [o]), k = u((e) => {
    o(e.currentTarget);
  }, [o]), y = _(M, j);
  return /* @__PURE__ */ N(L, { children: [
    /* @__PURE__ */ s(
      R,
      {
        ownerState: E,
        icon: /* @__PURE__ */ s(U, { size: r, avatarUrl: v, firstName: A, lastName: O, slotAvatar: T }),
        src: "",
        onClick: k,
        selected: !!t,
        className: y.iconButtonRoot
      }
    ),
    !!t && /* @__PURE__ */ s(
      D,
      {
        size: r,
        data: g,
        asyncState: l,
        itemHandleClick: d,
        version: f,
        handleLogOut: h,
        userData: i,
        settingsHandleOpen: C,
        anchorEl: t,
        handleClose: P
      }
    )
  ] });
};
export {
  Q as AccountPopover
};
