"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.handleRowDragSort = exports.handleColumnDragSort = exports.useDragAndDrop = void 0;
const React = __importStar(require("react"));
const core_1 = require("@dnd-kit/core");
const constants_1 = require("./constants");
const drag_sort_1 = require("./drag-sort");
const primary_key_1 = require("../../../../../../cn-ai-table/view/utils/primary-key");
/**
 * 拖拽功能 Hook
 * 封装行拖拽和列拖拽的通用逻辑
 */
const useDragAndDrop = (options = {}) => {
    const { activationDistance = 5 } = options;
    // 拖拽类型引用（区分行拖拽和列拖拽）
    const dragTypeRef = React.useRef(null);
    // 当前正在拖拽的列 ID
    const [activeColumnId, setActiveColumnId] = React.useState(null);
    // 当前正在拖拽的行 ID
    const [activeRowId, setActiveRowId] = React.useState(null);
    // 当前拖拽类型（用于区分列拖拽和行拖拽的悬停指示器）
    const [currentDragType, setCurrentDragType] = React.useState(null);
    // 拖拽传感器配置
    const sensors = (0, core_1.useSensors)((0, core_1.useSensor)(core_1.PointerSensor, { activationConstraint: { distance: activationDistance } }));
    // 处理拖拽开始
    const handleDragStart = React.useCallback((event) => {
        var _a, _b;
        const dragType = (_b = (_a = event.active.data.current) === null || _a === void 0 ? void 0 : _a.type) !== null && _b !== void 0 ? _b : null;
        dragTypeRef.current = dragType;
        setCurrentDragType(dragType);
        if (dragType === constants_1.DRAG_TYPES.COLUMN) {
            setActiveColumnId(String(event.active.id));
        }
        else if (dragType === constants_1.DRAG_TYPES.ROW) {
            setActiveRowId(String(event.active.id));
        }
    }, []);
    // 创建拖拽结束处理函数（不使用 useCallback，确保每次都能获取最新的 dragTypeRef）
    const createDragEndHandler = (callbacks) => {
        return (event) => {
            const { active, over } = event;
            const dragType = dragTypeRef.current;
            dragTypeRef.current = null;
            // 清除拖拽状态
            setActiveColumnId(null);
            setActiveRowId(null);
            setCurrentDragType(null);
            if (!over || active.id === over.id)
                return;
            const activeId = active.id;
            const overId = over.id;
            if (dragType === constants_1.DRAG_TYPES.COLUMN && callbacks.onColumnDragEnd) {
                callbacks.onColumnDragEnd(activeId, overId);
            }
            else if (dragType === constants_1.DRAG_TYPES.ROW && callbacks.onRowDragEnd) {
                callbacks.onRowDragEnd(activeId, overId);
            }
        };
    };
    return {
        activeColumnId,
        activeRowId,
        currentDragType,
        dragTypeRef,
        setActiveColumnId,
        setActiveRowId,
        setCurrentDragType,
        sensors,
        handleDragStart,
        createDragEndHandler,
    };
};
exports.useDragAndDrop = useDragAndDrop;
/**
 * 列拖拽排序（兼容包装器）
 *
 * 内部委托 sortColumns，返回 `Array | null` 保持旧调用方式。
 * 新代码推荐直接使用 `sortColumns` 获取带失败原因的 Result。
 */
const handleColumnDragSort = (columnOrder, activeId, overId, pinnedColumnIds) => {
    const result = (0, drag_sort_1.sortColumns)(columnOrder, activeId, overId, pinnedColumnIds);
    return result.ok ? result.value : null;
};
exports.handleColumnDragSort = handleColumnDragSort;
/**
 * 行拖拽排序（兼容包装器）
 *
 * 内部委托 sortRows（使用 getRowKeyStrict，不做 index 兜底）。
 * 新代码推荐直接使用 `sortRows`。
 */
const handleRowDragSort = (data, activeId, overId, primaryKey = primary_key_1.DEFAULT_PRIMARY_KEY) => {
    const result = (0, drag_sort_1.sortRows)(data, activeId, overId, primaryKey);
    return result.ok ? result.value : null;
};
exports.handleRowDragSort = handleRowDragSort;
