var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        if (typeof b !== "function" && b !== null)
            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
    return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (g && (g = 0, op[0] && (_ = 0)), _) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
import { AcDbPolyline, AcGePoint2d, AcGePoint3d } from '@mlightcad/data-model';
import { AcApDocManager } from '../app';
import { AcEdCommand, AcEdOpenMode, AcEdPreviewJig, AcEdPromptDoubleOptions, AcEdPromptPointOptions, AcEdPromptStateMachine, AcEdPromptStatus } from '../editor';
import { AcApI18n } from '../i18n';
/**
 * Preview jig that dynamically updates a polyline while the user is acquiring
 * the *next* point.
 *
 * Responsibilities:
 * - Keep the already-confirmed vertices visible.
 * - Add a transient "current" vertex that follows the cursor.
 * - Update the bulge of the last confirmed vertex to preview arc/line changes.
 *
 * Why this jig exists:
 * - Point acquisition needs *live* geometry updates as the cursor moves.
 * - We update the polyline incrementally (add/remove a single temp vertex)
 *   instead of `reset()` to avoid flicker and preserve transient state.
 */
var AcApPolylineJig = /** @class */ (function (_super) {
    __extends(AcApPolylineJig, _super);
    /**
     * Creates a polyline jig.
     *
     * @param view - The associated view
     */
    /**
     * Create a polyline preview jig for point acquisition.
     *
     * @param view - The associated view for transient rendering.
     * @param points - Confirmed polyline vertices.
     * @param bulges - Bulge values aligned with `points`.
     * @param currentBulge - Optional fixed bulge used for the preview segment.
     * @param bulgeProvider - Optional function to compute bulge based on the
     *                        current cursor point.
     */
    function AcApPolylineJig(view, points, bulges, currentBulge, bulgeProvider) {
        var _this = _super.call(this, view) || this;
        _this._hasTemp = false;
        _this._polyline = new AcDbPolyline();
        _this._points = points;
        _this._bulges = bulges;
        _this._currentBulge = currentBulge;
        _this._bulgeProvider = bulgeProvider;
        _this._baseCount = points.length;
        _this._appliedBulge = _this._bulges[_this._baseCount - 1];
        _this.addBaseVertices();
        return _this;
    }
    Object.defineProperty(AcApPolylineJig.prototype, "entity", {
        /**
         * The transient polyline entity being previewed.
         */
        get: function () {
            return this._polyline;
        },
        enumerable: false,
        configurable: true
    });
    /**
     * Update the preview polyline as the cursor moves.
     *
     * This updates:
     * - The bulge of the last confirmed vertex (if needed).
     * - A single temporary vertex representing the current cursor position.
     */
    AcApPolylineJig.prototype.update = function (point) {
        var _a, _b;
        if (this._baseCount === 0)
            return;
        if (this._hasTemp) {
            this._polyline.removeVertexAt(this._baseCount);
            this._hasTemp = false;
        }
        var lastIndex = this._baseCount - 1;
        var dynamicBulge = (_a = this._bulgeProvider) === null || _a === void 0 ? void 0 : _a.call(this, point);
        var desiredBulge = (_b = dynamicBulge !== null && dynamicBulge !== void 0 ? dynamicBulge : this._currentBulge) !== null && _b !== void 0 ? _b : this._bulges[lastIndex];
        if (desiredBulge !== this._appliedBulge) {
            var lastPoint = this._points[lastIndex];
            this._polyline.removeVertexAt(lastIndex);
            this._polyline.addVertexAt(lastIndex, lastPoint, desiredBulge);
            this._appliedBulge = desiredBulge;
        }
        this._polyline.addVertexAt(this._baseCount, new AcGePoint2d(point), undefined);
        this._hasTemp = true;
    };
    /**
     * Seed the preview entity with the confirmed vertices.
     */
    AcApPolylineJig.prototype.addBaseVertices = function () {
        var _this = this;
        this._points.forEach(function (p, index) {
            return _this._polyline.addVertexAt(index, p, _this._bulges[index]);
        });
    };
    return AcApPolylineJig;
}(AcEdPreviewJig));
export { AcApPolylineJig };
/**
 * Static preview jig used to keep the already-built polyline visible during
 * *non-point* prompts (angle, center, radius, etc.).
 *
 * Why this jig exists:
 * - Some prompts (like `getDouble`) don't update geometry, but the existing
 *   polyline should remain visible so the user doesn't lose context.
 * - Using a separate jig avoids coupling static rendering with the dynamic
 *   point-following behavior of {@link AcApPolylineJig}.
 *
 * In short:
 * - `AcApPolylineJig` is for live point tracking.
 * - `AcApPolylineStaticJig` is for "hold the drawing while asking other inputs".
 */
var AcApPolylineStaticJig = /** @class */ (function (_super) {
    __extends(AcApPolylineStaticJig, _super);
    /**
     * Create a static polyline preview jig.
     *
     * @param view - The associated view for transient rendering.
     * @param points - Confirmed polyline vertices.
     * @param bulges - Bulge values aligned with `points`.
     */
    function AcApPolylineStaticJig(view, points, bulges) {
        var _this = _super.call(this, view) || this;
        _this._polyline = new AcDbPolyline();
        points.forEach(function (p, index) {
            return _this._polyline.addVertexAt(index, p, bulges[index]);
        });
        return _this;
    }
    Object.defineProperty(AcApPolylineStaticJig.prototype, "entity", {
        /**
         * The transient polyline entity being previewed.
         */
        get: function () {
            return this._polyline;
        },
        enumerable: false,
        configurable: true
    });
    /**
     * No-op update. This jig keeps a static preview visible.
     */
    AcApPolylineStaticJig.prototype.update = function (_value) {
        // Static preview: keep current polyline visible during prompts.
    };
    return AcApPolylineStaticJig;
}(AcEdPreviewJig));
/**
 * Command to create one polyline.
 */
var AcApPolylineCmd = /** @class */ (function (_super) {
    __extends(AcApPolylineCmd, _super);
    function AcApPolylineCmd() {
        var _this = _super.call(this) || this;
        _this.mode = AcEdOpenMode.Write;
        return _this;
    }
    AcApPolylineCmd.prototype.computeBulgeFromCenter = function (start, end, center) {
        var v1x = start.x - center.x;
        var v1y = start.y - center.y;
        var v2x = end.x - center.x;
        var v2y = end.y - center.y;
        var cross = v1x * v2y - v1y * v2x;
        var dot = v1x * v2x + v1y * v2y;
        var angle = Math.atan2(cross, dot);
        if (!Number.isFinite(angle))
            return undefined;
        return Math.tan(angle / 4);
    };
    AcApPolylineCmd.prototype.computeCircleCenter = function (p1, p2, p3) {
        var x1 = p1.x;
        var y1 = p1.y;
        var x2 = p2.x;
        var y2 = p2.y;
        var x3 = p3.x;
        var y3 = p3.y;
        var a = x1 - x2;
        var b = y1 - y2;
        var c = x1 - x3;
        var d = y1 - y3;
        var e = (x1 * x1 - x2 * x2 + y1 * y1 - y2 * y2) / 2;
        var f = (x1 * x1 - x3 * x3 + y1 * y1 - y3 * y3) / 2;
        var det = a * d - b * c;
        if (Math.abs(det) < 1e-9)
            return undefined;
        var cx = (d * e - b * f) / det;
        var cy = (-c * e + a * f) / det;
        return new AcGePoint2d(cx, cy);
    };
    AcApPolylineCmd.prototype.computeBulgeFromThreePoints = function (start, mid, end) {
        var center = this.computeCircleCenter(start, mid, end);
        if (!center)
            return undefined;
        return this.computeBulgeFromCenter(start, end, center);
    };
    AcApPolylineCmd.prototype.computeBulgeFromRadius = function (start, end, radius) {
        var r = Math.abs(radius);
        var dx = end.x - start.x;
        var dy = end.y - start.y;
        var chord = Math.hypot(dx, dy);
        if (chord === 0 || chord > 2 * r)
            return undefined;
        var midx = (start.x + end.x) / 2;
        var midy = (start.y + end.y) / 2;
        var h = Math.sqrt(Math.max(0, r * r - (chord * chord) / 4));
        var ux = -dy / chord;
        var uy = dx / chord;
        var sign = radius >= 0 ? 1 : -1;
        var cx = midx + ux * h * sign;
        var cy = midy + uy * h * sign;
        var center = new AcGePoint2d(cx, cy);
        return this.computeBulgeFromCenter(start, end, center);
    };
    AcApPolylineCmd.prototype.execute = function (context) {
        return __awaiter(this, void 0, void 0, function () {
            var points, bulges, closed, ARC_BULGE, getArcDirectionFactor, applyArcDirection, computeBulgeFromCenter, computeBulgeFromThreePoints, computeBulgeFromRadius, createStaticJig, createPreviewJig, getCurrentPoint, setSegmentBulge, appendPoint, undoLast, shouldFinish, LineState, ArcState, firstPointPrompt, firstPointResult, machine, db, polyline_1;
            var _this = this;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        AcApDocManager.instance.editor.resetInputToggles();
                        points = [];
                        bulges = [];
                        closed = false;
                        ARC_BULGE = 0.5;
                        getArcDirectionFactor = function () {
                            var toggles = AcApDocManager.instance.editor.getInputToggles();
                            return toggles.ctrlArcFlip ? -1 : 1;
                        };
                        applyArcDirection = function (bulge) {
                            if (bulge == null)
                                return bulge;
                            return bulge * getArcDirectionFactor();
                        };
                        computeBulgeFromCenter = function (start, end, center) { return _this.computeBulgeFromCenter(start, end, center); };
                        computeBulgeFromThreePoints = function (start, mid, end) { return _this.computeBulgeFromThreePoints(start, mid, end); };
                        computeBulgeFromRadius = function (start, end, radius) { return _this.computeBulgeFromRadius(start, end, radius); };
                        createStaticJig = function () {
                            return new AcApPolylineStaticJig(context.view, points, bulges);
                        };
                        createPreviewJig = function (currentBulge, bulgeProvider) {
                            return new AcApPolylineJig(context.view, points, bulges, currentBulge, bulgeProvider);
                        };
                        getCurrentPoint = function () { return points[points.length - 1]; };
                        setSegmentBulge = function (bulge) {
                            if (points.length > 0) {
                                bulges[points.length - 1] = bulge;
                            }
                        };
                        appendPoint = function (point) {
                            points.push(new AcGePoint2d(point));
                            bulges.push(undefined);
                        };
                        undoLast = function () {
                            if (points.length <= 1)
                                return;
                            points.pop();
                            bulges.pop();
                            bulges[bulges.length - 1] = undefined;
                        };
                        shouldFinish = function (status) {
                            return status !== AcEdPromptStatus.OK && status !== AcEdPromptStatus.Keyword;
                        };
                        LineState = /** @class */ (function () {
                            function LineState(machine) {
                                this.machine = machine;
                            }
                            LineState.prototype.buildPrompt = function () {
                                var prompt = new AcEdPromptPointOptions(AcApI18n.t('jig.polyline.nextPointWithOptions'));
                                prompt.keywords.add(AcApI18n.t('jig.polyline.keywords.arc.display'), AcApI18n.t('jig.polyline.keywords.arc.global'), AcApI18n.t('jig.polyline.keywords.arc.local'));
                                prompt.keywords.add(AcApI18n.t('jig.polyline.keywords.undo.display'), AcApI18n.t('jig.polyline.keywords.undo.global'), AcApI18n.t('jig.polyline.keywords.undo.local'));
                                prompt.keywords.add(AcApI18n.t('jig.polyline.keywords.close.display'), AcApI18n.t('jig.polyline.keywords.close.global'), AcApI18n.t('jig.polyline.keywords.close.local'));
                                prompt.useDashedLine = true;
                                prompt.useBasePoint = true;
                                var basePoint = getCurrentPoint();
                                if (basePoint) {
                                    prompt.basePoint = new AcGePoint3d(basePoint);
                                }
                                prompt.jig = createPreviewJig(undefined);
                                return prompt;
                            };
                            LineState.prototype.handleResult = function (result) {
                                return __awaiter(this, void 0, void 0, function () {
                                    var keyword;
                                    var _a;
                                    return __generator(this, function (_b) {
                                        if (result.status === AcEdPromptStatus.Keyword) {
                                            keyword = (_a = result.stringResult) !== null && _a !== void 0 ? _a : '';
                                            if (keyword === 'Arc') {
                                                this.machine.setState(new ArcState(this.machine));
                                            }
                                            else if (keyword === 'Undo') {
                                                undoLast();
                                            }
                                            else if (keyword === 'Close' && points.length > 1) {
                                                closed = true;
                                                setSegmentBulge(undefined);
                                                return [2 /*return*/, 'finish'];
                                            }
                                            return [2 /*return*/, 'continue'];
                                        }
                                        if (result.status === AcEdPromptStatus.OK) {
                                            setSegmentBulge(undefined);
                                            appendPoint(result.value);
                                            return [2 /*return*/, 'continue'];
                                        }
                                        return [2 /*return*/, 'finish'];
                                    });
                                });
                            };
                            return LineState;
                        }());
                        ArcState = /** @class */ (function () {
                            function ArcState(machine) {
                                this.machine = machine;
                            }
                            ArcState.prototype.buildPrompt = function () {
                                var prompt = new AcEdPromptPointOptions(AcApI18n.t('jig.polyline.nextPointWithArcOptions'));
                                prompt.keywords.add(AcApI18n.t('jig.polyline.keywords.angle.display'), AcApI18n.t('jig.polyline.keywords.angle.global'), AcApI18n.t('jig.polyline.keywords.angle.local'));
                                prompt.keywords.add(AcApI18n.t('jig.polyline.keywords.center.display'), AcApI18n.t('jig.polyline.keywords.center.global'), AcApI18n.t('jig.polyline.keywords.center.local'));
                                prompt.keywords.add(AcApI18n.t('jig.polyline.keywords.secondPoint.display'), AcApI18n.t('jig.polyline.keywords.secondPoint.global'), AcApI18n.t('jig.polyline.keywords.secondPoint.local'));
                                prompt.keywords.add(AcApI18n.t('jig.polyline.keywords.radius.display'), AcApI18n.t('jig.polyline.keywords.radius.global'), AcApI18n.t('jig.polyline.keywords.radius.local'));
                                prompt.keywords.add(AcApI18n.t('jig.polyline.keywords.line.display'), AcApI18n.t('jig.polyline.keywords.line.global'), AcApI18n.t('jig.polyline.keywords.line.local'));
                                prompt.keywords.add(AcApI18n.t('jig.polyline.keywords.undo.display'), AcApI18n.t('jig.polyline.keywords.undo.global'), AcApI18n.t('jig.polyline.keywords.undo.local'));
                                prompt.keywords.add(AcApI18n.t('jig.polyline.keywords.close.display'), AcApI18n.t('jig.polyline.keywords.close.global'), AcApI18n.t('jig.polyline.keywords.close.local'));
                                prompt.useDashedLine = true;
                                prompt.useBasePoint = true;
                                var basePoint = getCurrentPoint();
                                if (basePoint) {
                                    prompt.basePoint = new AcGePoint3d(basePoint);
                                }
                                prompt.jig = createPreviewJig(undefined, function () {
                                    return applyArcDirection(ARC_BULGE);
                                });
                                return prompt;
                            };
                            ArcState.prototype.handleResult = function (result) {
                                return __awaiter(this, void 0, void 0, function () {
                                    var keyword, startPoint, anglePrompt, angleResult, endPrompt, angleRad, angleBulge_1, endResult, centerPrompt, centerResult_1, endPrompt, endResult, bulge, secondPrompt, secondResult_1, endPrompt, endResult, bulge, radiusPrompt, radiusResult_1, endPrompt, endResult, bulge;
                                    var _a, _b, _c;
                                    return __generator(this, function (_d) {
                                        switch (_d.label) {
                                            case 0:
                                                if (result.status === AcEdPromptStatus.OK) {
                                                    setSegmentBulge(applyArcDirection(ARC_BULGE));
                                                    appendPoint(result.value);
                                                    return [2 /*return*/, 'continue'];
                                                }
                                                if (result.status !== AcEdPromptStatus.Keyword)
                                                    return [2 /*return*/, 'finish'];
                                                keyword = (_a = result.stringResult) !== null && _a !== void 0 ? _a : '';
                                                if (keyword === 'Line') {
                                                    this.machine.setState(new LineState(this.machine));
                                                    return [2 /*return*/, 'continue'];
                                                }
                                                if (keyword === 'Undo') {
                                                    undoLast();
                                                    return [2 /*return*/, 'continue'];
                                                }
                                                if (keyword === 'Close' && points.length > 1) {
                                                    closed = true;
                                                    setSegmentBulge(undefined);
                                                    return [2 /*return*/, 'finish'];
                                                }
                                                startPoint = getCurrentPoint();
                                                if (!startPoint)
                                                    return [2 /*return*/, 'finish'];
                                                if (!(keyword === 'Angle')) return [3 /*break*/, 3];
                                                anglePrompt = new AcEdPromptDoubleOptions(AcApI18n.t('jig.polyline.arcAngle'));
                                                anglePrompt.jig = createStaticJig();
                                                return [4 /*yield*/, AcApDocManager.instance.editor.getDouble(anglePrompt)];
                                            case 1:
                                                angleResult = _d.sent();
                                                if (shouldFinish(angleResult.status))
                                                    return [2 /*return*/, 'finish'];
                                                endPrompt = new AcEdPromptPointOptions(AcApI18n.t('jig.polyline.arcEndPoint'));
                                                endPrompt.useDashedLine = true;
                                                endPrompt.useBasePoint = true;
                                                endPrompt.basePoint = new AcGePoint3d(startPoint);
                                                angleRad = ((_b = angleResult.value) !== null && _b !== void 0 ? _b : 0) * (Math.PI / 180);
                                                angleBulge_1 = Math.tan(angleRad / 4);
                                                endPrompt.jig = createPreviewJig(undefined, function () {
                                                    return applyArcDirection(angleBulge_1);
                                                });
                                                return [4 /*yield*/, AcApDocManager.instance.editor.getPoint(endPrompt)];
                                            case 2:
                                                endResult = _d.sent();
                                                if (shouldFinish(endResult.status))
                                                    return [2 /*return*/, 'finish'];
                                                setSegmentBulge(applyArcDirection(angleBulge_1));
                                                appendPoint(endResult.value);
                                                return [2 /*return*/, 'continue'];
                                            case 3:
                                                if (!(keyword === 'Center')) return [3 /*break*/, 6];
                                                centerPrompt = new AcEdPromptPointOptions(AcApI18n.t('jig.polyline.arcCenter'));
                                                centerPrompt.jig = createStaticJig();
                                                return [4 /*yield*/, AcApDocManager.instance.editor.getPoint(centerPrompt)];
                                            case 4:
                                                centerResult_1 = _d.sent();
                                                if (shouldFinish(centerResult_1.status))
                                                    return [2 /*return*/, 'finish'];
                                                endPrompt = new AcEdPromptPointOptions(AcApI18n.t('jig.polyline.arcEndPoint'));
                                                endPrompt.useDashedLine = true;
                                                endPrompt.useBasePoint = true;
                                                endPrompt.basePoint = new AcGePoint3d(startPoint);
                                                endPrompt.jig = createPreviewJig(undefined, function (end) {
                                                    return applyArcDirection(computeBulgeFromCenter(startPoint, end, centerResult_1.value));
                                                });
                                                return [4 /*yield*/, AcApDocManager.instance.editor.getPoint(endPrompt)];
                                            case 5:
                                                endResult = _d.sent();
                                                if (shouldFinish(endResult.status))
                                                    return [2 /*return*/, 'finish'];
                                                bulge = computeBulgeFromCenter(startPoint, endResult.value, centerResult_1.value);
                                                if (bulge === undefined)
                                                    return [2 /*return*/, 'continue'];
                                                setSegmentBulge(applyArcDirection(bulge));
                                                appendPoint(endResult.value);
                                                return [2 /*return*/, 'continue'];
                                            case 6:
                                                if (!(keyword === 'SecondPoint')) return [3 /*break*/, 9];
                                                secondPrompt = new AcEdPromptPointOptions(AcApI18n.t('jig.polyline.arcSecondPoint'));
                                                secondPrompt.jig = createStaticJig();
                                                secondPrompt.useDashedLine = true;
                                                secondPrompt.useBasePoint = true;
                                                secondPrompt.basePoint = new AcGePoint3d(startPoint);
                                                return [4 /*yield*/, AcApDocManager.instance.editor.getPoint(secondPrompt)];
                                            case 7:
                                                secondResult_1 = _d.sent();
                                                if (shouldFinish(secondResult_1.status))
                                                    return [2 /*return*/, 'finish'];
                                                endPrompt = new AcEdPromptPointOptions(AcApI18n.t('jig.polyline.arcEndPoint'));
                                                endPrompt.useDashedLine = true;
                                                endPrompt.useBasePoint = true;
                                                endPrompt.basePoint = new AcGePoint3d(startPoint);
                                                endPrompt.jig = createPreviewJig(undefined, function (end) {
                                                    return applyArcDirection(computeBulgeFromThreePoints(startPoint, secondResult_1.value, end));
                                                });
                                                return [4 /*yield*/, AcApDocManager.instance.editor.getPoint(endPrompt)];
                                            case 8:
                                                endResult = _d.sent();
                                                if (shouldFinish(endResult.status))
                                                    return [2 /*return*/, 'finish'];
                                                bulge = computeBulgeFromThreePoints(startPoint, secondResult_1.value, endResult.value);
                                                if (bulge === undefined)
                                                    return [2 /*return*/, 'continue'];
                                                setSegmentBulge(applyArcDirection(bulge));
                                                appendPoint(endResult.value);
                                                return [2 /*return*/, 'continue'];
                                            case 9:
                                                if (!(keyword === 'Radius')) return [3 /*break*/, 12];
                                                radiusPrompt = new AcEdPromptDoubleOptions(AcApI18n.t('jig.polyline.arcRadius'));
                                                radiusPrompt.jig = createStaticJig();
                                                return [4 /*yield*/, AcApDocManager.instance.editor.getDouble(radiusPrompt)];
                                            case 10:
                                                radiusResult_1 = _d.sent();
                                                if (shouldFinish(radiusResult_1.status))
                                                    return [2 /*return*/, 'finish'];
                                                endPrompt = new AcEdPromptPointOptions(AcApI18n.t('jig.polyline.arcEndPoint'));
                                                endPrompt.useDashedLine = true;
                                                endPrompt.useBasePoint = true;
                                                endPrompt.basePoint = new AcGePoint3d(startPoint);
                                                endPrompt.jig = createPreviewJig(undefined, function (end) {
                                                    var _a;
                                                    return applyArcDirection(computeBulgeFromRadius(startPoint, end, (_a = radiusResult_1.value) !== null && _a !== void 0 ? _a : 0));
                                                });
                                                return [4 /*yield*/, AcApDocManager.instance.editor.getPoint(endPrompt)];
                                            case 11:
                                                endResult = _d.sent();
                                                if (shouldFinish(endResult.status))
                                                    return [2 /*return*/, 'finish'];
                                                bulge = computeBulgeFromRadius(startPoint, endResult.value, (_c = radiusResult_1.value) !== null && _c !== void 0 ? _c : 0);
                                                if (bulge === undefined)
                                                    return [2 /*return*/, 'continue'];
                                                setSegmentBulge(applyArcDirection(bulge));
                                                appendPoint(endResult.value);
                                                return [2 /*return*/, 'continue'];
                                            case 12: return [2 /*return*/, 'continue'];
                                        }
                                    });
                                });
                            };
                            return ArcState;
                        }());
                        firstPointPrompt = new AcEdPromptPointOptions(AcApI18n.t('jig.polyline.firstPoint'));
                        return [4 /*yield*/, AcApDocManager.instance.editor.getPoint(firstPointPrompt)];
                    case 1:
                        firstPointResult = _a.sent();
                        if (firstPointResult.status !== AcEdPromptStatus.OK)
                            return [2 /*return*/];
                        appendPoint(firstPointResult.value);
                        machine = new AcEdPromptStateMachine();
                        machine.setState(new LineState(machine));
                        return [4 /*yield*/, machine.run(function (prompt) { return AcApDocManager.instance.editor.getPoint(prompt); })
                            // Create polyline if we have at least two points
                        ];
                    case 2:
                        _a.sent();
                        // Create polyline if we have at least two points
                        if (points.length >= 2) {
                            db = context.doc.database;
                            polyline_1 = new AcDbPolyline();
                            points.forEach(function (p, index) {
                                return polyline_1.addVertexAt(index, p, bulges[index]);
                            });
                            if (closed) {
                                polyline_1.closed = true;
                            }
                            db.tables.blockTable.modelSpace.appendEntity(polyline_1);
                        }
                        return [2 /*return*/];
                }
            });
        });
    };
    return AcApPolylineCmd;
}(AcEdCommand));
export { AcApPolylineCmd };
//# sourceMappingURL=AcApPolylineCmd.js.map