import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
import Icon from '../Icon/Icon.js';
import classNames from '../../_virtual/index.js';
import Typography from '../Typography/Typography.js';
import { checkEmpty } from '../../utils/checkEmpty/checkEmpty.js';
import AllProjectsDropdown from '../AllProjectsDropdown/AllProjectsDropdown.js';
import MenuOption from '../MenuOption/MenuOption.js';
import Tooltip from '../Tooltip/Tooltip.js';
import { useRef, useState, useEffect, useCallback, Fragment as Fragment$1 } from 'react';
import { getStoreValue } from '../../utils/indexDBStore/indexDB.js';

const AppHeader = ({ logo, profileContent, width, borderRadius, leftContent, rightContent, projectsList, appHeaderMenuItems, selectedMenu, selectedSubMenu, selectedQuickMenu, selectedProject = { label: '', value: '', iconName: '' }, onMenuClick = () => { }, onSubMenuClick = () => { }, onQuickMenuClick = () => { }, onProjectMenuClick = () => { }, onProjectDropdownLabelClick = () => { }, onMoreMenuOptionClick = () => { }, disabled = false, isClient = false, hideNavbar = false, scriptId, centerInfoItems = [], rightButtons = [], }) => {
    const hiddenMenuRef = useRef(null);
    const hiddenQuickMenuRef = useRef(null);
    const [projectArrayList, setProjectArrayList] = useState(projectsList);
    const [quickMenuItems, setQuickMenuItems] = useState([]);
    const [appHeaderHiddenMenuItems, setAppHeaderHiddenMenuItems] = useState([]);
    const projectDetails = getStoreValue('current-project');
    const timeoutRef = useRef(null);
    const [selectedMenuItem, setSelectedMenuItem] = useState(selectedMenu);
    useEffect(() => {
        if (projectsList !== projectArrayList) {
            setProjectArrayList(projectsList);
        }
    }, [projectsList, projectArrayList]);
    useEffect(() => {
        if (selectedMenu !== selectedMenuItem) {
            setSelectedMenuItem(selectedMenu);
        }
    }, [selectedMenu, selectedMenuItem]);
    const handleMenuClick = (menuItem) => {
        if (!disabled) {
            onMenuClick(menuItem);
            calculateVisibleItems();
        }
    };
    const handleSubMenuClick = (subMenuItem) => {
        if (!disabled) {
            onSubMenuClick(subMenuItem);
            if (timeoutRef.current)
                clearTimeout(timeoutRef.current);
            timeoutRef.current = setTimeout(() => {
                calculateVisibleItems();
            }, 800);
        }
    };
    const handleQuickMenuClick = (quickMenuItem) => {
        if (!disabled) {
            onQuickMenuClick(quickMenuItem);
        }
    };
    const handleProjectMenuClick = (projectItem) => {
        if (projectItem?.value === projectDetails?.id &&
            projectItem?.value !== 'All Projects') {
            return;
        }
        if (!disabled) {
            onProjectMenuClick(projectItem);
            calculateVisibleItems();
        }
    };
    const handleProjectDropdownLabelClick = () => {
        if (!disabled) {
            onProjectDropdownLabelClick();
            calculateVisibleItems();
        }
    };
    const handleMoreMenuOptionClick = (option) => {
        if (!disabled) {
            onMoreMenuOptionClick(option, calculateVisibleItems);
        }
    };
    const menuContainerRef = useRef(null);
    const menuItemsRef = useRef([]);
    const subMenuItemsRef = useRef([]);
    const quickMenuItemsRef = useRef([]);
    const menuItemsWidthRef = useRef({});
    const calculateVisibleItems = useCallback(() => {
        if ((!isClient && projectDetails?.id === 'All Projects') ||
            !menuContainerRef.current) {
            setAppHeaderHiddenMenuItems([]);
            return;
        }
        const containerWidth = isClient ? 700 : 890;
        let totalWidth = 0;
        const hiddenItems = [];
        const sortedMenuItems = [...appHeaderMenuItems];
        sortedMenuItems.forEach((item, index) => {
            const itemWidth = menuItemsRef.current[index]?.offsetWidth ??
                menuItemsWidthRef.current[item.label] ??
                0;
            if (itemWidth > 0) {
                menuItemsWidthRef.current[item.label] = itemWidth;
            }
        });
        const currentSelectedMenuItem = sortedMenuItems.find((item) => item.label === selectedMenuItem);
        const otherMenuItems = sortedMenuItems.filter((item) => item.label !== selectedMenuItem);
        if (currentSelectedMenuItem) {
            totalWidth +=
                menuItemsWidthRef.current[currentSelectedMenuItem.label] ?? 0;
        }
        for (const item of otherMenuItems) {
            const itemWidth = menuItemsWidthRef.current[item.label] ?? 0;
            if (totalWidth + itemWidth > containerWidth) {
                hiddenItems.push(item);
            }
            else {
                totalWidth += itemWidth;
            }
        }
        setAppHeaderHiddenMenuItems(hiddenItems?.map((item) => ({
            label: item.label,
            value: item.path,
            icon: item?.iconName,
        })));
    }, [
        appHeaderMenuItems,
        selectedMenuItem,
        selectedMenu,
        selectedSubMenu,
        selectedQuickMenu,
    ]);
    useEffect(() => {
        const timer = setTimeout(() => {
            calculateVisibleItems();
        }, 800);
        return () => clearTimeout(timer);
    }, [
        projectDetails?.id,
        selectedMenuItem,
        scriptId,
        quickMenuItems,
        selectedSubMenu,
        appHeaderMenuItems,
    ]);
    const checkIsHavingEntityPendingCounts = (data) => {
        if (data.label === 'Approval Request' && data.entityPendingCounts) {
            return Object.values(data.entityPendingCounts).some((count) => count > 0);
        }
        return false;
    };
    useEffect(() => {
        if (appHeaderMenuItems && selectedMenu && selectedSubMenu) {
            const quickMenuItemsData = getQuickMenuItems(appHeaderMenuItems, selectedMenu, selectedSubMenu);
            if (JSON.stringify(quickMenuItemsData) !== JSON.stringify(quickMenuItems)) {
                setQuickMenuItems(quickMenuItemsData || []);
            }
        }
    }, [appHeaderMenuItems, selectedMenu, selectedSubMenu]);
    const getQuickMenuItems = (dataArray, menuLabel, subMenuLabel) => {
        const mainItem = dataArray.find((item) => item.label === menuLabel);
        if (!mainItem) {
            return [];
        }
        const subItem = mainItem?.subMenuItems?.find((sub) => sub.label === subMenuLabel);
        if (!subItem) {
            return [];
        }
        if (subItem.quickMenuItems) {
            return subItem?.quickMenuItems?.filter((quickItem) => !quickItem?.hide);
        }
        else {
            return [];
        }
    };
    return (jsx("div", { className: classNames('ff-app-header-main'), children: jsxs("div", { className: "ff-app-header", style: {
                borderStartStartRadius: borderRadius,
                borderStartEndRadius: borderRadius,
            }, children: [jsxs("div", { className: "ff-app-header-left-container", children: [logo && jsx("div", { className: "ff-app-header-logo-icon", children: logo }), leftContent && (jsx("div", { className: "ff-app-header-left-content", children: leftContent }))] }), jsxs("div", { className: classNames('ff-app-header-nav-bar', {
                        'ff-app-header-nav-bar--with-center-info': !checkEmpty(centerInfoItems) && !checkEmpty(rightButtons),
                    }), style: {
                        width: width,
                        visibility: (checkEmpty(appHeaderMenuItems) &&
                            projectDetails?.id === 'All Projects') ||
                            hideNavbar
                            ? 'hidden'
                            : 'visible',
                        position: 'relative',
                        zIndex: 1,
                    }, ref: menuContainerRef, children: [!checkEmpty(centerInfoItems) && (jsx("div", { className: classNames('ff-app-header-center-info', {
                                'ff-app-header-center-info--no-right': checkEmpty(rightButtons),
                            }), children: centerInfoItems.map((item, idx) => (jsxs("div", { className: "ff-app-header-center-info-item", children: [jsx(Icon, { name: item.iconName, color: item.iconColor }), jsx(Typography, { children: item.label })] }, idx))) })), !checkEmpty(rightButtons) && (jsx("div", { className: "ff-app-header-right-buttons", children: rightButtons.map((btn, index) => (jsxs("button", { className: "ff-app-header-right-button", style: {
                                    color: btn.color,
                                    backgroundColor: btn.backgroundColor,
                                }, onClick: btn.onClick, children: [btn.iconName && jsx(Icon, { name: btn.iconName }), jsx(Typography, { fontSize: 10, fontWeight: "semi-bold", children: btn.label })] }, index))) })), projectArrayList && !checkEmpty(projectArrayList) && (jsx("div", { children: jsx(AllProjectsDropdown, { onClick: handleProjectMenuClick, options: projectArrayList, selectedOption: selectedProject, onMenuClick: handleProjectDropdownLabelClick, selected: selectedMenuItem === 'All Projects', placeholder: "Search Projects", disabled: disabled }) })), jsx("div", { className: "ff-app-header-nav-bar-items fontSm", children: appHeaderMenuItems?.map((menuItem, index) => {
                                return (!appHeaderHiddenMenuItems?.find((menu) => menu.label === menuItem.label) &&
                                    !menuItem.hide &&
                                    menuItem.access !== 'No Access' &&
                                    (menuItem?.isReviewer !== false ||
                                        checkIsHavingEntityPendingCounts(menuItem)) &&
                                    menuItem.label !== 'All Projects' && (jsxs("div", { ref: (element) => (menuItemsRef.current[index] = element), className: classNames('ff-app-header-nav-bar-item', {
                                        ['ff-app-header-nav-bar-item--selected']: menuItem.label === selectedMenuItem,
                                        'ff-app-header-nav-bar-item--disabled': disabled && menuItem.label !== selectedMenuItem,
                                    }), children: [jsx(Typography, { as: "div", className: "ff-app-header-nav-bar-item-label", lineHeight: "18px", onClick: () => handleMenuClick(menuItem), children: menuItem.label }), menuItem.label === selectedMenuItem &&
                                            menuItem?.subMenuItems && (jsxs(Fragment, { children: [menuItem?.subMenuItems?.map((subMenuItem, index) => {
                                                    return (jsx(Fragment$1, { children: !subMenuItem.hide && (jsx("div", { className: "ff-app-header-submenu-container", ref: (element) => (subMenuItemsRef.current[index] = element), children: jsx(Tooltip, { title: subMenuItem.disable
                                                                    ? subMenuItem.disableText
                                                                    : '', placement: "bottom", children: jsx(Typography, { as: "div", className: classNames('ff-app-header-nav-bar-submenu-item', {
                                                                        ['ff-app-header-nav-bar-submenu-item--selected']: subMenuItem.label ===
                                                                            selectedSubMenu,
                                                                        'ff-app-header-nav-bar-submenu-item--disabled': disabled || subMenuItem.disable,
                                                                    }), lineHeight: "18px", onClick: () => !subMenuItem.disable &&
                                                                        handleSubMenuClick(subMenuItem), children: subMenuItem.label }) }) })) }, `subMenuItem.label-${index}`));
                                                }), menuItem?.subMenuItems?.map((subMenuItem, index) => {
                                                    return (jsx(Fragment$1, { children: !subMenuItem.hide && (jsx("div", { className: "ff-app-header-submenu-container", ref: (element) => (quickMenuItemsRef.current[index] =
                                                                element), children: subMenuItem.label === selectedSubMenu &&
                                                                subMenuItem?.quickMenuItems &&
                                                                subMenuItem?.quickMenuItems.length > 0 &&
                                                                (() => {
                                                                    const quickMenuItemsArray = quickMenuItems.slice(0, scriptId ? 10 : 8);
                                                                    const quickMenuHiddenItemsArray = quickMenuItems.slice(scriptId ? 10 : 8);
                                                                    const quickMenuHiddenItems = quickMenuHiddenItemsArray?.map((item) => ({
                                                                        ...item,
                                                                        value: item.path,
                                                                        icon: item.iconName,
                                                                    }));
                                                                    return (jsxs("div", { className: classNames('ff-app-header-quickmenu-container', subMenuItem.quickMenuItems?.length
                                                                            ? 'visible'
                                                                            : ''), children: [!checkEmpty(quickMenuItemsArray) && (jsx("div", { children: jsx(Icon, { name: "vertical_separator" }) })), quickMenuItemsArray?.map((quickMenuItem) => {
                                                                                return (jsx(Fragment$1, { children: !quickMenuItem.hide && (jsx("div", { onClick: () => handleQuickMenuClick(quickMenuItem), className: classNames('ff-app-header-nav-bar-quickmenu-item', {
                                                                                            ['ff-app-header-nav-bar-quickmenu-item--selected']: quickMenuItem.path ===
                                                                                                selectedQuickMenu,
                                                                                        }), children: jsx(Tooltip, { title: quickMenuItem?.label, children: jsx(Icon, { name: quickMenuItem?.iconName ??
                                                                                                    '', height: 16, width: 16, hoverEffect: true }) }) })) }, quickMenuItem.label));
                                                                            }), quickMenuHiddenItemsArray &&
                                                                                !checkEmpty(quickMenuHiddenItemsArray) && (jsx("div", { ref: hiddenQuickMenuRef, children: jsx(MenuOption, { iconName: "more", options: quickMenuHiddenItems, onOptionClick: onQuickMenuClick, dropdownPlacement: "down", zIndex: 1001, tooltipTitle: "More", variant: "light", targetRef: hiddenQuickMenuRef }) }))] }));
                                                                })() })) }, subMenuItem.label));
                                                })] }))] }, menuItem.label)));
                            }) }), appHeaderHiddenMenuItems &&
                            !checkEmpty(appHeaderHiddenMenuItems) && (jsx("div", { className: "more-menu", ref: hiddenMenuRef, children: jsx(MenuOption, { iconName: "more", options: appHeaderHiddenMenuItems, onOptionClick: handleMoreMenuOptionClick, variant: "dark", dropdownPlacement: "down", zIndex: 1001, tooltipTitle: "More", optionCardVariant: "default", targetRef: hiddenMenuRef, disabled: disabled }) }))] }), rightContent && (jsx("div", { className: "ff-app-header-right-content", children: rightContent })), profileContent && (jsx("div", { className: "ff-app-header-profile-content", children: profileContent }))] }) }));
};

export { AppHeader as default };
//# sourceMappingURL=AppHeader.js.map
