import { getSingletonComponent } from "@vived/core";
import { AppObjectEntity } from "@vived/core";
import { makeSelectable } from "./Selectable";
export class Selection extends AppObjectEntity {
    static get(appObjects) {
        return getSingletonComponent(Selection.type, appObjects);
    }
}
Selection.type = "SELECTION_ENTITY";
export function makeSelection(appObj) {
    return new SelectionImp(appObj);
}
class SelectionImp extends Selection {
    get pressedObjectId() {
        return this._pressed;
    }
    set pressedObjectId(val) {
        let finalVal = null;
        if (val !== null && this.selectableLookup.has(val)) {
            finalVal = val;
        }
        if (this._pressed === finalVal)
            return;
        const prevPressed = this._pressed;
        this._pressed = finalVal;
        if (prevPressed !== null) {
            const selectable = this.selectableLookup.get(prevPressed);
            if (selectable)
                selectable.pressed = false;
        }
        if (this._pressed !== null) {
            const selectable = this.selectableLookup.get(this._pressed);
            if (selectable)
                selectable.pressed = true;
        }
        this.notifyOnChange();
        return;
    }
    get selectedObjectId() {
        return this._selected;
    }
    set selectedObjectId(val) {
        let finalVal = null;
        if (val !== null && this.selectableLookup.has(val)) {
            finalVal = val;
        }
        if (this._selected === finalVal)
            return;
        const prevSelected = this._selected;
        this._selected = finalVal;
        if (prevSelected !== null) {
            const selectable = this.selectableLookup.get(prevSelected);
            if (selectable)
                selectable.selected = false;
        }
        if (this._selected !== null) {
            const selectable = this.selectableLookup.get(this._selected);
            if (selectable)
                selectable.selected = true;
        }
        this.notifyOnChange();
        return;
    }
    get hoveredObjectId() {
        return this._hovered;
    }
    set hoveredObjectId(val) {
        let finalVal = null;
        if (val !== null && this.selectableLookup.has(val)) {
            finalVal = val;
        }
        if (this._hovered === finalVal)
            return;
        const prevPressed = this._hovered;
        this._hovered = finalVal;
        if (prevPressed !== null) {
            const selectable = this.selectableLookup.get(prevPressed);
            if (selectable)
                selectable.hovered = false;
        }
        if (this._hovered !== null) {
            const selectable = this.selectableLookup.get(this._hovered);
            if (selectable)
                selectable.hovered = true;
        }
        this.notifyOnChange();
        return;
    }
    removeSelectable(selectable) {
        const id = selectable.appObject.id;
        if (!this.selectableLookup.has(id))
            return;
        this.selectableLookup.delete(id);
        if (this._pressed === id) {
            this.pressedObjectId = null;
        }
        if (this._hovered === id) {
            this.hoveredObjectId = null;
        }
        if (this._selected === id) {
            this.selectedObjectId = null;
        }
    }
    addSelectable(selectable) {
        selectable.inSelection = this;
        this.selectableLookup.set(selectable.appObject.id, selectable);
    }
    hasSelectable(id) {
        return this.selectableLookup.has(id);
    }
    makeAppObjectSelectable(appObject) {
        const selectable = makeSelectable(appObject);
        this.addSelectable(selectable);
        return selectable;
    }
    constructor(appObj) {
        super(appObj, Selection.type);
        this.selectableLookup = new Map();
        this._pressed = null;
        this._selected = null;
        this._hovered = null;
    }
}
//# sourceMappingURL=Selection.js.map