"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChannelModelEntity = exports.ChannelModelState = void 0;
exports.makeChannelModelEntity = makeChannelModelEntity;
const core_1 = require("@vived/core");
const Enablement_1 = require("../../Enablement");
const ChannelModelObject_1 = require("./ChannelModelObject");
var ChannelModelState;
(function (ChannelModelState) {
    ChannelModelState["INIT"] = "Initialized";
    ChannelModelState["LOADING"] = "Loading";
    ChannelModelState["READY"] = "Ready";
    ChannelModelState["ERROR"] = "Error";
})(ChannelModelState || (exports.ChannelModelState = ChannelModelState = {}));
class ChannelModelEntity extends core_1.AppObjectEntity {
    static get(id, appObjects) {
        const appObject = appObjects.get(id);
        if (!appObject) {
            appObjects.submitWarning("ChannelModel.get", "Unable to find app object by ID");
            return;
        }
        return appObject.getComponent(ChannelModelEntity.type);
    }
}
exports.ChannelModelEntity = ChannelModelEntity;
ChannelModelEntity.type = "ChannelModelEntity";
function makeChannelModelEntity(appObject) {
    return new ChannelModelEntityImp(appObject);
}
class ChannelModelEntityImp extends ChannelModelEntity {
    get id() {
        return this.appObject.id;
    }
    get name() {
        return this._name.val;
    }
    set name(name) {
        this._name.val = name;
    }
    get bounds_min() {
        return this._boundsMin.val;
    }
    set bounds_min(bounds_min) {
        this._boundsMin.val = bounds_min;
    }
    get bounds_max() {
        return this._boundsMax.val;
    }
    set bounds_max(bounds_max) {
        this._boundsMax.val = bounds_max;
    }
    get defaultPosition() {
        return this._defaultPosition.val;
    }
    set defaultPosition(position) {
        this._defaultPosition.val = position;
    }
    get defaultRotation() {
        return this._defaultRotation.val;
    }
    set defaultRotation(rotation) {
        this._defaultRotation.val = rotation;
    }
    get defaultScale() {
        return this._defaultScale.val;
    }
    set defaultScale(scale) {
        this._defaultScale.val = scale;
    }
    get rootRotation() {
        return this._rootRotation.val;
    }
    set rootRotation(rotation) {
        this._rootRotation.val = rotation;
    }
    get state() {
        return this._state.val;
    }
    set state(val) {
        this._state.val = val;
    }
    get modelError() {
        return this._error;
    }
    set modelError(val) {
        this._error = val;
        this._state.val = ChannelModelState.ERROR;
    }
    get enabled() {
        return this.enablement.enabled;
    }
    set enabled(val) {
        this.enablement.enabled = val;
    }
    get modelObjectIds() {
        return Array.from(this.modelObjectLookup.keys());
    }
    get data() {
        const modelObjects = Array.from(this.modelObjectLookup.values());
        const objects = modelObjects.map((obj) => {
            return {
                id: obj.id,
                label: obj.label.default,
                meshPaths: [...obj.subMeshPaths],
                tags: [...obj.tags],
            };
        });
        return {
            bounds_max: this._boundsMax.val.dto,
            bounds_min: this._boundsMin.val.dto,
            defaultPosition: this._defaultPosition.val.dto,
            defaultRotation: this._defaultRotation.val.dto,
            defaultScale: this._defaultScale.val,
            name: this._name.val,
            objects,
            version: 1,
            originalCollectionID: this.originalCollectionID,
            rootRotation: this._rootRotation.val.dto,
        };
    }
    constructor(appObject) {
        super(appObject, ChannelModelEntity.type);
        this._name = new core_1.MemoizedString("", this.notifyOnChange);
        this._boundsMin = new core_1.MemoizedVector3(core_1.Vector3.Zero(), this.notifyOnChange);
        this._boundsMax = new core_1.MemoizedVector3(core_1.Vector3.Zero(), this.notifyOnChange);
        this._defaultPosition = new core_1.MemoizedVector3(core_1.Vector3.Zero(), this.notifyOnChange);
        this._defaultRotation = new core_1.MemoizedQuaternion(core_1.Quaternion.Identity(), this.notifyOnChange);
        this._defaultScale = new core_1.MemoizedNumber(1, this.notifyOnChange);
        this._rootRotation = new core_1.MemoizedQuaternion(core_1.Quaternion.Identity(), this.notifyOnChange);
        this._state = new core_1.MemoizedString(ChannelModelState.INIT, this.notifyOnChange);
        this.modelObjectLookup = new Map();
        this.modelObjectFactory = (id) => {
            const ao = this.appObjects.getOrCreate(id);
            return (0, ChannelModelObject_1.makeChannelModelObject)(ao);
        };
        this.addModelObject = (id) => {
            const idToUse = id ?? (0, core_1.generateUniqueID)();
            const obj = this.modelObjectFactory(idToUse);
            this.modelObjectLookup.set(idToUse, obj);
            obj.addChangeObserver(this.notifyOnChange);
            this.notifyOnChange();
            return obj;
        };
        this.getModelObject = (id) => {
            return this.modelObjectLookup.get(id);
        };
        this.hasModelObject = (id) => {
            return this.modelObjectLookup.has(id);
        };
        this.modelObjects = () => {
            return Array.from(this.modelObjectLookup.values());
        };
        this.setupFromData = (data) => {
            if ("bounds_max" in data) {
                this.bounds_max = core_1.Vector3.FromDTO(data.bounds_max);
            }
            else {
                this.bounds_max = core_1.Vector3.Zero();
            }
            if ("bounds_min" in data) {
                this.bounds_min = core_1.Vector3.FromDTO(data.bounds_min);
            }
            else {
                this.bounds_min = core_1.Vector3.Zero();
            }
            if ("defaultPosition" in data) {
                this.defaultPosition = core_1.Vector3.FromDTO(data.defaultPosition);
            }
            else {
                this.defaultPosition = core_1.Vector3.Zero();
            }
            if ("defaultRotation" in data) {
                this.defaultRotation = core_1.Quaternion.FromDTO(data.defaultRotation);
            }
            else {
                this.defaultRotation = core_1.Quaternion.Identity();
            }
            if ("rootRotation" in data) {
                this.rootRotation = core_1.Quaternion.FromDTO(data.rootRotation);
            }
            else {
                this.rootRotation = core_1.Quaternion.Identity();
            }
            if ("defaultScale" in data) {
                this.defaultScale = data.defaultScale;
            }
            else {
                this.defaultScale = 1;
            }
            if ("name" in data) {
                this.name = data.name;
            }
            else {
                this.name = "Unnamed Channel";
            }
            if ("originalCollectionID" in data) {
                this.originalCollectionID = data.originalCollectionID;
            }
            if ("objects" in data) {
                const objects = data.objects;
                objects.forEach((obj) => {
                    if ("id" in obj) {
                        const modelObj = this.addModelObject(obj.id);
                        if ("label" in obj) {
                            modelObj.label.default = obj.label;
                        }
                        if ("meshPaths" in obj) {
                            modelObj.subMeshPaths = obj.meshPaths;
                        }
                        if ("tags" in obj) {
                            modelObj.tags = obj.tags;
                        }
                    }
                });
            }
        };
        this.enablement = Enablement_1.EnablementEntity.addIfMissing(appObject);
        this.enablement.addChangeObserver(this.notifyOnChange);
    }
}
//# sourceMappingURL=ChannelModel.js.map