"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useAgentVisibilityWithFilter = useAgentVisibilityWithFilter;
const react_1 = require("react");
const stark_data_1 = require("@ice/stark-data");
const cn_ai_list_event_context_1 = require("../context/cn-ai-list-event-context");
/**
 * 与 CnAiFilter 中的图标通过事件总线同步 AI 助理显隐（显隐由父组件受控）。
 */
function useAgentVisibilityWithFilter({ agentVisible, onAgentVisibleChange, }) {
    const eventNames = (0, cn_ai_list_event_context_1.useCnAiListEventNames)();
    /**
     * 助理面板是否可见
     */
    const visible = agentVisible;
    const visibleRef = (0, react_1.useRef)(visible);
    visibleRef.current = visible;
    const onAgentVisibleChangeRef = (0, react_1.useRef)(onAgentVisibleChange);
    onAgentVisibleChangeRef.current = onAgentVisibleChange;
    (0, react_1.useEffect)(() => {
        const handleFromBus = (state) => {
            /**
             * 如果未启用助理显隐联动，则不订阅总线、不响应显隐变化
             */
            if (!state.enabled)
                return;
            /**
             * 如果助理面板已可见，则不响应显隐变化
             */
            if (state.visible === visibleRef.current)
                return;
            /**
             * 更新助理面板是否可见
             */
            onAgentVisibleChangeRef.current(state.visible);
        };
        /**
         * 请求助理当前状态，用于组件挂载时同步状态
         */
        const handleRequest = () => {
            stark_data_1.event.emit(eventNames.AGENT_VISIBILITY_CHANGE, {
                enabled: true,
                visible: visibleRef.current,
            });
        };
        stark_data_1.event.on(eventNames.AGENT_VISIBILITY_CHANGE, handleFromBus);
        stark_data_1.event.on(eventNames.REQUEST_AGENT_STATE, handleRequest);
        return () => {
            stark_data_1.event.off(eventNames.AGENT_VISIBILITY_CHANGE, handleFromBus);
            stark_data_1.event.off(eventNames.REQUEST_AGENT_STATE, handleRequest);
        };
    }, [eventNames]);
    (0, react_1.useEffect)(() => {
        visibleRef.current = visible;
        stark_data_1.event.emit(eventNames.AGENT_VISIBILITY_CHANGE, {
            enabled: true,
            visible,
        });
    }, [visible, eventNames]);
    const panelHidden = !visible;
    return { visible, panelHidden };
}
