import {useAnimatedReaction} from "react-native-reanimated"

import {useBottomSheetToolboxInternalContext} from "../context"
import {useBottomSheetActions} from "./use-bottom-sheet-actions"

export function useBottomSheetOpenWhenReady(initialSnapPointIndex: number) {
    const {isReadySharedValue} = useBottomSheetToolboxInternalContext()
    const {snapToIndex} = useBottomSheetActions("useBottomSheetOpenWhenReady")

    useAnimatedReaction(
        () => isReadySharedValue.get(),
        (ready, lastReady) => {
            if (!lastReady && ready && initialSnapPointIndex !== 0) {
                snapToIndex(initialSnapPointIndex)
            }
        },
        [isReadySharedValue]
    )
}
