import { utilities as csUtils } from '@cornerstonejs/core';
import LivewireContourTool from './LivewireContourTool';
import { LivewirePath } from '../../utilities/livewire/LiveWirePath';
import { triggerAnnotationModified } from '../../stateManagement/annotation/helpers/state';
import { ChangeTypes } from '../../enums';
import { drawPolyline as drawPolylineSvg } from '../../drawingSvg';
class LivewireContourSegmentationTool extends LivewireContourTool {
    static { this.toolName = 'LivewireContourSegmentationTool'; }
    updateInterpolatedAnnotation(annotation, enabledElement) {
        if (this.editData ||
            !annotation.invalidated ||
            !annotation.data.handles.interpolationSources) {
            return;
        }
        annotation.data.contour.originalPolyline = annotation.data.contour.polyline;
        queueMicrotask(() => {
            if (!annotation.data.handles.interpolationSources) {
                return;
            }
            const { points } = annotation.data.handles;
            const { element } = enabledElement.viewport;
            this.setupBaseEditData(points[0], element, annotation);
            const { length: count } = points;
            const { scissors } = this;
            const { nearestEdge, repeatInterpolation } = this.configuration.interpolation;
            annotation.data.handles.originalPoints = points;
            const { worldToSlice, sliceToWorld } = this.editData;
            const handleSmoothing = [];
            if (nearestEdge) {
                let lastPoint = worldToSlice(points[points.length - 1]);
                points.forEach((point, hIndex) => {
                    const testPoint = worldToSlice(point);
                    lastPoint = testPoint;
                    handleSmoothing.push(testPoint);
                    scissors.startSearch(lastPoint);
                    scissors.findPathToPoint(testPoint);
                    scissors.findPathToPoint(worldToSlice(points[(hIndex + 3) % points.length]));
                    const minPoint = scissors.findMinNearby(testPoint, nearestEdge);
                    if (!csUtils.isEqual(testPoint, minPoint)) {
                        handleSmoothing[hIndex] = minPoint;
                        lastPoint = minPoint;
                        points[hIndex] = sliceToWorld(minPoint);
                    }
                });
            }
            const acceptedPath = new LivewirePath();
            for (let i = 0; i < count; i++) {
                scissors.startSearch(worldToSlice(points[i]));
                const path = scissors.findPathToPoint(worldToSlice(points[(i + 1) % count]));
                acceptedPath.addPoints(path);
            }
            this.updateAnnotation(acceptedPath);
            this.scissors = null;
            this.scissorsNext = null;
            this.editData = null;
            annotation.data.handles.interpolationSources = null;
            if (repeatInterpolation) {
                triggerAnnotationModified(annotation, enabledElement.viewport.element, ChangeTypes.InterpolationUpdated);
            }
        });
    }
    renderAnnotationInstance(renderContext) {
        const { enabledElement, svgDrawingHelper } = renderContext;
        const annotation = renderContext.annotation;
        const { annotationUID } = annotation;
        const { viewport } = enabledElement;
        const { worldToCanvas } = viewport;
        const { showInterpolationPolyline } = this.configuration.interpolation || {};
        this.updateInterpolatedAnnotation?.(annotation, enabledElement);
        const { originalPolyline } = annotation.data.contour;
        const rendered = super.renderAnnotationInstance(renderContext);
        if (showInterpolationPolyline &&
            originalPolyline &&
            annotation.autoGenerated) {
            const polylineCanvasPoints = originalPolyline.map(worldToCanvas);
            polylineCanvasPoints.push(polylineCanvasPoints[0]);
            drawPolylineSvg(svgDrawingHelper, annotationUID, 'interpolationContour-0', polylineCanvasPoints, {
                color: '#70ffff',
                lineWidth: 1,
                fillOpacity: 0,
            });
        }
        return rendered;
    }
    isContourSegmentationTool() {
        return true;
    }
}
export default LivewireContourSegmentationTool;
