"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@vived/core");
const core_2 = require("@vived/core");
const Transform3D_1 = require("../../Transform3D");
const Stickable_1 = require("./Stickable");
function makeTestRig() {
    const appObjects = (0, core_1.makeAppObjectRepo)();
    const ao = appObjects.getOrCreate("AO");
    const transform = (0, Transform3D_1.makeTransform3D)(ao);
    const stickEntity = (0, Stickable_1.makeStickableEntity)(ao);
    const observer = jest.fn();
    stickEntity.addChangeObserver(observer);
    return { stickEntity, observer, appObjects, transform };
}
describe("Stickable Entity", () => {
    it("Gets the entity", () => {
        const { appObjects, stickEntity } = makeTestRig();
        expect(Stickable_1.StickableEntity.get("AO", appObjects)).toEqual(stickEntity);
    });
    it("Notifies when the stick position changes", () => {
        const { observer, stickEntity } = makeTestRig();
        stickEntity.stickPosition = new core_2.Vector3(1, 2, 3);
        stickEntity.stickPosition = new core_2.Vector3(1, 2, 3);
        stickEntity.stickPosition = new core_2.Vector3(1, 2, 3);
        stickEntity.stickPosition = new core_2.Vector3(1, 2, 3);
        expect(observer).toHaveBeenCalledTimes(2); // 2 because the is stickable flag also changed
        expect(stickEntity.stickPosition).toEqual(new core_2.Vector3(1, 2, 3));
    });
    it("Notifies when the stick rotation changes", () => {
        const { observer, stickEntity } = makeTestRig();
        const quat = core_2.Quaternion.FromAngleAxis(new core_2.Vector3(1, 2, 3), core_2.Angle.FromDegrees(45));
        stickEntity.stickRotation = quat;
        stickEntity.stickRotation = quat;
        stickEntity.stickRotation = quat;
        stickEntity.stickRotation = quat;
        expect(observer).toHaveBeenCalledTimes(2); // 2 because the is stickable flag also changed
        expect(stickEntity.stickRotation).toEqual(quat);
    });
    it("Notifies when the stick scale changes", () => {
        const { observer, stickEntity } = makeTestRig();
        stickEntity.stickScale = 5;
        stickEntity.stickScale = 5;
        stickEntity.stickScale = 5;
        stickEntity.stickScale = 5;
        expect(observer).toHaveBeenCalledTimes(2); // 2 because the is stickable flag also changed
        expect(stickEntity.stickScale).toEqual(5);
    });
    it("Notifies when the show stick targets changes", () => {
        const { observer, stickEntity } = makeTestRig();
        stickEntity.showStickTarget = true;
        observer.mockClear();
        stickEntity.showStickTarget = false;
        stickEntity.showStickTarget = false;
        stickEntity.showStickTarget = false;
        stickEntity.showStickTarget = false;
        expect(observer).toHaveBeenCalledTimes(1);
        expect(stickEntity.showStickTarget).toEqual(false);
        stickEntity.showStickTarget = true;
        expect(observer).toHaveBeenCalledTimes(2);
        expect(stickEntity.showStickTarget).toEqual(true);
    });
    it("Notifies when the highlight stick targets changes", () => {
        const { observer, stickEntity } = makeTestRig();
        stickEntity.highlightTarget = true;
        observer.mockClear();
        stickEntity.highlightTarget = false;
        stickEntity.highlightTarget = false;
        stickEntity.highlightTarget = false;
        stickEntity.highlightTarget = false;
        expect(observer).toHaveBeenCalledTimes(1);
        expect(stickEntity.highlightTarget).toEqual(false);
        stickEntity.highlightTarget = true;
        expect(observer).toHaveBeenCalledTimes(2);
        expect(stickEntity.highlightTarget).toEqual(true);
    });
    it("Returns can be stuck as false if the transform and stick position the same", () => {
        const { transform, stickEntity } = makeTestRig();
        expect(stickEntity.canBeStuck).toEqual(false);
        transform.localPosition = new core_2.Vector3(1, 2, 3);
        expect(stickEntity.canBeStuck).toEqual(true);
        stickEntity.stickPosition = new core_2.Vector3(1, 2, 3);
        expect(stickEntity.canBeStuck).toEqual(false);
    });
    it("Returns can be stuck as false if the transform and stick scale the same", () => {
        const { transform, stickEntity } = makeTestRig();
        expect(stickEntity.canBeStuck).toEqual(false);
        transform.localScale = new core_2.Vector3(3, 3, 3);
        expect(stickEntity.canBeStuck).toEqual(true);
        stickEntity.stickScale = 3;
        expect(stickEntity.canBeStuck).toEqual(false);
    });
    it("Returns can be stuck as false if the transform and stick rotation the same", () => {
        const { transform, stickEntity } = makeTestRig();
        expect(stickEntity.canBeStuck).toEqual(false);
        const quat = core_2.Quaternion.FromAngleAxis(new core_2.Vector3(1, 2, 3), core_2.Angle.FromDegrees(45));
        transform.localRotation = quat;
        expect(stickEntity.canBeStuck).toEqual(true);
        stickEntity.stickRotation = quat;
        expect(stickEntity.canBeStuck).toEqual(false);
    });
    it("Does not update if the is stickable flag does not change", () => {
        const { transform, stickEntity, observer } = makeTestRig();
        transform.localPosition = new core_2.Vector3(1, 2, 3);
        expect(stickEntity.canBeStuck).toEqual(true);
        expect(observer).toHaveBeenCalled();
        observer.mockClear();
        transform.localPosition = new core_2.Vector3(2, 3, 4);
        expect(stickEntity.canBeStuck).toEqual(true);
        expect(observer).not.toHaveBeenCalled();
    });
    it("Sticking calls the Lerp on the transform", () => {
        const { transform, stickEntity } = makeTestRig();
        const lerpSpy = jest.spyOn(transform, "lerp");
        transform.localPosition = new core_2.Vector3(1, 2, 3);
        expect(stickEntity.canBeStuck).toEqual(true);
        const mockCB = jest.fn();
        stickEntity.doStick(mockCB);
        expect(lerpSpy).toHaveBeenCalled();
        const lerpDTO = lerpSpy.mock.calls[0][0];
        expect(lerpDTO.durationMS).toEqual(stickEntity.stickDurationMS);
        expect(lerpDTO.ease).toEqual(stickEntity.easeFn);
        expect(lerpDTO.desiredPosition).toEqual(stickEntity.stickPosition);
        expect(lerpDTO.desiredScale).toEqual(new core_2.Vector3(stickEntity.stickScale, stickEntity.stickScale, stickEntity.stickScale));
        expect(lerpDTO.desiredRotation).toEqual(stickEntity.stickRotation);
        expect(lerpDTO.onCompletedCallback).toEqual(mockCB);
    });
    it("Sticking does not call if it cannot be stuck", () => {
        const { transform, stickEntity } = makeTestRig();
        const lerpSpy = jest.spyOn(transform, "lerp");
        expect(stickEntity.canBeStuck).toEqual(false);
        stickEntity.doStick();
        expect(lerpSpy).not.toHaveBeenCalled();
    });
});
//# sourceMappingURL=Stickable.test.js.map