import React from 'react';
import {useAtomValue} from 'jotai';
import {selectedDateAtom} from '@/components/pages/calendar/atoms';
import {FlashList} from "@shopify/flash-list";
import {useDidUpdate} from "react-native-ui-lib/src/hooks";

interface CalendarControllerProps {
    scrollViewRef: React.RefObject<FlashList<any>>;
    centerMonthIndex: number;
}

export const CalendarController: React.FC<CalendarControllerProps> = (
    {
        scrollViewRef,
        centerMonthIndex
    }) => {
    const selectedDate = useAtomValue(selectedDateAtom);

    useDidUpdate(() => {
        scrollViewRef.current?.scrollToIndex({
            index: centerMonthIndex,
            animated: false
        })
    }, [selectedDate, centerMonthIndex]);

    return null;
};