import * as React from 'react';
import { event as eventBus } from '@ice/stark-data';
import { useCnAiListEventNames } from '../../../cn-ai-agent/context/cn-ai-list-event-context';
/**
 * 供 CnAiFilter 使用：维护筛选条上「AI 助理显隐」图标所需的状态，并通过事件总线与 CnAiAgent / 页面层对齐。
 *
 * @param showAgentIcon - 与 CnAiFilter 的 `showAgentIcon` 一致。为 false 时不注册总线监听、不处理点击切换。
 */
export function useFilterAgentToggle(showAgentIcon) {
    const eventNames = useCnAiListEventNames();
    const [agentState, setAgentState] = React.useState({
        /**
         * 是否启用 AI 助理
         */
        enabled: false,
        /**
         * 是否显示 AI 助理
         */
        visible: false,
    });
    const enableAIAgent = agentState.enabled;
    const agentVisible = agentState.visible;
    React.useEffect(() => {
        /**
         * 如果未启用筛选条上的 AI 助理显隐切换，则不订阅总线、不响应切换
         */
        if (!showAgentIcon)
            return;
        /**
         * 订阅总线事件，更新 AI 助理显隐状态
         */
        const handleAgentVisibilityChange = (state) => {
            setAgentState(state);
        };
        eventBus.on(eventNames.AGENT_VISIBILITY_CHANGE, handleAgentVisibilityChange);
        eventBus.emit(eventNames.REQUEST_AGENT_STATE);
        return () => {
            eventBus.off(eventNames.AGENT_VISIBILITY_CHANGE, handleAgentVisibilityChange);
        };
    }, [showAgentIcon, eventNames]);
    /**
     * 点击图标切换 AI 助理显隐
     */
    const handleToggleAgent = React.useCallback(() => {
        if (!showAgentIcon)
            return;
        const newVisible = !agentVisible;
        const newState = { enabled: enableAIAgent, visible: newVisible };
        setAgentState(newState);
        eventBus.emit(eventNames.AGENT_VISIBILITY_CHANGE, newState);
    }, [showAgentIcon, agentVisible, enableAIAgent, eventNames]);
    return { enableAIAgent, agentVisible, handleToggleAgent };
}
