/**
 * amis-ui v6.12.0-beta.0
 * Copyright 2018-2025 fex
 */

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var tslib = require('tslib');

// 计算向量的模长
function vectorLength(v) {
    return Math.sqrt(v.reduce(function (sum, cur) { return sum + Math.pow(cur, 2); }, 0));
}
// 计算向量的点积
function dotProduct(v1, v2) {
    return v1.reduce(function (sum, cur, index) { return sum + cur * v2[index]; }, 0);
}
// 计算向量的夹角
function vectorAngle(v1, v2) {
    return Math.acos(dotProduct(v1, v2) / (vectorLength(v1) * vectorLength(v2)));
}
// 通过直角边和夹角计算另一直角边长度
function computeSideLength(side, angle) {
    return side / Math.tan(angle);
}
/**
 * 传入起点、终点和距离，返回直线上距离起点距离为distance
 *
 * @param {[number, number]} [startX, startY]
 * @param {[number, number]} [endX, endY]
 * @param {number} distance
 * @returns
 * @memberof SvgPathGenerator
 */
function radiusPoint(_a, _b, distance) {
    var _c = tslib.__read(_a, 2), startX = _c[0], startY = _c[1];
    var _d = tslib.__read(_b, 2), endX = _d[0], endY = _d[1];
    // 计算终点与起点的直线距离
    var dx = endX - startX;
    var dy = endY - startY;
    var totalDistance = Math.sqrt(dx * dx + dy * dy);
    // 如果距离大于总距离的一半，则取一半
    if (distance > totalDistance / 2) {
        distance = totalDistance / 2;
    }
    // 计算比例
    var ratio = distance / totalDistance;
    // 计算新点
    var x = startX + dx * ratio;
    var y = startY + dy * ratio;
    return [x, y];
}
// 传入2个向量和圆角半径，返回圆角弧线的起始点坐标
function radiusStartEndPoint(v1, v2, radius) {
    // 计算向量夹角
    var angle = vectorAngle([v1[2] - v1[0], v1[3] - v1[1]], [v2[2] - v1[0], v2[3] - v1[1]]);
    // 计算3点是顺时针还是逆时针
    var sweepFlag = (v1[2] - v1[0]) * (v2[3] - v1[1]) - (v1[3] - v1[1]) * (v2[2] - v1[0]) < 0
        ? 1
        : 0;
    // 计算短边一半的长度
    var maxLength = Math.min(vectorLength([v1[2] - v1[0], v1[3] - v1[1]]) / 2, vectorLength([v2[2] - v1[0], v2[3] - v1[1]]) / 2);
    // 计算radius的最大值
    var maxRadius = Math.min(radius, maxLength * Math.tan(angle / 2));
    // 直角边的长度
    var distance = computeSideLength(maxRadius, angle / 2);
    var _a = tslib.__read(radiusPoint([v1[0], v1[1]], [v1[2], v1[3]], distance), 2), startX = _a[0], startY = _a[1];
    var _b = tslib.__read(radiusPoint([v2[0], v2[1]], [v2[2], v2[3]], distance), 2), endX = _b[0], endY = _b[1];
    return {
        start: [startX, startY],
        end: [endX, endY],
        radius: maxRadius,
        sweepFlag: sweepFlag
    };
}

exports.computeSideLength = computeSideLength;
exports.dotProduct = dotProduct;
exports.radiusPoint = radiusPoint;
exports.radiusStartEndPoint = radiusStartEndPoint;
exports.vectorAngle = vectorAngle;
exports.vectorLength = vectorLength;
