/*!
 * Copyright (c) 2026 Vertex Software LLC. All rights reserved.
 */
'use strict';

var bundle = require('./bundle-BhTmftd-.js');

/**
 * Returns a new `Point` with the given horizontal and vertical position.
 */
function create(x = 0, y = 0) {
    return { x, y };
}
/**
 * Returns the distance between two points.
 */
function distance(a, b) {
    const delta = subtract(a, b);
    return Math.sqrt(delta.x * delta.x + delta.y * delta.y);
}
/**
 * Returns a new `Point` where `b` is subtracted from `a`.
 */
function subtract(a, b) {
    return { x: a.x - b.x, y: a.y - b.y };
}
/**
 * Returns a new `Point` where `b` is added to `a`.
 */
function add(a, b) {
    return { x: a.x + b.x, y: a.y + b.y };
}
/**
 * Returns a new `Point` where `x` and `y` are multiplied by the given scale
 * factor.
 */
function scaleProportional(pt, scale) {
    return {
        x: pt.x * scale,
        y: pt.y * scale,
    };
}
/**
 * Returns the magnitude of a point.
 */
function magnitude(pt) {
    return Math.sqrt(pt.x * pt.x + pt.y * pt.y);
}
/**
 * Transforms a vector into the corresponding normal (unit) vector.
 */
function normalizeVector(pt) {
    const magnitudeOfPoint = magnitude(pt);
    if (magnitudeOfPoint === 0) {
        return create(0, 0);
    }
    else {
        return scaleProportional(pt, 1 / magnitudeOfPoint);
    }
}
/**
 * Returns a new normal (unit) vector pointing between the two given points.
 */
function normalDirectionVector(ptA, ptB) {
    return normalizeVector(subtract(ptB, ptA));
}
/**
 * Returns a vector orthogonal to the vector between the two given points.
 */
function orthogonalVector(ptA, ptB) {
    const unitVectorBetweenPoints = normalDirectionVector(ptA, ptB);
    // Handle vectors that are parallel to the x or y axis
    if (unitVectorBetweenPoints.x === 0 || unitVectorBetweenPoints.y === 0) {
        return create(-1 * unitVectorBetweenPoints.y, unitVectorBetweenPoints.x);
    }
    if (Math.abs(unitVectorBetweenPoints.x) > Math.abs(unitVectorBetweenPoints.y)) {
        const vectorXValue = 1 - Math.pow(unitVectorBetweenPoints.x, 2);
        const vectorYValue = -1 * unitVectorBetweenPoints.x * unitVectorBetweenPoints.y;
        return normalizeVector(create(vectorXValue, vectorYValue));
    }
    else {
        const vectorXValue = -1 * unitVectorBetweenPoints.x * unitVectorBetweenPoints.y;
        const vectorYValue = 1 - Math.pow(unitVectorBetweenPoints.y, 2);
        return normalizeVector(create(vectorXValue, vectorYValue));
    }
}
/**
 * Parses a JSON string representation of a Point and returns an object.
 *
 * @param json A JSON string, either in the form `[x,y]` or `{"x": 0, "y": 0}`
 * @returns A parsed Point.
 */
function fromJson(json) {
    const obj = JSON.parse(json);
    if (Array.isArray(obj)) {
        const [x, y] = obj;
        return create(x, y);
    }
    else {
        const { x, y } = obj;
        return create(x, y);
    }
}

function createArrowheadPoints(start, end, arrowSideLength, arrowHeadTheta) {
    const arrowOrthogonalVector = orthogonalVector(start, end);
    const normalizedDirection = normalDirectionVector(start, end);
    const arrowLeft = subtract(end, add(scaleProportional(normalizedDirection, arrowSideLength * Math.cos(arrowHeadTheta)), scaleProportional(arrowOrthogonalVector, arrowSideLength * Math.sin(arrowHeadTheta))));
    const arrowRight = subtract(end, subtract(scaleProportional(normalizedDirection, arrowSideLength * Math.cos(arrowHeadTheta)), scaleProportional(arrowOrthogonalVector, arrowSideLength * Math.sin(arrowHeadTheta))));
    return {
        leftPoint: arrowLeft,
        rightPoint: arrowRight,
    };
}
function createLineAnchorStylePoints(start, end, triangleArrowAngle = 65, lineArrowAngle = 85) {
    // Adjust the size of the end style to the distance between the start and end points
    const distance$1 = distance(start, end);
    const arrowHeadHeight = Math.max(4, Math.min(16, distance$1 * 0.25));
    const hashHeight = Math.max(4, Math.min(12, distance$1 * 0.16));
    const radius = Math.min(5, distance$1 * 0.1);
    // Triangle arrow position
    const triangleArrowRelativeHeight = arrowHeadHeight / distance$1;
    const triangleArrowBasePosition = add(scaleProportional(end, 1 - triangleArrowRelativeHeight), scaleProportional(start, triangleArrowRelativeHeight));
    const triangleArrowTheta = bundle.angle.toRadians(triangleArrowAngle / 2);
    const triangleArrowSideLength = arrowHeadHeight / Math.cos(triangleArrowTheta);
    const triangleArrow = createArrowheadPoints(start, end, triangleArrowSideLength, triangleArrowTheta);
    // Line arrow position
    const lineArrowTheta = bundle.angle.toRadians(lineArrowAngle / 2);
    const lineArrowSideLength = arrowHeadHeight / Math.cos(lineArrowTheta);
    const lineArrow = createArrowheadPoints(start, end, lineArrowSideLength, lineArrowTheta);
    // Hash position
    // Setting the arrowHeadAngle to 90 degrees results in a
    // straight hash mark perpendicular to the direction of the line
    const hashLineTheta = bundle.angle.toRadians(90);
    const hashLine = createArrowheadPoints(start, end, hashHeight, hashLineTheta);
    return {
        tip: end,
        base: triangleArrowBasePosition,
        arrowTriangle: triangleArrow,
        arrowLine: lineArrow,
        hash: hashLine,
        radius: radius,
    };
}
function arrowheadPointsToPolygonPoints(points, scale = 1) {
    const scaledRight = scalePointProportional(points.tip, points.arrowTriangle.rightPoint, scale);
    const scaledLeft = scalePointProportional(points.tip, points.arrowTriangle.leftPoint, scale);
    const scaledBase = scalePointProportional(points.tip, points.base, scale);
    return [points.tip, scaledRight, scaledBase, scaledLeft]
        .map((pt) => `${pt.x},${pt.y}`)
        .join(' ');
}
function arrowheadPointsToPathPoints(points, scale = 1) {
    const scaledRight = scalePointProportional(points.tip, points.arrowLine.rightPoint, scale);
    const scaledLeft = scalePointProportional(points.tip, points.arrowLine.leftPoint, scale);
    return `M${scaledRight.x} ${scaledRight.y} L${points.tip.x} ${points.tip.y} L${scaledLeft.x} ${scaledLeft.y} L${points.tip.x} ${points.tip.y} Z`;
}
function arrowheadPointsToHashPoints(points, scale = 1) {
    const scaledRight = scalePointProportional(points.tip, points.hash.rightPoint, scale);
    const scaledLeft = scalePointProportional(points.tip, points.hash.leftPoint, scale);
    return {
        x1: scaledRight.x,
        y1: scaledRight.y,
        x2: scaledLeft.x,
        y2: scaledLeft.y,
    };
}
function arrowheadPointsToCirclePoints(points, scale = 1) {
    return {
        cx: points.tip.x,
        cy: points.tip.y,
        r: points.radius * scale,
    };
}
function isVertexViewerArrowMarkup(el) {
    return (el instanceof HTMLElement && el.nodeName === 'VERTEX-VIEWER-MARKUP-ARROW');
}
function parsePoint(value) {
    return typeof value === 'string' ? fromJson(value) : value;
}
function scalePointProportional(basePoint, scaledPoint, scale) {
    return add(basePoint, scaleProportional(subtract(scaledPoint, basePoint), scale));
}

function isVertexViewerCircleMarkup(el) {
    return (el instanceof HTMLElement && el.nodeName === 'VERTEX-VIEWER-MARKUP-CIRCLE');
}
function parseBounds(value) {
    return typeof value === 'string' ? bundle.rectangle.fromJson(value) : value;
}

function parsePoints(value) {
    return typeof value === 'string'
        ? JSON.parse(value).map((values) => bundle.point.fromJson(JSON.stringify(values)))
        : value;
}
function isVertexViewerFreeformMarkup(el) {
    return (el instanceof HTMLElement && el.nodeName === 'VERTEX-VIEWER-MARKUP-FREEFORM');
}

exports.arrowheadPointsToCirclePoints = arrowheadPointsToCirclePoints;
exports.arrowheadPointsToHashPoints = arrowheadPointsToHashPoints;
exports.arrowheadPointsToPathPoints = arrowheadPointsToPathPoints;
exports.arrowheadPointsToPolygonPoints = arrowheadPointsToPolygonPoints;
exports.createLineAnchorStylePoints = createLineAnchorStylePoints;
exports.isVertexViewerArrowMarkup = isVertexViewerArrowMarkup;
exports.isVertexViewerCircleMarkup = isVertexViewerCircleMarkup;
exports.isVertexViewerFreeformMarkup = isVertexViewerFreeformMarkup;
exports.parseBounds = parseBounds;
exports.parsePoint = parsePoint;
exports.parsePoints = parsePoints;
//# sourceMappingURL=utils-m4ihMvcS.js.map

//# sourceMappingURL=utils-m4ihMvcS.js.map