import ContextModel from "./ContextModel.js";
import Model from "../Data/Model.js";
import { default as EditorCollection } from "./EditorCollection.js";
import { default as ErrorModel } from "./ErrorModel.js";
export default class RootModel extends Model {
    constructor(options) {
        super(options);
        this.m_errors = [];
        this.m_context = new ContextModel({
            root: this,
            parent: this,
            data: options.context,
        });
        this.m_editorCollection =
            new EditorCollection(options.editors);
        this.loadErrors(options.errors);
    }
    get context() {
        return this.m_context;
    }
    get editors() {
        return this.m_editorCollection;
    }
    get initialized() {
        return this.state.get("initialized");
    }
    get initializers() {
        return this.state.get("initializers");
    }
    get errors() {
        return this.m_errors;
    }
    get postbackHandler() {
        return this.m_postbackHandlerFn || (() => { });
    }
    set postbackHandler(callback) {
        this.m_postbackHandlerFn = callback;
    }
    clearErrors() {
        this.m_errors = [];
        this.context.featureSet.setErrors();
    }
    loadErrors(errors) {
        if (!_.isArray(errors)) {
            return;
        }
        this.clearErrors();
        errors.forEach(error => {
            this.m_errors[this.m_errors.length] =
                new ErrorModel(error);
        });
        this.context.featureSet.setErrors();
    }
    toJSON() {
        return {
            context: this.context.toJSON(),
            editors: this.editors.toJSON(),
            errors: this.errors.map(error => {
                return error.toJSON();
            }),
            initialized: this.initialized,
            intializers: this.initializers
        };
    }
}
