"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.EntityLoadConfigurationBuilder = void 0;
const guard_1 = __importDefault(require("../../guard"));
const culture_load_option_1 = require("./culture-load-option");
const entity_load_configuration_1 = require("./entity-load-configuration");
const load_options_1 = require("./load-options");
const property_load_option_1 = require("./property-load-option");
const relation_load_option_1 = require("./relation-load-option");
const relation_specification_1 = require("./relation-specification");
class EntityLoadConfigurationBuilder {
    constructor(entityLoadConfiguration) {
        this._entityLoadConfiguration = new entity_load_configuration_1.EntityLoadConfiguration();
        if (entityLoadConfiguration)
            this._entityLoadConfiguration = entityLoadConfiguration.deepClone();
    }
    inCultures(option) {
        guard_1.default.notNullOrUndefined(option);
        if (option instanceof Array) {
            this._entityLoadConfiguration.cultureLoadOption =
                this._entityLoadConfiguration.cultureLoadOption || new culture_load_option_1.CultureLoadOption();
            this._entityLoadConfiguration.cultureLoadOption.loadOption = load_options_1.LoadOption.Custom;
            this._entityLoadConfiguration.cultureLoadOption.cultures = [
                ...new Set((this._entityLoadConfiguration.cultureLoadOption.cultures || []).concat(option)),
            ];
        }
        else {
            this._entityLoadConfiguration.cultureLoadOption =
                this._entityLoadConfiguration.cultureLoadOption || new culture_load_option_1.CultureLoadOption();
            this._entityLoadConfiguration.cultureLoadOption.loadOption = option;
        }
        return this;
    }
    inCulture(culture) {
        guard_1.default.notUndefined(culture);
        guard_1.default.stringNotNullOrEmpty(culture);
        return this.inCultures([culture]);
    }
    withProperties(option) {
        guard_1.default.notNullOrUndefined(option);
        if (option instanceof Array) {
            this._entityLoadConfiguration.propertyLoadOption =
                this._entityLoadConfiguration.propertyLoadOption || new property_load_option_1.PropertyLoadOption();
            this._entityLoadConfiguration.propertyLoadOption.loadOption = load_options_1.LoadOption.Custom;
            this._entityLoadConfiguration.propertyLoadOption.properties = [
                ...new Set((this._entityLoadConfiguration.propertyLoadOption.properties || []).concat(option)),
            ];
        }
        else {
            this._entityLoadConfiguration.propertyLoadOption =
                this._entityLoadConfiguration.propertyLoadOption || new property_load_option_1.PropertyLoadOption();
            this._entityLoadConfiguration.propertyLoadOption.loadOption = option;
        }
        return this;
    }
    withProperty(property) {
        guard_1.default.notUndefined(property);
        guard_1.default.stringNotNullOrEmpty(property);
        return this.withProperties([property]);
    }
    withRelations(option) {
        guard_1.default.notNullOrUndefined(option);
        if (option instanceof Array) {
            let relationSpecifications = option.map(relation => {
                return typeof relation === "string" ? new relation_specification_1.RelationSpecification(relation) : relation;
            });
            this._entityLoadConfiguration.relationLoadOption =
                this._entityLoadConfiguration.relationLoadOption || new relation_load_option_1.RelationLoadOption();
            this._entityLoadConfiguration.relationLoadOption.loadOption = load_options_1.LoadOption.Custom;
            relationSpecifications = relationSpecifications.filter(value => {
                return (this._entityLoadConfiguration.relationLoadOption.relations.findIndex(relationValue => {
                    if (typeof value === "string") {
                        return relationValue.name === value;
                    }
                    else {
                        return relationValue.deepEquals(value);
                    }
                }) === -1);
            });
            this._entityLoadConfiguration.relationLoadOption.relations = (this._entityLoadConfiguration.relationLoadOption.relations || []).concat(relationSpecifications);
        }
        else {
            this._entityLoadConfiguration.relationLoadOption =
                this._entityLoadConfiguration.relationLoadOption || new relation_load_option_1.RelationLoadOption();
            this._entityLoadConfiguration.relationLoadOption.loadOption = option;
        }
        return this;
    }
    withRelation(relation, role) {
        guard_1.default.notUndefined(relation);
        guard_1.default.stringNotNullOrEmpty(relation);
        return this.withRelations([new relation_specification_1.RelationSpecification(relation, role)]);
    }
    build() {
        return this._entityLoadConfiguration.deepClone();
    }
}
exports.EntityLoadConfigurationBuilder = EntityLoadConfigurationBuilder;
//# sourceMappingURL=entity-load-configuration-builder.js.map