"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SelectableStatePM = exports.defaultSelectableStateVM = void 0;
exports.makeSelectableStatePM = makeSelectableStatePM;
const core_1 = require("@vived/core");
const Entities_1 = require("../Entities");
exports.defaultSelectableStateVM = {
    isHovered: false,
    isPressed: false,
    isSelected: false,
    somethingElseIsSelected: false,
};
class SelectableStatePM extends core_1.AppObjectPM {
    static get(id, appObjects) {
        return appObjects
            .get(id)
            ?.getComponent(SelectableStatePM.type);
    }
}
exports.SelectableStatePM = SelectableStatePM;
SelectableStatePM.type = "SelectableStatePM";
function makeSelectableStatePM(appObject) {
    return new SelectableStatePMImp(appObject);
}
class SelectableStatePMImp extends SelectableStatePM {
    get selectable() {
        return this.getCachedLocalComponent(Entities_1.Selectable.type);
    }
    get selection() {
        return this.getCachedSingleton(Entities_1.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