"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CreateCalloutUC = void 0;
exports.makeCreateCalloutUC = makeCreateCalloutUC;
const core_1 = require("@vived/core");
const LabelEntity_1 = require("../../Label/Entities/LabelEntity");
const CalloutGlobals_1 = require("../Entities/CalloutGlobals");
const CalloutRepo_1 = require("../Entities/CalloutRepo");
const CreateCalloutUndoRedo_1 = require("./CreateCalloutUndoRedo");
class CreateCalloutUC extends core_1.AppObjectUC {
    constructor() {
        super(...arguments);
        this.newLabelBumpAmount = 0.01;
    }
    static get(appObj) {
        return appObj.getComponent(this.type);
    }
    static getById(id, appObjects) {
        return appObjects.get(id)?.getComponent(this.type);
    }
}
exports.CreateCalloutUC = CreateCalloutUC;
CreateCalloutUC.type = "CreateCalloutUC";
function makeCreateCalloutUC(appObject) {
    return new CreateCalloutUCImp(appObject);
}
class CreateCalloutUCImp extends CreateCalloutUC {
    get repo() {
        return this.getCachedLocalComponent(CalloutRepo_1.CalloutRepo.type);
    }
    get calloutGlobals() {
        return this.getCachedSingleton(CalloutGlobals_1.CalloutGlobals.type);
    }
    get repoLabel() {
        return this.getCachedLocalComponent(LabelEntity_1.LabelEntity.type);
    }
    fromPick(pickHit, pickHitNormal, undoRedo = true) {
        if (!this.repo) {
            throw new Error("No Callout Repo");
        }
        const callout = this.repo.makeCallout();
        callout.rootTransform.worldPosition = pickHit;
        callout.rootTransform.worldRotation =
            core_1.Quaternion.FromDirectionVector(pickHitNormal);
        const labelBump = core_1.Vector3.NewVectorOfLength(pickHitNormal, this.newLabelBumpAmount);
        callout.labelTransform.worldPosition = core_1.Vector3.Add(labelBump, pickHit);
        callout.labelTransform.worldRotation = core_1.Quaternion.FromYawPitchRoll(core_1.Angle.FromDegrees(180), core_1.Angle.FromDegrees(0), core_1.Angle.FromDegrees(0));
        callout.label.default = this.getInitialLabel();
        if (this.calloutGlobals) {
            callout.size = this.calloutGlobals.lastSize;
            callout.overlayColor.overlayColor = this.calloutGlobals.lastColor;
        }
        if (undoRedo) {
            new CreateCalloutUndoRedo_1.CreateCalloutUndoRedo(callout.data, this.appObjects);
        }
        return callout;
    }
    getInitialLabel() {
        if (this.appObject.hasComponent(LabelEntity_1.LabelEntity.type)) {
            return this.repoLabel ? this.repoLabel.display : "Callout";
        }
        else {
            return "Callout";
        }
    }
    constructor(appObject) {
        super(appObject, CreateCalloutUC.type);
    }
}
//# sourceMappingURL=CreateCalloutUC.js.map