"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.useFilterAgentToggle = useFilterAgentToggle;
const React = __importStar(require("react"));
const stark_data_1 = require("@ice/stark-data");
const cn_ai_list_event_context_1 = require("../../../cn-ai-agent/context/cn-ai-list-event-context");
/**
 * 供 CnAiFilter 使用：维护筛选条上「AI 助理显隐」图标所需的状态，并通过事件总线与 CnAiAgent / 页面层对齐。
 *
 * @param showAgentIcon - 与 CnAiFilter 的 `showAgentIcon` 一致。为 false 时不注册总线监听、不处理点击切换。
 */
function useFilterAgentToggle(showAgentIcon) {
    const eventNames = (0, cn_ai_list_event_context_1.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);
        };
        stark_data_1.event.on(eventNames.AGENT_VISIBILITY_CHANGE, handleAgentVisibilityChange);
        stark_data_1.event.emit(eventNames.REQUEST_AGENT_STATE);
        return () => {
            stark_data_1.event.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);
        stark_data_1.event.emit(eventNames.AGENT_VISIBILITY_CHANGE, newState);
    }, [showAgentIcon, agentVisible, enableAIAgent, eventNames]);
    return { enableAIAgent, agentVisible, handleToggleAgent };
}
