import { Position } from "../../xyflow/react.js";
import { GRID_SPACING } from "../../constants.js";
const isHorizontalEdge = (position)=>position === Position.Top || position === Position.Bottom;
const isVerticalEdge = (position)=>position === Position.Left || position === Position.Right;
const HANDLE_CROSS_AXIS_SIZE_PX = 24;
const HANDLE_EDGE_COVERAGE_RATIO = 0.5;
const snapToGrid = (value, gridSize = GRID_SPACING)=>Math.round(value / gridSize) * gridSize;
const calculateGridAlignedHandlePositions = (nodeSize, numHandles, gridSize = GRID_SPACING)=>{
    if (0 === numHandles) return [];
    if (nodeSize <= 0) return [];
    if (1 === numHandles) return [
        nodeSize / 2
    ];
    const idealSpacing = nodeSize / (numHandles + 1);
    if (nodeSize % gridSize !== 0) {
        const positions = [];
        for(let i = 0; i < numHandles; i++)positions.push(idealSpacing * (i + 1));
        return positions;
    }
    const roundedSpacing = Math.round(idealSpacing / gridSize) * gridSize;
    let gridAlignedSpacing = Math.max(gridSize, roundedSpacing);
    let totalSpan = (numHandles - 1) * gridAlignedSpacing;
    let startPosition = (nodeSize - totalSpan) / 2;
    if (startPosition % gridSize !== 0 && gridAlignedSpacing > gridSize) {
        gridAlignedSpacing -= gridSize;
        totalSpan = (numHandles - 1) * gridAlignedSpacing;
        startPosition = (nodeSize - totalSpan) / 2;
    }
    const positions = [];
    for(let i = 0; i < numHandles; i++)positions.push(startPosition + i * gridAlignedSpacing);
    return positions;
};
const pixelToPercent = (pixelPosition, nodeSize)=>{
    if (0 === nodeSize) return 0;
    return pixelPosition / nodeSize * 100;
};
const widthForHandleWithPosition = ({ position, numHandles, customWidth })=>{
    if (customWidth) return `${customWidth}px`;
    return isHorizontalEdge(position) ? `${100 * HANDLE_EDGE_COVERAGE_RATIO / numHandles}%` : `${HANDLE_CROSS_AXIS_SIZE_PX}px`;
};
const heightForHandleWithPosition = ({ position, numHandles, customHeight })=>{
    if (customHeight) return `${customHeight}px`;
    return isHorizontalEdge(position) ? `${HANDLE_CROSS_AXIS_SIZE_PX}px` : `${100 * HANDLE_EDGE_COVERAGE_RATIO / numHandles}%`;
};
const topPositionForHandle = ({ position, positionPercent, customHeight, customTop, customBottom })=>{
    if (null != customTop) {
        if (isVerticalEdge(position)) {
            if (customHeight) return `${customTop + positionPercent / 100 * customHeight - customHeight / 2}px`;
            return `calc(${positionPercent}% + ${customTop / 2}px)`;
        }
        return `${customTop}px`;
    }
    if (null != customBottom) return 'unset';
    if (position === Position.Top) return '0';
    if (position === Position.Bottom) return 'unset';
    return `${positionPercent}%`;
};
const bottomPositionForHandle = ({ position, positionPercent, customHeight, customTop, customBottom })=>{
    if (null != customBottom) {
        if (isVerticalEdge(position)) {
            if (customHeight) return `${customBottom + positionPercent / 100 * customHeight - customHeight / 2}px`;
            return `calc(${positionPercent}% + ${customBottom / 2}px)`;
        }
        return `${customBottom}px`;
    }
    if (null != customTop) return 'unset';
    if (position === Position.Bottom) return '0';
    return 'unset';
};
const leftPositionForHandle = ({ position, positionPercent, customWidth, customRight, customLeft })=>{
    if (null != customLeft) {
        if (isHorizontalEdge(position)) {
            if (customWidth) return `${customLeft + positionPercent / 100 * customWidth - customWidth / 2}px`;
            return `calc(${positionPercent}% + ${customLeft / 2}px)`;
        }
        return `${customLeft}px`;
    }
    if (null != customRight) return 'unset';
    if (position === Position.Left) return '0';
    if (position === Position.Right) return 'unset';
    return `${positionPercent}%`;
};
const rightPositionForHandle = ({ position, positionPercent, customWidth, customRight, customLeft })=>{
    if (null != customRight) {
        if (isHorizontalEdge(position)) {
            if (customWidth) return `${customRight + positionPercent / 100 * customWidth - customWidth / 2}px`;
            return `calc(${positionPercent}% + ${customRight / 2}px)`;
        }
        return `${customRight}px`;
    }
    if (null != customLeft) return 'unset';
    if (position === Position.Right) return '0';
    return 'unset';
};
const transformForHandle = ({ position, customPositionAndOffsets })=>{
    const hasCustomVertical = (customPositionAndOffsets?.top != null || customPositionAndOffsets?.bottom != null) && customPositionAndOffsets?.height;
    const hasCustomHorizontal = (customPositionAndOffsets?.left != null || customPositionAndOffsets?.right != null) && customPositionAndOffsets?.width;
    const verticalPercent = hasCustomVertical ? '0%' : position === Position.Bottom ? '50%' : '-50%';
    const horizontalPercent = hasCustomHorizontal ? '0%' : position === Position.Right ? '50%' : '-50%';
    return `translate(${horizontalPercent}, ${verticalPercent})`;
};
export { HANDLE_CROSS_AXIS_SIZE_PX, HANDLE_EDGE_COVERAGE_RATIO, bottomPositionForHandle, calculateGridAlignedHandlePositions, heightForHandleWithPosition, leftPositionForHandle, pixelToPercent, rightPositionForHandle, snapToGrid, topPositionForHandle, transformForHandle, widthForHandleWithPosition };
