'use strict';
'use client';
import { c } from 'react/compiler-runtime';
import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
import { Menu } from '@base-ui/react/menu';
import { Select, SelectTrigger, SelectContent, SelectGroup, SelectItem } from '../../../fd-component-toolbox/select/Select.js';
import { themes } from '../themes.js';
import { UserDropdownMenuItem } from './user-dropdown-menu-item/UserDropdownMenuItem.js';
import { Avatar, AvatarImage, AvatarFallback } from '../../../fd-component-toolbox/avatar/Avatar.js';
import { ChevronDown } from 'lucide-react';
import { getLoginState } from '../../../../fd-toolbox/auth/internal-login-states.js';
import { useTheme } from '../../providers/ThemeProvider/ThemeProvider.js';
import { useState, useEffect, Suspense } from 'react';
import { imageNotFoundUrl } from '../../../../fd-toolbox/constants/public-files.js';
import { emptyUuid } from '../../../../fd-toolbox/constants/constants.js';
import { SubHeading } from '../../../fd-component-toolbox/subheading/SubHeading.js';
import { Icon } from '../../../fd-component-toolbox/icon/Icon.js';
import { isPackageMode } from '../../../../fd-toolbox/lib/environments.js';
import { useSaveComplete } from '../../../meta/edit-resource/save-complete-context.js';
import { WorkspacePicker } from '../workspace-picker/WorkspacePicker.js';
import { elements } from '../../../../fd-toolbox/constants/elements.js';
import { VisibleWorkspacePicker } from '../visible-workspace-picker/VisibleWorkspacePicker.js';
import { getThemeIcon, getThemeLabel, getUserDropDownMenuTriggerId } from './UserDropdownMenu.funcs.js';

var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", {
  value,
  configurable: true
});
function UserDropdownMenu(t0) {
  const $ = c(16);
  const {
    id,
    items,
    parentContextName,
    userWorkspaces,
    visibleWorkspaces
  } = t0;
  const [open, setOpen] = useState(false);
  const {
    theme: themeState,
    switchTheme
  } = useTheme();
  const [loginState, setLoginState] = useState();
  let t1;
  if ($[0] !== loginState) {
    t1 = loginState?.loggedIn && loginState.imageUrl && !loginState.imageUrl.includes(emptyUuid) && loginState.imageUrl !== imageNotFoundUrl ? loginState.imageUrl : imageNotFoundUrl;
    $[0] = loginState;
    $[1] = t1;
  } else {
    t1 = $[1];
  }
  const avatarImageUrl = t1;
  useSaveComplete();
  let t2;
  let t3;
  if ($[2] === Symbol.for("react.memo_cache_sentinel")) {
    t2 = () => {
      setLoginState(getLoginState());
    };
    t3 = [];
    $[2] = t2;
    $[3] = t3;
  } else {
    t2 = $[2];
    t3 = $[3];
  }
  useEffect(t2, t3);
  let t4;
  if ($[4] !== avatarImageUrl || $[5] !== id || $[6] !== items || $[7] !== loginState || $[8] !== open || $[9] !== parentContextName || $[10] !== switchTheme || $[11] !== themeState || $[12] !== userWorkspaces || $[13] !== visibleWorkspaces) {
    let t5;
    if ($[15] === Symbol.for("react.memo_cache_sentinel")) {
      t5 = item => jsx(UserDropdownMenuItem, {
        id: item.id,
        item,
        className: "hover:bg-muted/50 z-50 rounded-md",
        closeMenu: () => setOpen(false)
      }, `${item.text}_${item.type}`);
      $[15] = t5;
    } else {
      t5 = $[15];
    }
    t4 = jsxs(Menu.Root, {
      open,
      onOpenChange: setOpen,
      children: [jsx(Menu.Trigger, {
        id: elements.userSettings.userDropdownButton,
        className: "hover:bg-fd-tertiary-700 cursor-pointer rounded-md p-2",
        children: jsxs("div", {
          id,
          className: "flex flex-row items-center",
          children: [jsxs(Avatar, {
            className: "mr-2.5 size-5",
            children: [jsx(AvatarImage, {
              src: avatarImageUrl
            }), jsx(AvatarFallback, {})]
          }), jsx(SubHeading, {
            className: "text-fd-tertiary-50 mr-2 p-0 text-center text-sm whitespace-nowrap",
            children: loginState?.loggedIn ? loginState.displayName : "Login"
          }), jsx(ChevronDown, {
            className: "text-fd-tertiary-50 size-5 justify-end"
          })]
        })
      }), jsx(Menu.Portal, {
        children: jsx(Menu.Positioner, {
          align: "start",
          sideOffset: 4,
          className: "z-50",
          children: jsxs(Menu.Popup, {
            className: "bg-background focus:ring-primary min-w-32 rounded-md border p-1 shadow outline-none",
            children: [!isPackageMode() && jsxs(Fragment, {
              children: [jsxs(Select, {
                value: themeState,
                onValueChange: value => value && switchTheme(value),
                children: [jsx(SelectTrigger, {
                  noUnderline: true,
                  className: "hover:bg-muted/50 cursor-pointer p-2 font-medium shadow-none",
                  children: jsxs("div", {
                    className: "flex items-center gap-x-2",
                    children: [jsx(Icon, {
                      name: getThemeIcon(themeState),
                      className: "size-3"
                    }), jsx("p", {
                      children: getThemeLabel(themeState)
                    })]
                  })
                }), jsx(SelectContent, {
                  children: jsxs(SelectGroup, {
                    children: [jsx(SelectItem, {
                      value: themes.light,
                      children: themes.lightMode
                    }), jsx(SelectItem, {
                      value: themes.dark,
                      children: themes.darkMode
                    }), jsx(SelectItem, {
                      value: themes.system,
                      children: themes.systemMode
                    })]
                  })
                })]
              }), jsx(Suspense, {
                children: jsx(WorkspacePicker, {
                  parentContextName: getUserDropDownMenuTriggerId(parentContextName ?? ""),
                  userWorkspaces
                })
              }), jsx(Suspense, {
                children: jsx(VisibleWorkspacePicker, {
                  visibleWorkspaces
                })
              })]
            }), items.map(t5)]
          })
        })
      })]
    });
    $[4] = avatarImageUrl;
    $[5] = id;
    $[6] = items;
    $[7] = loginState;
    $[8] = open;
    $[9] = parentContextName;
    $[10] = switchTheme;
    $[11] = themeState;
    $[12] = userWorkspaces;
    $[13] = visibleWorkspaces;
    $[14] = t4;
  } else {
    t4 = $[14];
  }
  return t4;
}
__name(UserDropdownMenu, "UserDropdownMenu");

export { UserDropdownMenu };
