"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@vived/core");
const ToolEntity_1 = require("./ToolEntity");
const ToolRepository_1 = require("./ToolRepository");
function makeTestRig() {
    const appObjects = (0, core_1.makeAppObjectRepo)();
    const repoAO = appObjects.getOrCreate("ToolRepo");
    const repo = (0, ToolRepository_1.makeToolRepo)(repoAO);
    const observer = jest.fn();
    repo.addChangeObserver(observer);
    repo.makeToolFromData({
        toolType: "Tool1",
        isDefault: true,
        hotkeys: [{ keyCode: "Key1" }],
        label: "Tool One",
        shortcutLabel: "[1]",
    });
    repo.makeToolFromData({
        toolType: "Tool2",
        isDefault: false,
        hotkeys: [{ keyCode: "Key2" }],
        label: "Tool Two",
        shortcutLabel: "[2]",
    });
    repo.makeToolFromData({
        toolType: "Tool3",
        isDefault: false,
        hotkeys: [{ keyCode: "Key2" }],
        label: "Tool Three",
        shortcutLabel: "[2]",
    });
    return { repo, observer, appObjects };
}
describe("Tool Repository Entity", () => {
    it("Gets all tools", () => {
        const { repo } = makeTestRig();
        expect(repo.allTools).toHaveLength(3);
        expect(repo.allTools[0].toolType).toEqual("Tool1");
        expect(repo.allTools[1].toolType).toEqual("Tool2");
        expect(repo.allTools[2].toolType).toEqual("Tool3");
    });
    it("Initializes the active tool", () => {
        const { repo } = makeTestRig();
        expect(repo.activeTool?.toolType).toEqual("Tool1");
    });
    it("Gets a tool", () => {
        const { repo } = makeTestRig();
        const tool1 = repo.getTool("Tool1");
        expect(tool1?.toolType).toEqual("Tool1");
    });
    it("Returns an undefined if it cannot find a tool", () => {
        const { repo } = makeTestRig();
        const tool = repo.getTool("Unknown");
        expect(tool).toBeUndefined();
    });
    it("Sets the active tool", () => {
        const { repo } = makeTestRig();
        expect(repo.activeTool?.toolType).toEqual("Tool1");
        repo.triggerTool("Tool2");
        expect(repo.activeTool?.toolType).toEqual("Tool2");
    });
    it("Notifies when a different tool is set active", () => {
        const { repo, observer } = makeTestRig();
        observer.mockClear();
        expect(repo.activeTool?.toolType).toEqual("Tool1");
        repo.triggerTool("Tool2");
        expect(observer).toHaveBeenCalled();
    });
    it("Does not notify when a the same tool is set active", () => {
        const { repo, observer } = makeTestRig();
        observer.mockClear();
        expect(repo.activeTool?.toolType).toEqual("Tool1");
        repo.triggerTool("Tool1");
        repo.triggerTool("Tool1");
        repo.triggerTool("Tool1");
        expect(observer).not.toHaveBeenCalled();
    });
    it("Warns if an unknown tool is set active", () => {
        const { repo, observer, appObjects } = makeTestRig();
        observer.mockClear();
        appObjects.submitWarning = jest.fn();
        expect(repo.activeTool?.toolType).toEqual("Tool1");
        repo.triggerTool("Unknown");
        expect(observer).not.toHaveBeenCalled();
        expect(repo.activeTool?.toolType).toEqual("Tool1");
        expect(appObjects.submitWarning).toHaveBeenCalled();
    });
    it("Sets the default tool active", () => {
        const { repo, observer } = makeTestRig();
        repo.triggerTool("Tool2");
        observer.mockClear();
        repo.setDefaultToolActive();
        expect(repo.activeTool?.toolType).toEqual("Tool1");
        expect(observer).toHaveBeenCalled();
    });
    it("Does not notify if the default tool is already active", () => {
        const { repo, observer } = makeTestRig();
        observer.mockClear();
        repo.setDefaultToolActive();
        repo.setDefaultToolActive();
        repo.setDefaultToolActive();
        expect(observer).not.toHaveBeenCalled();
    });
    it("Sets the states of the tool entities", () => {
        const { repo } = makeTestRig();
        expect(repo.getTool("Tool1")?.state).toEqual(ToolEntity_1.ToolState.ACTIVE);
        expect(repo.getTool("Tool2")?.state).toEqual(ToolEntity_1.ToolState.ENABLED);
        repo.triggerTool("Tool2");
        expect(repo.getTool("Tool1")?.state).toEqual(ToolEntity_1.ToolState.ENABLED);
        expect(repo.getTool("Tool2")?.state).toEqual(ToolEntity_1.ToolState.ACTIVE);
    });
    it("Does not set the state of a disabled tool", () => {
        const { repo, observer } = makeTestRig();
        observer.mockClear();
        repo.getTool("Tool2").state = ToolEntity_1.ToolState.DISABLED;
        repo.triggerTool("Tool2");
        expect(observer).not.toHaveBeenCalled();
        expect(repo.getTool("Tool1")?.state).toEqual(ToolEntity_1.ToolState.ACTIVE);
        expect(repo.getTool("Tool2")?.state).toEqual(ToolEntity_1.ToolState.DISABLED);
    });
    it("Does not set the state of a hidden tool", () => {
        const { repo, observer } = makeTestRig();
        observer.mockClear();
        repo.getTool("Tool2").state = ToolEntity_1.ToolState.HIDDEN;
        repo.triggerTool("Tool2");
        expect(observer).not.toHaveBeenCalled();
        expect(repo.getTool("Tool1")?.state).toEqual(ToolEntity_1.ToolState.ACTIVE);
        expect(repo.getTool("Tool2")?.state).toEqual(ToolEntity_1.ToolState.HIDDEN);
    });
    it("Returns the tool for a given key code", () => {
        const { repo } = makeTestRig();
        const tool = repo.getToolForHotkey({ keyCode: "Key1" });
        expect(tool?.toolType).toEqual("Tool1");
    });
    it("Returns undefined for a key code without a tool", () => {
        const { repo } = makeTestRig();
        const tool = repo.getToolForHotkey({ keyCode: "unknown" });
        expect(tool).toBeUndefined();
    });
    it("Triggers an tool's action if it is an action tool", () => {
        const { repo } = makeTestRig();
        const actionTool = repo.getTool("Tool3");
        const mockAction = jest.fn();
        actionTool.registerAction(mockAction);
        repo.triggerTool("Tool3");
        expect(mockAction).toHaveBeenCalled();
    });
    it("Does not change the active tool if the tool set active is an action", () => {
        const { repo } = makeTestRig();
        const actionTool = repo.getTool("Tool3");
        const mockAction = jest.fn();
        actionTool.registerAction(mockAction);
        expect(repo.activeTool?.toolType).toEqual("Tool1");
        repo.triggerTool("Tool3");
        expect(repo.activeTool?.toolType).toEqual("Tool1");
    });
});
//# sourceMappingURL=ToolRepository.test.js.map