import { jsx } from "react/jsx-runtime";
import { createContext, useCallback, useContext, useEffect, useState } from "react";
import { AutopilotChatEvent } from "../service/index.js";
import { useChatService } from "./chat-service.provider.js";
const PickerContext = /*#__PURE__*/ createContext({
    models: [],
    selectedModel: void 0,
    handleModelChange: ()=>{},
    agentModes: [],
    selectedAgentMode: void 0,
    handleAgentModeChange: ()=>{},
    customHeaderActions: [],
    handleCustomHeaderAction: ()=>{}
});
const AutopilotPickerProvider = ({ children })=>{
    const chatService = useChatService();
    const [models, setModels] = useState([]);
    const [selectedModel, setSelectedModel] = useState();
    const [agentModes, setAgentModes] = useState([]);
    const [selectedAgentMode, setSelectedAgentMode] = useState();
    const [customHeaderActions, setCustomHeaderActions] = useState([]);
    useEffect(()=>{
        if (!chatService) return;
        const unsubscribeModels = chatService.on(AutopilotChatEvent.SetModels, (newModels)=>{
            setModels(newModels);
        });
        const unsubscribeSelectedModel = chatService.on(AutopilotChatEvent.SetSelectedModel, (model)=>{
            setSelectedModel(model);
        });
        const unsubscribeAgentModes = chatService.on(AutopilotChatEvent.SetAgentModes, (modes)=>{
            setAgentModes(modes);
        });
        const unsubscribeSelectedAgentMode = chatService.on(AutopilotChatEvent.SetSelectedAgentMode, (mode)=>{
            setSelectedAgentMode(mode);
        });
        const unsubscribeCustomHeaderActions = chatService.on(AutopilotChatEvent.SetCustomHeaderActions, (actions)=>{
            setCustomHeaderActions(actions);
        });
        if (chatService.getModels) setModels(chatService.getModels() ?? []);
        if (chatService.getSelectedModel) setSelectedModel(chatService.getSelectedModel());
        if (chatService.getAgentModes) setAgentModes(chatService.getAgentModes() ?? []);
        if (chatService.getAgentMode) setSelectedAgentMode(chatService.getAgentMode());
        if (chatService.getCustomHeaderActions) setCustomHeaderActions(chatService.getCustomHeaderActions() ?? []);
        return ()=>{
            unsubscribeModels();
            unsubscribeSelectedModel();
            unsubscribeAgentModes();
            unsubscribeSelectedAgentMode();
            unsubscribeCustomHeaderActions();
        };
    }, [
        chatService
    ]);
    const handleModelChange = useCallback((modelId)=>{
        chatService?.setSelectedModel(modelId);
    }, [
        chatService
    ]);
    const handleAgentModeChange = useCallback((modeId)=>{
        chatService?.setAgentMode(modeId);
    }, [
        chatService
    ]);
    const handleCustomHeaderAction = useCallback((action)=>{
        chatService?._eventBus.publish(AutopilotChatEvent.CustomHeaderActionClicked, action);
    }, [
        chatService
    ]);
    return /*#__PURE__*/ jsx(PickerContext.Provider, {
        value: {
            models,
            selectedModel,
            handleModelChange,
            agentModes,
            selectedAgentMode,
            handleAgentModeChange,
            customHeaderActions,
            handleCustomHeaderAction
        },
        children: children
    });
};
const usePicker = ()=>useContext(PickerContext);
export { AutopilotPickerProvider, usePicker };
