import { AppObjectPM } from "@vived/core";
import { Selectable, Selection } from "../Entities";
export const defaultSelectableStateVM = {
    isHovered: false,
    isPressed: false,
    isSelected: false,
    somethingElseIsSelected: false,
};
export class SelectableStatePM extends AppObjectPM {
    static get(id, appObjects) {
        return appObjects
            .get(id)
            ?.getComponent(SelectableStatePM.type);
    }
}
SelectableStatePM.type = "SelectableStatePM";
export function makeSelectableStatePM(appObject) {
    return new SelectableStatePMImp(appObject);
}
class SelectableStatePMImp extends SelectableStatePM {
    get selectable() {
        return this.getCachedLocalComponent(Selectable.type);
    }
    get selection() {
        return this.getCachedSingleton(Selection.type);
    }
    vmsAreEqual(a, b) {
        if (a.isHovered !== b.isHovered)
            return false;
        if (a.isPressed !== b.isPressed)
            return false;
        if (a.isSelected !== b.isSelected)
            return false;
        if (a.somethingElseIsSelected !== b.somethingElseIsSelected)
            return false;
        return true;
    }
    constructor(appObject) {
        super(appObject, SelectableStatePM.type);
        this.onSelectionChange = () => {
            if (!this.selectable || !this.selection)
                return;
            let somethingElseIsSelected = false;
            const selectedObject = this.selection.selectedObjectId;
            if (selectedObject !== null && selectedObject !== this.appObject.id) {
                somethingElseIsSelected = true;
            }
            const vm = {
                isHovered: this.selectable.hovered,
                isPressed: this.selectable.pressed,
                isSelected: this.selectable.selected,
                somethingElseIsSelected,
            };
            this.doUpdateView(vm);
        };
        this.dispose = () => {
            super.dispose();
            this.selectable?.removeChangeObserver(this.onSelectionChange);
            this.selection?.removeChangeObserver(this.onSelectionChange);
        };
        this.selectable?.addChangeObserver(this.onSelectionChange);
        this.selection?.addChangeObserver(this.onSelectionChange);
        this.onSelectionChange();
    }
}
//# sourceMappingURL=SelectableStatePM.js.map