import { useEffect } from 'react';
import { menuItemClassnames } from '../../MenuItem';
import { useHidePersonalData } from '../../personalDataSecurity';
import { ErrorService } from '../../services/ErrorService';
import { useMenu } from '../../useMenu';
import { PROFILE_POPOVER_ID } from '../constants';
export const useLogic = ({ menuList, menu: Menu, exitButton, isHidePersonalData, }) => {
    const { open, anchorRef, handleOpenMenu, handleCloseMenu } = useMenu();
    useEffect(() => {
        if (!open) {
            return;
        }
        const observer = new MutationObserver((_, obs) => {
            const popover = document.getElementById(PROFILE_POPOVER_ID);
            if (popover) {
                const menuItems = popover.querySelectorAll(`.${menuItemClassnames.root}`);
                if (menuItems.length > 0) {
                    menuItems.forEach((item) => {
                        item.onclick = handleCloseMenu;
                    });
                    obs.disconnect();
                }
            }
        });
        observer.observe(document.body, {
            childList: true,
            subtree: true,
        });
        return () => observer.disconnect();
    }, [open, handleCloseMenu]);
    useEffect(() => {
        const errorService = ErrorService.getInstance();
        if (!Menu && !menuList && !exitButton) {
            errorService.logError('Profile', 'Profile должен иметь один из следующих props: menu, menuList, exitButton');
        }
    }, []);
    const hidePersonalDataClassname = useHidePersonalData({
        isEnabled: isHidePersonalData,
    });
    return {
        open,
        anchorRef,
        handleOpenMenu,
        handleCloseMenu,
        hidePersonalDataClassname,
    };
};
