"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RelationFactory = void 0;
const relation_cardinality_1 = require("../contracts/base/relation-cardinality");
const relation_role_1 = require("../contracts/base/relation-role");
const child_to_many_parents_relation_1 = require("../contracts/base/relations/child-to-many-parents-relation");
const child_to_one_parent_relation_1 = require("../contracts/base/relations/child-to-one-parent-relation");
const parent_to_many_children_relation_1 = require("../contracts/base/relations/parent-to-many-children-relation");
const parent_to_one_child_relation_1 = require("../contracts/base/relations/parent-to-one-child-relation");
const guard_1 = __importDefault(require("../guard"));
class RelationFactory {
    constructor(client) {
        this._client = client;
    }
    createChildToManyParentsRelation(definition) {
        const relation = new child_to_many_parents_relation_1.ChildToManyParentsRelation(definition.name, null, this._client);
        return relation;
    }
    createChildToOneParentRelation(definition) {
        const relation = new child_to_one_parent_relation_1.ChildToOneParentRelation(definition.name, null, this._client);
        return relation;
    }
    createParentToManyChildrenRelation(definition) {
        const relation = new parent_to_many_children_relation_1.ParentToManyChildrenRelation(definition.name, null, this._client);
        return relation;
    }
    createParentToOneChildRelation(definition) {
        const relation = new parent_to_one_child_relation_1.ParentToOneChildRelation(definition.name, null, this._client);
        return relation;
    }
    createRelation(definition) {
        guard_1.default.notNullOrUndefined(definition);
        let result = null;
        if (definition.role === relation_role_1.RelationRole.Parent) {
            if (definition.cardinality === relation_cardinality_1.RelationCardinality.ManyToMany ||
                definition.cardinality === relation_cardinality_1.RelationCardinality.OneToMany) {
                result = this.createParentToManyChildrenRelation(definition);
            }
            else if (definition.cardinality === relation_cardinality_1.RelationCardinality.OneToOne) {
                result = this.createParentToOneChildRelation(definition);
            }
        }
        else if (definition.role === relation_role_1.RelationRole.Child) {
            if (definition.cardinality === relation_cardinality_1.RelationCardinality.ManyToMany) {
                result = this.createChildToManyParentsRelation(definition);
            }
            else if (definition.cardinality === relation_cardinality_1.RelationCardinality.OneToOne ||
                definition.cardinality === relation_cardinality_1.RelationCardinality.OneToMany) {
                result = this.createChildToOneParentRelation(definition);
            }
        }
        if (result === null) {
            throw `Relation with name '${definition.name}', cardinality '${definition.cardinality}' and role '${definition.role}' not supported.`; //TODO
        }
        return result;
    }
    createRelations(definitions) {
        if (!definitions || !definitions.length) {
            return [];
        }
        const list = definitions.map(definition => {
            return this.createRelation(definition);
        });
        return list;
    }
}
exports.RelationFactory = RelationFactory;
//# sourceMappingURL=relation-factory.js.map