import { useReducer } from "react";
import { useOutsideClick } from "../../../hooks";
import { pathReducer } from "../reducers/pathReducer";
import { focusedPathReducer } from "../reducers/focusedPathReducer";
/**
 * Хук для управления логикой открытия/закрытия выпадающего списка.
 */ export const useList = ({ alwaysOpened, disabled, onToggle, closeOnOverlayClick, floatingPopoverRef, listWrapperRef })=>{
    const initialPath = alwaysOpened ? [
        'root'
    ] : [];
    const [path, dispatchPath] = useReducer(pathReducer, initialPath);
    const [focusedPath, dispatchFocusedPath] = useReducer(focusedPathReducer, []);
    /* Callback для глобального открытия/закрытия списка */ const handleGlobalToggle = (opened, event)=>{
        if (disabled) {
            return;
        }
        if (alwaysOpened || opened) {
            dispatchPath({
                type: 'opened_first_level'
            });
        } else {
            dispatchFocusedPath({
                type: 'reset'
            });
            dispatchPath({
                type: 'reset'
            });
        }
        if (onToggle) {
            onToggle(opened, event);
        }
    };
    /* Логика работы при клике за пределами выпадающего списка */ useOutsideClick((event)=>{
        if (!path[0] || !closeOnOverlayClick || alwaysOpened) {
            return;
        }
        dispatchPath({
            type: 'reset'
        });
        dispatchFocusedPath({
            type: 'reset'
        });
        if (onToggle) {
            onToggle(false, event);
        }
    }, [
        floatingPopoverRef,
        listWrapperRef
    ]);
    return {
        path,
        dispatchPath,
        focusedPath,
        dispatchFocusedPath,
        handleGlobalToggle
    };
};
