"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChannelModelPM = exports.defaultChannelModelVM = void 0;
exports.makeChannelModelPM = makeChannelModelPM;
const core_1 = require("@vived/core");
const Entities_1 = require("../Entities");
exports.defaultChannelModelVM = {
    childHasValidationMessage: false,
    modelObjectIds: [],
};
class ChannelModelPM extends core_1.AppObjectPM {
    static get(id, appObjects) {
        return appObjects
            .get(id)
            ?.getComponent(ChannelModelPM.type);
    }
}
exports.ChannelModelPM = ChannelModelPM;
ChannelModelPM.type = "ChannelModelPM";
function makeChannelModelPM(appObject) {
    return new ChannelModelPMImp(appObject);
}
class ChannelModelPMImp extends ChannelModelPM {
    get modelEntity() {
        return this.getCachedLocalComponent(Entities_1.ChannelModelEntity.type);
    }
    vmsAreEqual(a, b) {
        if (a.childHasValidationMessage !== b.childHasValidationMessage)
            return false;
        if (a.modelObjectIds.length !== b.modelObjectIds.length)
            return false;
        let objectsAreTheSame = true;
        a.modelObjectIds.forEach((objA, i) => {
            const objB = b.modelObjectIds[i];
            if (objA !== objB) {
                objectsAreTheSame = false;
            }
        });
        return objectsAreTheSame;
    }
    constructor(appObject) {
        super(appObject, ChannelModelPM.type);
        this.onEntityChange = () => {
            if (!this.modelEntity)
                return;
            let childHasValidationMessage = false;
            this.modelEntity.modelObjects().forEach((obj) => {
                if (childHasValidationMessage) {
                    // Don't need to keep checking
                    return;
                }
                if (obj.label.customLabelValidationMessage !== null) {
                    childHasValidationMessage = true;
                }
            });
            const vm = {
                childHasValidationMessage,
                modelObjectIds: this.modelEntity.modelObjectIds,
            };
            this.doUpdateView(vm);
        };
        this.modelEntity?.addChangeObserver(this.onEntityChange);
        this.onEntityChange();
    }
}
//# sourceMappingURL=ChannelModelPM.js.map