"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.WorkPlaneEntity = exports.WorkPlaneShape = exports.WorkPlaneUnits = void 0;
exports.makeWorkPlaneEntity = makeWorkPlaneEntity;
const core_1 = require("@vived/core");
const Transform3D_1 = require("../../Transform3D");
var WorkPlaneUnits;
(function (WorkPlaneUnits) {
    WorkPlaneUnits[WorkPlaneUnits["INCHES"] = 0] = "INCHES";
    WorkPlaneUnits[WorkPlaneUnits["METERS"] = 1] = "METERS";
    WorkPlaneUnits[WorkPlaneUnits["FEET"] = 2] = "FEET";
})(WorkPlaneUnits || (exports.WorkPlaneUnits = WorkPlaneUnits = {}));
var WorkPlaneShape;
(function (WorkPlaneShape) {
    WorkPlaneShape[WorkPlaneShape["RECTANGLE"] = 0] = "RECTANGLE";
    WorkPlaneShape[WorkPlaneShape["CIRCLE"] = 1] = "CIRCLE";
    WorkPlaneShape[WorkPlaneShape["UNKNOWN"] = 2] = "UNKNOWN";
})(WorkPlaneShape || (exports.WorkPlaneShape = WorkPlaneShape = {}));
class WorkPlaneEntity extends core_1.AppObjectEntity {
}
exports.WorkPlaneEntity = WorkPlaneEntity;
WorkPlaneEntity.type = "WorkPlaneEntity";
WorkPlaneEntity.get = (appObjects) => (0, core_1.getSingletonComponent)(WorkPlaneEntity.type, appObjects);
function makeWorkPlaneEntity(appObj) {
    return new WorkPlaneImp(appObj);
}
class WorkPlaneImp extends WorkPlaneEntity {
    get workPlaneRadius() {
        return this._workPlaneRadius;
    }
    get workPlaneSize() {
        return this._workPlaneSize;
    }
    get units() {
        return this._units;
    }
    set units(val) {
        if (val === this._units)
            return;
        this._units = val;
        const scale = 1 / this.conversionFactor;
        this.transform.localScale = new core_1.Vector3(scale, scale, scale);
    }
    get workPlaneShape() {
        return this._workPlaneShape;
    }
    get conversionFactor() {
        switch (this.units) {
            case WorkPlaneUnits.FEET:
                return 3.28084;
            case WorkPlaneUnits.INCHES:
                return 39.3701;
            case WorkPlaneUnits.METERS:
                return 1;
            default:
                console.warn("[WorkPlane] Unknown units " + this.units);
                return 1;
        }
    }
    constrainToRadius(workPlanePick) {
        if (!this.workPlaneRadius)
            return workPlanePick;
        const pickRadius = workPlanePick.magnitued;
        if (pickRadius <= this.workPlaneRadius) {
            return workPlanePick;
        }
        else {
            return undefined;
        }
    }
    constrainToSize(workPlanePick) {
        if (!this._workPlaneHalfSize)
            return workPlanePick;
        const x = workPlanePick.x;
        const maxX = this._workPlaneHalfSize.x;
        const minX = -this._workPlaneHalfSize.x;
        const y = workPlanePick.y;
        const maxY = this._workPlaneHalfSize.y;
        const minY = -this._workPlaneHalfSize.y;
        if (x >= minX && x <= maxX && y >= minY && y <= maxY) {
            return workPlanePick;
        }
        else {
            return undefined;
        }
    }
    constructor(appObject) {
        super(appObject, WorkPlaneEntity.type);
        this._workPlaneHalfSize = core_1.Vector2.One();
        this.showWorkPlane = true;
        this._units = WorkPlaneUnits.METERS;
        this._workPlaneShape = WorkPlaneShape.UNKNOWN;
        this.setWorkPlaneSize = (size) => {
            this._workPlaneSize = size;
            this._workPlaneHalfSize = new core_1.Vector2(0.5 * size.x, 0.5 * size.y);
            this._workPlaneShape = WorkPlaneShape.RECTANGLE;
        };
        this.setWorkPlaneRadius = (radius) => {
            this._workPlaneRadius = radius;
            this._workPlaneShape = WorkPlaneShape.CIRCLE;
        };
        this.pickOnWorkPlane = (ray, unconstrained = false) => {
            const worldPos = this.transform.worldPosition;
            const worldUp = this.transform.localDirectionToWorld(core_1.Vector3.Up());
            const pickPlane = core_1.ParametricPlane.FromPointNormal(worldPos, worldUp);
            const worldPick = pickPlane.intersectLine(ray);
            if (worldPick === undefined) {
                return undefined;
            }
            const localPick = this.transform.worldPointToLocal(worldPick);
            const workPlanePick = new core_1.Vector2(localPick.x, localPick.z);
            if (unconstrained) {
                return workPlanePick;
            }
            if (this._workPlaneShape === WorkPlaneShape.CIRCLE) {
                return this.constrainToRadius(workPlanePick);
            }
            else if (this._workPlaneShape === WorkPlaneShape.RECTANGLE) {
                return this.constrainToSize(workPlanePick);
            }
            else {
                return workPlanePick;
            }
        };
        this.transform = Transform3D_1.Transform3D.addIfMissing(appObject);
        this.transform.addChangeObserver(this.notifyOnChange);
    }
}
//# sourceMappingURL=WorkPlaneEntity.js.map