"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.d = (exports1, definition)=>{
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
})();
(()=>{
    __webpack_require__.r = (exports1)=>{
        if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    reorderTasks: ()=>reorderTasks,
    flattenTasks: ()=>flattenTasks,
    getProjection: ()=>getProjection,
    buildTaskGroups: ()=>buildTaskGroups
});
const sortable_namespaceObject = require("@dnd-kit/sortable");
const external_StageNode_styles_cjs_namespaceObject = require("./StageNode.styles.cjs");
function flattenTasks(tasks) {
    const flattened = [];
    for (const [groupIndex, group] of tasks.entries()){
        if (!group) continue;
        const depth = group.length > 1 ? 1 : 0;
        for (const [taskIndex, task] of group.entries())if (task) flattened.push({
            id: task.id,
            task,
            groupIndex,
            taskIndex,
            depth
        });
    }
    return flattened;
}
function buildTaskGroups(flattenedTasks) {
    const groups = [];
    let currentGroup = [];
    let previousDepth = null;
    let previousGroupIndex = null;
    for (const item of flattenedTasks){
        if (null === previousDepth) currentGroup.push(item.task);
        else if (1 === item.depth && 1 === previousDepth && item.groupIndex === previousGroupIndex) currentGroup.push(item.task);
        else {
            if (currentGroup.length > 0) groups.push(currentGroup);
            currentGroup = [
                item.task
            ];
        }
        previousDepth = item.depth;
        previousGroupIndex = item.groupIndex;
    }
    if (currentGroup.length > 0) groups.push(currentGroup);
    return groups;
}
function reorderTasks(tasks, activeId, overId, projectedDepth) {
    const flattened = flattenTasks(tasks);
    const activeIndex = flattened.findIndex((t)=>t.id === activeId);
    const overIndex = flattened.findIndex((t)=>t.id === overId);
    if (-1 === activeIndex || -1 === overIndex) return tasks;
    const activeTask = flattened[activeIndex];
    if (!activeTask) return tasks;
    const reordered = (0, sortable_namespaceObject.arrayMove)(flattened, activeIndex, overIndex);
    const movedItem = reordered[overIndex];
    if (movedItem) {
        let newGroupIndex = movedItem.groupIndex;
        if (1 === projectedDepth) {
            const prevItem = reordered[overIndex - 1];
            const nextItem = reordered[overIndex + 1];
            if (prevItem?.depth === 1) newGroupIndex = prevItem.groupIndex;
            else if (nextItem?.depth === 1) newGroupIndex = nextItem.groupIndex;
        }
        reordered[overIndex] = {
            ...movedItem,
            depth: projectedDepth,
            groupIndex: newGroupIndex
        };
    }
    return buildTaskGroups(reordered);
}
function getDragDepth(offsetLeft) {
    return Math.round(offsetLeft / external_StageNode_styles_cjs_namespaceObject.INDENTATION_WIDTH);
}
function getParallelGroupInfo(tasks, groupIndex, taskIndex) {
    const group = tasks[groupIndex];
    if (!group || group.length <= 1) return {
        isParallel: false,
        isFirstItem: false,
        isLastItem: false
    };
    return {
        isParallel: true,
        isFirstItem: 0 === taskIndex,
        isLastItem: taskIndex === group.length - 1
    };
}
function getProjection(tasks, activeId, overId, offsetLeft) {
    const flattened = flattenTasks(tasks);
    const activeIndex = flattened.findIndex((t)=>t.id === activeId);
    const overIndex = flattened.findIndex((t)=>t.id === overId);
    if (-1 === activeIndex || -1 === overIndex) return null;
    const activeTask = flattened[activeIndex];
    const overTask = flattened[overIndex];
    if (!activeTask || !overTask) return null;
    const overInfo = getParallelGroupInfo(tasks, overTask.groupIndex, overTask.taskIndex);
    const minDepth = 0;
    const maxDepth = 1;
    let depth = 0;
    const isActiveParallel = 1 === activeTask.depth;
    const dragDepth = isActiveParallel ? offsetLeft > 0 ? 1 : 0 : getDragDepth(offsetLeft);
    const isDraggingDown = activeIndex < overIndex;
    const isDraggingUp = activeIndex > overIndex;
    const isDraggingInPlace = activeIndex === overIndex;
    if (isDraggingInPlace) if (overInfo.isParallel) {
        const isOnBorder = overInfo.isFirstItem || overInfo.isLastItem;
        depth = isOnBorder ? Math.max(minDepth, Math.min(maxDepth, dragDepth)) : maxDepth;
    } else {
        const nextTask = flattened[overIndex + 1];
        const prevTask = flattened[overIndex - 1];
        const isAdjacentToParallel = nextTask?.depth === 1 || prevTask?.depth === 1;
        if (isAdjacentToParallel) depth = Math.max(minDepth, Math.min(maxDepth, dragDepth));
    }
    else if (overInfo.isParallel) {
        const isEdgeInsertion = isDraggingUp && overInfo.isFirstItem || isDraggingDown && overInfo.isLastItem;
        depth = isEdgeInsertion ? Math.max(minDepth, Math.min(maxDepth, dragDepth)) : maxDepth;
    } else {
        const nextTask = flattened[overIndex + 1];
        const prevTask = flattened[overIndex - 1];
        const isAdjacentToParallel = isDraggingDown && nextTask?.depth === 1 || isDraggingUp && prevTask?.depth === 1;
        if (isAdjacentToParallel) depth = Math.max(minDepth, Math.min(maxDepth, dragDepth));
    }
    return {
        depth,
        maxDepth,
        groupIndex: overTask.groupIndex,
        taskIndex: overTask.taskIndex
    };
}
exports.buildTaskGroups = __webpack_exports__.buildTaskGroups;
exports.flattenTasks = __webpack_exports__.flattenTasks;
exports.getProjection = __webpack_exports__.getProjection;
exports.reorderTasks = __webpack_exports__.reorderTasks;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "buildTaskGroups",
    "flattenTasks",
    "getProjection",
    "reorderTasks"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
