"use strict";
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module Curve
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConsolidateAdjacentCurvePrimitivesContext = void 0;
const Geometry_1 = require("../../Geometry");
const GeometryHandler_1 = require("../../geometry3d/GeometryHandler");
const PolylineCompressionByEdgeOffset_1 = require("../../geometry3d/PolylineCompressionByEdgeOffset");
const PolylineOps_1 = require("../../geometry3d/PolylineOps");
const Arc3d_1 = require("../Arc3d");
const CurveFactory_1 = require("../CurveFactory");
const LineSegment3d_1 = require("../LineSegment3d");
const LineString3d_1 = require("../LineString3d");
const RegionOps_1 = require("../RegionOps");
/**
 * * Implementation class for ConsolidateAdjacentCurvePrimitives.
 *
 * @internal
 */
class ConsolidateAdjacentCurvePrimitivesContext extends GeometryHandler_1.NullGeometryHandler {
    _options;
    constructor(options) {
        super();
        this._options = options ? options : new RegionOps_1.ConsolidateAdjacentCurvePrimitivesOptions();
    }
    /** look for adjacent compatible primitives in a path or loop. */
    handleCurveChain(g) {
        const children = g.children;
        const numOriginal = children.length;
        const points = [];
        let numAccept = 0;
        // i0 <= i < i1 is a range of child indices.
        // numAccept is the number of children accepted (contiguously at front of children)
        for (let i0 = 0; i0 < numOriginal;) {
            const basePrimitive = g.children[i0];
            if (this._options.consolidateLinearGeometry && (basePrimitive instanceof LineSegment3d_1.LineSegment3d || basePrimitive instanceof LineString3d_1.LineString3d)) {
                points.length = 0;
                let i1 = i0;
                // on exit, i1 is beyond the block of linear primitives  . ..
                for (; i1 < g.children.length; i1++) {
                    const nextPrimitive = g.children[i1];
                    if (nextPrimitive instanceof LineSegment3d_1.LineSegment3d) {
                        points.push(nextPrimitive.startPoint());
                        points.push(nextPrimitive.endPoint());
                    }
                    else if (nextPrimitive instanceof LineString3d_1.LineString3d) {
                        const source = nextPrimitive.packedPoints;
                        for (let k = 0; k < source.length; k++) {
                            points.push(source.getPoint3dAtUncheckedPointIndex(k));
                        }
                    }
                    else {
                        break;
                    }
                }
                if (points.length <= 1) {
                    g.children[numAccept++] = basePrimitive;
                }
                else if (this._options.disableLinearCompression) {
                    const pointsDeduped = PolylineOps_1.PolylineOps.compressShortEdges(points, Geometry_1.Geometry.smallFloatingPoint); // remove only exact duplicate interior points
                    g.children[numAccept++] = LineString3d_1.LineString3d.createPoints(pointsDeduped);
                }
                else { // compress points
                    const compressedPointsA = PolylineOps_1.PolylineOps.compressShortEdges(points, this._options.duplicatePointTolerance);
                    const compressedPointsB = PolylineOps_1.PolylineOps.compressByChordError(compressedPointsA, this._options.colinearPointTolerance, true);
                    if (i0 === 0 && i1 === numOriginal && this._options.consolidateLoopSeam) {
                        // points is the entire curve: if the curve is physically closed and end segments are colinear, re/move the seam
                        PolylineCompressionByEdgeOffset_1.PolylineCompressionContext.compressColinearWrapInPlace(compressedPointsB, this._options.duplicatePointTolerance, this._options.colinearPointTolerance);
                    }
                    if (compressedPointsB.length < 2) {
                        // Collapsed to a point?  Make a single point linestring
                        g.children[numAccept++] = LineString3d_1.LineString3d.create(compressedPointsB[0]);
                    }
                    else if (compressedPointsB.length === 2) {
                        g.children[numAccept++] = LineSegment3d_1.LineSegment3d.create(compressedPointsB[0], compressedPointsB[1]);
                    }
                    else {
                        g.children[numAccept++] = LineString3d_1.LineString3d.createPoints(compressedPointsB);
                    }
                }
                i0 = i1;
            }
            else if (this._options.consolidateCompatibleArcs && basePrimitive instanceof Arc3d_1.Arc3d) {
                // subsume subsequent arcs into basePrimitive.
                // always accept base primitive.
                for (; ++i0 < g.children.length;) {
                    const nextPrimitive = g.children[i0];
                    if (!(nextPrimitive instanceof Arc3d_1.Arc3d))
                        break;
                    if (!CurveFactory_1.CurveFactory.appendToArcInPlace(basePrimitive, nextPrimitive, false, this._options.duplicatePointTolerance))
                        break;
                }
                // i0 has already advanced
                g.children[numAccept++] = basePrimitive; // which has been extended 0 or more times.
            }
            else {
                g.children[numAccept++] = basePrimitive;
                i0++;
            }
        }
        g.children.length = numAccept;
    }
    handlePath(g) {
        return this.handleCurveChain(g);
    }
    handleLoop(g) {
        this.handleCurveChain(g);
        if (g.children.length > 1 && this._options.consolidateLoopSeam) {
            const lastChild = g.children[g.children.length - 1];
            const firstChild = g.children[0];
            if ((lastChild instanceof LineSegment3d_1.LineSegment3d || lastChild instanceof LineString3d_1.LineString3d) && (firstChild instanceof LineSegment3d_1.LineSegment3d || firstChild instanceof LineString3d_1.LineString3d)) {
                if (this._options.consolidateLinearGeometry && !this._options.disableLinearCompression) {
                    const lastPoints = lastChild.points;
                    lastPoints.pop(); // the original start point survives as an interior point in the new first primitive
                    g.children[0] = LineString3d_1.LineString3d.createPoints([...lastPoints, ...firstChild.points]);
                    g.children.pop();
                }
            }
            else if (lastChild instanceof Arc3d_1.Arc3d && firstChild instanceof Arc3d_1.Arc3d) {
                if (this._options.consolidateCompatibleArcs) {
                    if (CurveFactory_1.CurveFactory.appendToArcInPlace(lastChild, firstChild, false, this._options.duplicatePointTolerance)) {
                        g.children[0] = lastChild;
                        g.children.pop();
                    }
                }
            }
        }
    }
    handleParityRegion(g) {
        for (const child of g.children)
            child.dispatchToGeometryHandler(this);
    }
    handleUnionRegion(g) {
        for (const child of g.children)
            child.dispatchToGeometryHandler(this);
    }
}
exports.ConsolidateAdjacentCurvePrimitivesContext = ConsolidateAdjacentCurvePrimitivesContext;
//# sourceMappingURL=ConsolidateAdjacentPrimitivesContext.js.map