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