"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.useTableSelectionForAgent = useTableSelectionForAgent;
const React = __importStar(require("react"));
const stark_data_1 = require("@ice/stark-data");
const cn_ai_list_event_context_1 = require("../context/cn-ai-list-event-context");
/**
 * 订阅 CnAiTable 发出的单元格/行/列选中与清除事件，维护选中态。
 * 受控场景下外层传入 `selectedCell` 时，展示以 props 为准；内部仍会随事件更新，便于切回非受控时状态连续。
 */
function useTableSelectionForAgent() {
    const eventNames = (0, cn_ai_list_event_context_1.useCnAiListEventNames)();
    const [selectedCell, setSelectedCell] = React.useState(null);
    React.useEffect(() => {
        /**
         * 单元格选中事件处理函数
         * @param cellInfo
         */
        const handleCellSelect = (cellInfo) => {
            setSelectedCell(cellInfo);
        };
        /**
         * 行选中事件处理函数
         * @param cellInfo
         */
        const handleRowSelect = (cellInfo) => {
            setSelectedCell(cellInfo);
        };
        /**
         * 列选中事件处理函数
         * @param cellInfo
         */
        const handleColumnSelect = (cellInfo) => {
            setSelectedCell(cellInfo);
        };
        /**
         * 清除选中态事件处理函数
         */
        const handleClearSelect = () => {
            setSelectedCell(null);
        };
        /**
         * 列清除选中态事件处理函数
         */
        const handleColumnClear = () => {
            setSelectedCell((prev) => ((prev === null || prev === void 0 ? void 0 : prev.type) === 'column' ? null : prev));
        };
        stark_data_1.event.on(eventNames.CELL_SELECT, handleCellSelect);
        stark_data_1.event.on(eventNames.ROW_SELECT, handleRowSelect);
        stark_data_1.event.on(eventNames.COLUMN_SELECT, handleColumnSelect);
        stark_data_1.event.on(eventNames.COLUMN_CLEAR, handleColumnClear);
        stark_data_1.event.on(eventNames.CELL_CLEAR, handleClearSelect);
        return () => {
            stark_data_1.event.off(eventNames.CELL_SELECT, handleCellSelect);
            stark_data_1.event.off(eventNames.ROW_SELECT, handleRowSelect);
            stark_data_1.event.off(eventNames.COLUMN_SELECT, handleColumnSelect);
            stark_data_1.event.off(eventNames.COLUMN_CLEAR, handleColumnClear);
            stark_data_1.event.off(eventNames.CELL_CLEAR, handleClearSelect);
        };
    }, [eventNames]);
    return { selectedCell, setSelectedCell };
}
