import { Fragment, jsx, jsxs } from "react/jsx-runtime";
import styled from "@emotion/styled";
import { EdgeLabelRenderer, getSmoothStepPath, useStore } from "../../xyflow/react.js";
import { memo, useMemo } from "react";
const StageEdgeLabel = styled.div`
  position: absolute;
  color: var(--canvas-foreground);
  background: var(--canvas-background);
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 500;
  border: 1px solid var(--canvas-border);
  box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1);
  pointer-events: all;

  &:hover {
    background: var(--canvas-background-hover);
    border-color: var(--canvas-border-hover);
  }
`;
function stageEdgeGeometryEquality(previous, next) {
    return previous.sourceX === next.sourceX && previous.sourceY === next.sourceY && previous.targetX === next.targetX && previous.targetY === next.targetY;
}
function getArrowFromBezier(path, arrowSize) {
    const pathEl = document.createElementNS('http://www.w3.org/2000/svg', 'path');
    pathEl.setAttribute('d', path);
    const totalLength = pathEl.getTotalLength();
    const endPoint = pathEl.getPointAtLength(totalLength);
    const prevPoint = pathEl.getPointAtLength(totalLength - arrowSize);
    const angle = Math.atan2(endPoint.y - prevPoint.y, endPoint.x - prevPoint.x);
    return {
        endX: endPoint.x,
        endY: endPoint.y,
        angle
    };
}
function StageEdgeInner({ geometry, sourcePosition, targetPosition, selected, style, stroke = 'var(--canvas-foreground-emp)', strokeWidth = 2, arrowSize = 8, ...rest }) {
    const [pathData, labelX, labelY, _ex, _ey] = getSmoothStepPath({
        sourceX: geometry.sourceX,
        sourceY: geometry.sourceY,
        sourcePosition,
        targetX: geometry.targetX - 1,
        targetY: geometry.targetY,
        targetPosition,
        borderRadius: 40
    });
    const { endX, endY, angle } = useMemo(()=>getArrowFromBezier(pathData, arrowSize), [
        pathData,
        arrowSize
    ]);
    const arrowLineLength = arrowSize;
    const strokeColor = selected ? 'var(--canvas-selection-indicator)' : stroke;
    const strokeWidthValue = selected ? strokeWidth + 1 : strokeWidth;
    return /*#__PURE__*/ jsxs(Fragment, {
        children: [
            /*#__PURE__*/ jsxs("g", {
                className: `react-flow__edge stage-edge ${selected ? 'selected' : ''}`,
                children: [
                    /*#__PURE__*/ jsx("path", {
                        d: pathData,
                        stroke: strokeColor,
                        strokeWidth: strokeWidthValue,
                        strokeLinecap: "round",
                        strokeLinejoin: "round",
                        fill: "none",
                        style: style
                    }),
                    /*#__PURE__*/ jsx("line", {
                        x1: endX,
                        y1: endY,
                        x2: endX - arrowLineLength * Math.cos(angle - Math.PI / 6),
                        y2: endY - arrowLineLength * Math.sin(angle - Math.PI / 6),
                        stroke: strokeColor,
                        strokeWidth: strokeWidthValue
                    }),
                    /*#__PURE__*/ jsx("line", {
                        x1: endX,
                        y1: endY,
                        x2: endX - arrowLineLength * Math.cos(angle + Math.PI / 6),
                        y2: endY - arrowLineLength * Math.sin(angle + Math.PI / 6),
                        stroke: strokeColor,
                        strokeWidth: strokeWidthValue
                    }),
                    /*#__PURE__*/ jsx("path", {
                        d: pathData,
                        stroke: "transparent",
                        strokeWidth: 20,
                        fill: "none",
                        pointerEvents: "stroke"
                    })
                ]
            }),
            rest.label && /*#__PURE__*/ jsx(EdgeLabelRenderer, {
                children: /*#__PURE__*/ jsx(StageEdgeLabel, {
                    style: {
                        transform: `translate(-50%, -50%) translate(${labelX}px,${labelY}px)`
                    },
                    className: "nodrag nopan",
                    children: rest.label
                })
            })
        ]
    });
}
const StageEdgeInnerMemo = /*#__PURE__*/ memo(StageEdgeInner);
function StageEdgeComponent({ sourceX, sourceY, targetX, targetY, sourcePosition, targetPosition, selected, style, stroke, strokeWidth, arrowSize, ...rest }) {
    const geometry = useStore((state)=>{
        const sourceNode = state.nodeLookup.get(rest.source);
        const targetNode = state.nodeLookup.get(rest.target);
        return {
            sourceX: sourceNode ? sourceNode.position.x + (sourceNode.measured?.width ?? 0) : sourceX,
            sourceY: sourceNode ? sourceNode.position.y + 32 : sourceY,
            targetX: targetNode?.position.x ?? targetX,
            targetY: targetNode ? targetNode.position.y + 32 : targetY
        };
    }, stageEdgeGeometryEquality);
    return /*#__PURE__*/ jsx(StageEdgeInnerMemo, {
        geometry: geometry,
        sourcePosition: sourcePosition,
        targetPosition: targetPosition,
        selected: selected,
        style: style,
        stroke: stroke,
        strokeWidth: strokeWidth,
        arrowSize: arrowSize,
        ...rest
    });
}
const StageEdge = /*#__PURE__*/ memo(StageEdgeComponent);
export { StageEdge, StageEdgeLabel };
