"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ToolRepository = void 0;
exports.makeToolRepo = makeToolRepo;
const core_1 = require("@vived/core");
const core_2 = require("@vived/core");
const ToolEntity_1 = require("./ToolEntity");
class ToolRepository extends core_2.AppObjectEntity {
}
exports.ToolRepository = ToolRepository;
ToolRepository.type = "TOOL_REPOSITORY";
ToolRepository.get = (appObjects) => (0, core_1.getSingletonComponent)(ToolRepository.type, appObjects);
function makeToolRepo(appObject) {
    return new ToolRepositoryImp(appObject);
}
class ToolRepositoryImp extends ToolRepository {
    get allTools() {
        return Array.from(this.toolLookup.values());
    }
    setDefaultToolActive() {
        if (this.defaultToolType !== undefined) {
            this.triggerTool(this.defaultToolType);
        }
    }
    getTool(type) {
        return this.toolLookup.get(type);
    }
    getToolForHotkey(hotkey) {
        let tool;
        this.allTools.forEach((t) => {
            if (t.isMyHotkey(hotkey)) {
                tool = t;
            }
        });
        return tool;
    }
    get activeTool() {
        if (!this._activeToolType)
            return undefined;
        return this.getTool(this._activeToolType);
    }
    triggerTool(type) {
        if (this._activeToolType === type)
            return;
        const newActiveTool = this.toolLookup.get(type);
        if (!newActiveTool) {
            this.warn(`Unable to find tool ${type}`);
            return;
        }
        if (newActiveTool.isAction) {
            newActiveTool.triggerActions();
            return;
        }
        if (newActiveTool.state === ToolEntity_1.ToolState.DISABLED) {
            return;
        }
        if (newActiveTool.state === ToolEntity_1.ToolState.HIDDEN) {
            return;
        }
        this.toolLookup.forEach((tool) => {
            if (tool.toolType === type) {
                this._activeToolType = type;
                tool.state = ToolEntity_1.ToolState.ACTIVE;
            }
            else if (tool.state === ToolEntity_1.ToolState.ACTIVE) {
                tool.state = ToolEntity_1.ToolState.ENABLED;
            }
        });
        this.notifyOnChange();
    }
    makeTool(toolType) {
        const tool = this.toolFactory(toolType);
        if (this.toolLookup.has(toolType)) {
            this.warn(`A tool with the ID ${toolType} already exists. Replacing`);
        }
        this.toolLookup.set(toolType, tool);
        return tool;
    }
    makeToolFromData(toolData) {
        const { toolType, isDefault, label, shortcutLabel, hotkeys } = toolData;
        const tool = this.makeTool(toolType);
        tool.label = label;
        tool.shortcutLabel = shortcutLabel;
        tool.hotkeys = hotkeys;
        if (isDefault) {
            this.defaultToolType = toolType;
            this.triggerTool(toolData.toolType);
        }
        else if (this.activeTool === undefined) {
            this.triggerTool(toolData.toolType);
        }
        return tool;
    }
    constructor(appObject) {
        super(appObject, ToolRepository.type);
        this.toolLookup = new Map();
        this.toolFactory = (id) => {
            const ao = this.appObjects.getOrCreate(id);
            const toolEntity = (0, ToolEntity_1.makeToolEntity)(ao);
            return toolEntity;
        };
    }
}
//# sourceMappingURL=ToolRepository.js.map