import { AppObjectEntity, MemoizedString, } from "@vived/core";
export var ToolState;
(function (ToolState) {
    ToolState["HIDDEN"] = "HIDDEN";
    ToolState["DISABLED"] = "DISABLED";
    ToolState["ENABLED"] = "ENABLED";
    ToolState["ACTIVE"] = "ACTIVE";
})(ToolState || (ToolState = {}));
export class ToolEntity extends AppObjectEntity {
    static get(id, appObjects) {
        return appObjects.get(id)?.getComponent(ToolEntity.type);
    }
}
ToolEntity.type = "TOOL";
export function makeToolEntity(appObject) {
    return new ToolEntityImp(appObject);
}
class ToolEntityImp extends ToolEntity {
    get toolType() {
        return this.appObject.id;
    }
    get label() {
        return this.memoizedLabel.val;
    }
    set label(toolLabel) {
        this.memoizedLabel.val = toolLabel;
    }
    get shortcutLabel() {
        return this.memoizedShortcut.val;
    }
    set shortcutLabel(shorcut) {
        this.memoizedShortcut.val = shorcut;
    }
    get state() {
        return this.memoizedState.val;
    }
    set state(val) {
        this.memoizedState.val = val;
    }
    get isAction() {
        return this._isAction;
    }
    get hotkeys() {
        return this._hotkeys;
    }
    set hotkeys(val) {
        this._hotkeys = val;
        this.notifyOnChange();
    }
    hotkeysMatch(a, b) {
        if (a.keyCode !== b.keyCode)
            return false;
        const aComplete = this.fillInMissingModifiers(a);
        const bComplete = this.fillInMissingModifiers(b);
        if (aComplete.alt !== bComplete.alt)
            return false;
        if (aComplete.control !== bComplete.control)
            return false;
        if (aComplete.meta !== bComplete.meta)
            return false;
        if (aComplete.shift !== bComplete.shift)
            return false;
        return true;
    }
    fillInMissingModifiers(hotkey) {
        const rVal = { ...hotkey };
        rVal.alt = hotkey.alt === undefined ? false : hotkey.alt;
        rVal.control = hotkey.control === undefined ? false : hotkey.control;
        rVal.meta = hotkey.meta === undefined ? false : hotkey.meta;
        rVal.shift = hotkey.shift === undefined ? false : hotkey.shift;
        return rVal;
    }
    constructor(appObject) {
        super(appObject, ToolEntity.type);
        this.memoizedLabel = new MemoizedString("", this.notifyOnChange);
        this.memoizedShortcut = new MemoizedString("", this.notifyOnChange);
        this.memoizedState = new MemoizedString(ToolState.ENABLED, this.notifyOnChange);
        this.actions = [];
        this._isAction = false;
        this.registerAction = (action) => {
            this.actions.push(action);
            this._isAction = true;
        };
        this.triggerActions = () => {
            this.actions.forEach((action) => {
                action();
            });
        };
        this._hotkeys = [];
        this.isMyHotkey = (hotkey) => {
            let isMine = false;
            this.hotkeys.forEach((myHotkey) => {
                if (this.hotkeysMatch(myHotkey, hotkey)) {
                    isMine = true;
                }
            });
            return isMine;
        };
    }
}
//# sourceMappingURL=ToolEntity.js.map