'use strict';
import { c } from 'react/compiler-runtime';
import { jsxs, jsx } from 'react/jsx-runtime';
import { useState } from 'react';
import { Icon, iconSizes, iconNames } from '../../../fd-component-toolbox/icon/Icon.js';
import { Sheet, SheetTrigger, SheetContent, SheetHeader, SheetTitle, SheetDescription } from '../../../fd-component-toolbox/sheet/Sheet.js';
import { NotificationList } from './notification-list/NotificationList.js';
import { useAsyncEffect } from '../../../../fd-toolbox/hooks/use-async-effect.js';
import { initNotification, setNotificationState, handleNotificationOpenChange, getNotificationMenuTriggerId } from './NotificationMenu.funcs.js';
import { Button } from '../../../fd-component-toolbox/button/Button.js';
import { useAuth } from '../../providers/auth-provider/AuthProvider.js';
import { useOptionalPromise } from '../../../../fd-toolbox/hooks/use-optional-promise.js';
import { useElementId } from '../../../../fd-toolbox/hooks/use-element-id.js';
import { elements } from '../../../../fd-toolbox/constants/elements.js';

var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", {
  value,
  configurable: true
});
function NotificationMenu(t0) {
  const $ = c(18);
  const {
    parentContextName,
    notifications
  } = t0;
  const notificationProps = useOptionalPromise(notifications);
  const {
    loginState
  } = useAuth();
  let t1;
  if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
    t1 = [];
    $[0] = t1;
  } else {
    t1 = $[0];
  }
  const [notificationsState, setNotificationsState] = useState(t1);
  const [hasUnreadNotification, setHasUnreadNotification] = useState(false);
  const [isOpen, setIsOpen] = useState(false);
  const notificationMenuId = useElementId(NotificationMenu.displayName, parentContextName);
  let t2;
  if ($[1] !== loginState.loggedIn || $[2] !== loginState.userId || $[3] !== notificationProps) {
    t2 = async () => {
      await initNotification(loginState.loggedIn, loginState.userId, setNotificationsState, setHasUnreadNotification, notification => setNotificationState(notification, setNotificationsState, setHasUnreadNotification), notificationProps);
    };
    $[1] = loginState.loggedIn;
    $[2] = loginState.userId;
    $[3] = notificationProps;
    $[4] = t2;
  } else {
    t2 = $[4];
  }
  let t3;
  if ($[5] !== isOpen || $[6] !== loginState.loggedIn) {
    t3 = [loginState.loggedIn, isOpen];
    $[5] = isOpen;
    $[6] = loginState.loggedIn;
    $[7] = t3;
  } else {
    t3 = $[7];
  }
  useAsyncEffect(t2, t3);
  let t4;
  if ($[8] !== hasUnreadNotification || $[9] !== isOpen || $[10] !== notificationMenuId || $[11] !== notificationsState) {
    let t5;
    if ($[13] !== notificationsState) {
      t5 = async open => handleNotificationOpenChange(open, setIsOpen, notificationsState, setNotificationsState, setHasUnreadNotification);
      $[13] = notificationsState;
      $[14] = t5;
    } else {
      t5 = $[14];
    }
    const onOpenChange = __name(t5, "onOpenChange");
    let t6;
    if ($[15] !== hasUnreadNotification || $[16] !== notificationMenuId) {
      t6 = props => jsxs(Button, {
        ...props,
        variant: "icon",
        className: "hover:bg-fd-tertiary-700 relative flex size-8 justify-center rounded-md p-1",
        tooltipText: "Notifications",
        tooltipClassName: "mt-2",
        id: getNotificationMenuTriggerId(notificationMenuId),
        children: [hasUnreadNotification && jsx(Icon, {
          name: iconNames.dotFilled,
          size: iconSizes.large,
          className: "absolute -top-1 -right-1 stroke-[6]",
          color: "text-destructive"
        }), jsx(Icon, {
          name: iconNames.bell,
          className: "text-fd-tertiary-50 w-full",
          color: "color-white"
        })]
      });
      $[15] = hasUnreadNotification;
      $[16] = notificationMenuId;
      $[17] = t6;
    } else {
      t6 = $[17];
    }
    t4 = jsx("div", {
      children: jsxs(Sheet, {
        onOpenChange,
        open: isOpen,
        children: [jsx(SheetTrigger, {
          render: t6
        }), jsxs(SheetContent, {
          className: "bg-background flex h-full w-3/4 flex-col border-none p-0 pb-4 md:w-1/2 xl:w-3/12",
          children: [jsxs(SheetHeader, {
            className: "bg-gradient-accent flex min-h-16 flex-row items-center justify-between px-8",
            children: [jsx(SheetTitle, {
              className: "text-fd-tertiary-50 text-xl font-bold",
              children: "Notifications"
            }), jsx(SheetDescription, {})]
          }), jsx(NotificationList, {
            parentContextName: notificationMenuId,
            notifications: notificationsState
          })]
        })]
      })
    });
    $[8] = hasUnreadNotification;
    $[9] = isOpen;
    $[10] = notificationMenuId;
    $[11] = notificationsState;
    $[12] = t4;
  } else {
    t4 = $[12];
  }
  return t4;
}
__name(NotificationMenu, "NotificationMenu");
NotificationMenu.displayName = elements.notification.menu;

export { NotificationMenu };
