import { useEffect, useMemo, useRef } from 'react';
import { getFormatDisabledItems } from '../../Tree/utils';
import { useId } from '../../useId';
import { getChainsId } from './utils';
export const useLogic = ({ data, value, disabledItems, }) => {
    const prefixId = useId();
    const listRef = useRef(null);
    useEffect(() => {
        if (listRef.current && value?.length) {
            // Выбираем первый элемент из списка value
            const firstItemId = typeof value[0] === 'string' ? value[0] : value[0].id;
            const targetItem = listRef.current.querySelector(`
        li[id="${prefixId}${firstItemId}"]`);
            if (targetItem) {
                targetItem.scrollIntoView({ block: 'center' });
            }
        }
    }, [listRef, prefixId]);
    const formattedDisabledItems = useMemo(() => getFormatDisabledItems(disabledItems), [disabledItems]);
    const chainsToSelectedItem = useMemo(() => getChainsId(data, value), [data, value]);
    return {
        listProps: {
            ref: listRef,
        },
        itemProps: {
            chainsToSelectedItem,
            disabledItems: formattedDisabledItems,
            prefixId,
        },
    };
};
