import React, { useCallback, useImperativeHandle } from "react";
import { useDispatch } from "react-redux";

import type { OrderData } from "@hooks/apis/auto/useAutoHistory";
import { setScreenTab } from "@store/globalSlice";
import { ScreenTab } from "@store/globalSlice";
import { setInflightData } from "@store/inflightSlice";

export interface BungeeImperativeAPIType {
	setInflightData: (data: OrderData) => void;
}

export const BungeeImperativeAPI = React.forwardRef<BungeeImperativeAPIType>(
	(props, ref) => {
		const dispatch = useDispatch();

		const dispatchInflightData = useCallback(
			(data: OrderData) => {
				dispatch(setInflightData({ data }));
				dispatch(setScreenTab(ScreenTab.INFLIGHT));
			},
			[dispatch],
		);

		useImperativeHandle(
			ref,
			() => ({
				setInflightData: dispatchInflightData,
			}),
			[dispatchInflightData],
		);
		return null;
	},
);
