import { ModelMetadataCollection } from "../collections/model-metadata";
import { FieldMetadataCollection } from "../collections/field-metadata";
import { OdooModelMetadata } from "./model-metadata";
import { OdooFieldMetadata } from "./field-metadata";

export class MetadataStorage {

    readonly models = new ModelMetadataCollection<OdooModelMetadata>();
    readonly fields = new FieldMetadataCollection<OdooFieldMetadata>();

    /**
     *
     */
    getModels(models?: Function[]): Array<any> {
        const allModelMetadatas = models ? this.models.filterByTargets(models) : this.models;

        return allModelMetadatas.toArray().map(modelMetadata => {
            return this.mergeWith(allModelMetadatas, modelMetadata);
        });
    }

    /**
     *
     */
    protected mergeWith(allModelMetadatas: ModelMetadataCollection<OdooModelMetadata>, model: OdooModelMetadata) {
        const fields = this.fields.filterByTarget(model.target);
        return {
            model: model,
            fields: fields
        }
    }
}