import { useCallback, useEffect, useState } from 'react';
import { PickerModalState } from './PickerModalState';
import { configure } from '@wix/bex-utils/react-dom';
import { version as cairoVersion } from '../../version';
configure();
export function useCreatePickerModalState() {
    return useCallback((params) => new PickerModalState({
        ...params,
        environment: {
            ...params.environment,
            uiLibVersion: cairoVersion,
        },
    }), []);
}
export function useSyncPickerModalProps(state, { onSelect, disableAutoSelectOnSingleItem, }) {
    useEffect(() => {
        if (!state) {
            return;
        }
        state.onSelectRef.current = onSelect;
    }, [onSelect]);
    useState(() => {
        if (!state) {
            return;
        }
        if (state.isModalOpen) {
            state.prefetch();
        }
        state.disableAutoSelectionRef.current = disableAutoSelectOnSingleItem;
    });
    useEffect(() => {
        if (!state) {
            return;
        }
        state.disableAutoSelectionRef.current = disableAutoSelectOnSingleItem;
    }, [disableAutoSelectOnSingleItem]);
}
export function usePickerModalState(params) {
    const createPickerModal = useCreatePickerModalState();
    const [state] = useState(() => createPickerModal(params));
    useSyncPickerModalProps(state, params);
    return state;
}
//# sourceMappingURL=usePickerModalState.js.map