"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__, {
    moveWaypoint: ()=>moveWaypoint,
    removeWaypoint: ()=>removeWaypoint,
    materializePathWaypoints: ()=>materializePathWaypoints,
    waypointsEqual: ()=>waypointsEqual,
    rebalanceWaypoints: ()=>rebalanceWaypoints,
    insertWaypoint: ()=>insertWaypoint,
    waypointsPositionallyEqual: ()=>waypointsPositionallyEqual,
    moveSegmentByDelta: ()=>moveSegmentByDelta,
    generateWaypointId: ()=>generateWaypointId
});
const NodeUtils_cjs_namespaceObject = require("../../../utils/NodeUtils.cjs");
const external_constants_cjs_namespaceObject = require("./constants.cjs");
const external_geometry_cjs_namespaceObject = require("./geometry.cjs");
function materializePathWaypoints(vertices, stored) {
    return vertices.slice(1, -1).map((vertex)=>{
        const existing = vertex.waypointIndex >= 0 ? stored[vertex.waypointIndex] : void 0;
        return existing ?? {
            id: generateWaypointId(),
            x: vertex.x,
            y: vertex.y
        };
    });
}
function generateWaypointId() {
    return `wp-${crypto.randomUUID()}`;
}
function waypointsEqual(a, b) {
    if (a === b) return true;
    if (a.length !== b.length) return false;
    for(let i = 0; i < a.length; i++){
        const wa = a[i];
        const wb = b[i];
        if (wa.x !== wb.x || wa.y !== wb.y || wa.id !== wb.id) return false;
    }
    return true;
}
function waypointsPositionallyEqual(a, b) {
    if (a === b) return true;
    if (a.length !== b.length) return false;
    for(let i = 0; i < a.length; i++)if (a[i].x !== b[i].x || a[i].y !== b[i].y) return false;
    return true;
}
function insertWaypoint(vertices, stored, segmentIndex, position) {
    const newWaypoint = {
        id: generateWaypointId(),
        ...(0, external_geometry_cjs_namespaceObject.snapPointToGrid)(position)
    };
    const materialized = materializePathWaypoints(vertices, stored);
    materialized.splice(Math.max(0, segmentIndex), 0, newWaypoint);
    return materialized;
}
function removeWaypoint(waypoints, index) {
    return waypoints.filter((_, i)=>i !== index);
}
const REBALANCE_EPSILON = 0.5;
function rebalanceWaypoints(waypoints, source, target) {
    if (0 === waypoints.length) return waypoints;
    const sourceDelta = waypoints_delta(source);
    const targetDelta = waypoints_delta(target);
    if (isMoved(sourceDelta) && deltasEqual(sourceDelta, targetDelta)) return waypoints.map((w)=>({
            id: w.id,
            x: w.x + sourceDelta.x,
            y: w.y + sourceDelta.y
        }));
    const next = waypoints.map((w)=>({
            ...w
        }));
    if (1 === next.length) {
        const sourceMoved = isMoved(sourceDelta);
        const targetMoved = isMoved(targetDelta);
        if (sourceMoved && !targetMoved) shiftAdjacentWaypoint(next[0], source);
        else if (targetMoved && !sourceMoved) shiftAdjacentWaypoint(next[0], target);
        return next;
    }
    shiftAdjacentWaypoint(next[0], source);
    shiftAdjacentWaypoint(next[next.length - 1], target);
    return next;
}
function waypoints_delta(endpoint) {
    return {
        x: endpoint.to.x - endpoint.from.x,
        y: endpoint.to.y - endpoint.from.y
    };
}
function isMoved(d) {
    return Math.abs(d.x) >= REBALANCE_EPSILON || Math.abs(d.y) >= REBALANCE_EPSILON;
}
function deltasEqual(a, b) {
    return Math.abs(a.x - b.x) < REBALANCE_EPSILON && Math.abs(a.y - b.y) < REBALANCE_EPSILON;
}
function shiftAdjacentWaypoint(waypoint, endpoint) {
    if ((0, external_geometry_cjs_namespaceObject.isHorizontalPosition)(endpoint.position)) waypoint.y += endpoint.to.y - endpoint.from.y;
    else waypoint.x += endpoint.to.x - endpoint.from.x;
}
function moveWaypoint(waypoints, index, newPosition) {
    const snapped = (0, external_geometry_cjs_namespaceObject.snapPointToGrid)(newPosition);
    return waypoints.map((wp, i)=>i === index ? {
            ...wp,
            ...snapped
        } : wp);
}
function moveSegmentByDelta(initialWaypoints, initialSegment, delta, initialPathPoints) {
    const moveAxis = 'horizontal' === initialSegment.orientation ? 'y' : 'x';
    const moveValue = 'y' === moveAxis ? delta.y : delta.x;
    const working = initialWaypoints.map((wp)=>({
            ...wp
        }));
    const isConnectedToSource = initialSegment.waypointIndexBefore < 0;
    const isConnectedToTarget = initialSegment.waypointIndexAfter >= working.length;
    let insertedAtStart = false;
    const stubOffset = external_constants_cjs_namespaceObject.EDGE_CONSTANTS.STUB_OFFSET;
    const orientation = initialSegment.orientation;
    const segmentStart = initialSegment.start;
    const segmentEnd = initialSegment.end;
    const pathSource = initialPathPoints[0];
    const pathTarget = initialPathPoints[initialPathPoints.length - 1];
    const movedCoordinate = 'horizontal' === orientation ? (0, NodeUtils_cjs_namespaceObject.snapToGrid)(segmentStart.y + moveValue) : (0, NodeUtils_cjs_namespaceObject.snapToGrid)(segmentStart.x + moveValue);
    if (isConnectedToSource) {
        const stubCoord = (0, NodeUtils_cjs_namespaceObject.snapToGrid)(('horizontal' === orientation ? segmentStart.x : segmentStart.y) + stubOffset);
        const [anchorStub, movedStub] = makeStubPair(segmentStart, orientation, stubCoord, movedCoordinate);
        working.unshift(anchorStub, movedStub);
        insertedAtStart = true;
    }
    if (isConnectedToTarget) {
        const stubCoord = (0, NodeUtils_cjs_namespaceObject.snapToGrid)(('horizontal' === orientation ? segmentEnd.x : segmentEnd.y) - stubOffset);
        const [anchorStub, movedStub] = makeStubPair(segmentEnd, orientation, stubCoord, movedCoordinate);
        working.push(movedStub, anchorStub);
    }
    const indexOffset = insertedAtStart ? 2 : 0;
    if (!isConnectedToSource && initialSegment.waypointIndexBefore >= 0) {
        const idx = initialSegment.waypointIndexBefore + indexOffset;
        const wp = working[idx];
        if (wp) wp[moveAxis] = movedCoordinate;
    }
    if (!isConnectedToTarget && initialSegment.waypointIndexAfter < initialWaypoints.length) {
        const idx = initialSegment.waypointIndexAfter + indexOffset;
        const wp = working[idx];
        if (wp) wp[moveAxis] = movedCoordinate;
    }
    return (0, external_geometry_cjs_namespaceObject.consolidateWaypoints)(working, pathSource, pathTarget);
}
function makeStubPair(anchor, orientation, stubCoord, movedCoordinate) {
    if ('horizontal' === orientation) return [
        {
            id: generateWaypointId(),
            x: stubCoord,
            y: anchor.y
        },
        {
            id: generateWaypointId(),
            x: stubCoord,
            y: movedCoordinate
        }
    ];
    return [
        {
            id: generateWaypointId(),
            x: anchor.x,
            y: stubCoord
        },
        {
            id: generateWaypointId(),
            x: movedCoordinate,
            y: stubCoord
        }
    ];
}
exports.generateWaypointId = __webpack_exports__.generateWaypointId;
exports.insertWaypoint = __webpack_exports__.insertWaypoint;
exports.materializePathWaypoints = __webpack_exports__.materializePathWaypoints;
exports.moveSegmentByDelta = __webpack_exports__.moveSegmentByDelta;
exports.moveWaypoint = __webpack_exports__.moveWaypoint;
exports.rebalanceWaypoints = __webpack_exports__.rebalanceWaypoints;
exports.removeWaypoint = __webpack_exports__.removeWaypoint;
exports.waypointsEqual = __webpack_exports__.waypointsEqual;
exports.waypointsPositionallyEqual = __webpack_exports__.waypointsPositionallyEqual;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "generateWaypointId",
    "insertWaypoint",
    "materializePathWaypoints",
    "moveSegmentByDelta",
    "moveWaypoint",
    "rebalanceWaypoints",
    "removeWaypoint",
    "waypointsEqual",
    "waypointsPositionallyEqual"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
