import { getSingletonComponent } from "@vived/core";
import { AppObjectEntity } from "@vived/core";
import { makeToolEntity, ToolState } from "./ToolEntity";
export class ToolRepository extends AppObjectEntity {
}
ToolRepository.type = "TOOL_REPOSITORY";
ToolRepository.get = (appObjects) => getSingletonComponent(ToolRepository.type, appObjects);
export 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 === ToolState.DISABLED) {
            return;
        }
        if (newActiveTool.state === ToolState.HIDDEN) {
            return;
        }
        this.toolLookup.forEach((tool) => {
            if (tool.toolType === type) {
                this._activeToolType = type;
                tool.state = ToolState.ACTIVE;
            }
            else if (tool.state === ToolState.ACTIVE) {
                tool.state = 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 = makeToolEntity(ao);
            return toolEntity;
        };
    }
}
//# sourceMappingURL=ToolRepository.js.map