"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.EntityFactory = exports.EntityFactoryBase = void 0;
const string_format_1 = __importDefault(require("string-format"));
const entity_construction_args_1 = require("../contracts/base/entity-construction-args");
const culture_load_option_1 = require("../contracts/querying/culture-load-option");
const error_messages_1 = __importDefault(require("../error-messages"));
const argument_error_1 = require("../errors/argument-error");
const guard_1 = __importDefault(require("../guard"));
const load_options_mapper_1 = require("../mappers/load-options-mapper");
const property_factory_1 = require("./property-factory");
class EntityFactoryBase {
    constructor(client, relationFactory, cultureLoader, entityCreator) {
        guard_1.default.notNullOrUndefined(client);
        guard_1.default.notNullOrUndefined(relationFactory);
        guard_1.default.notNullOrUndefined(cultureLoader);
        guard_1.default.notNullOrUndefined(entityCreator);
        this._client = client;
        this._relationFactory = relationFactory;
        this._cultureLoader = cultureLoader;
        this._entityCreator = entityCreator;
    }
    createAsync(param_1) {
        return __awaiter(this, arguments, void 0, function* (param, cultureLoadOption = culture_load_option_1.CultureLoadOption.Default) {
            let definition;
            if (typeof param === "string") {
                guard_1.default.stringNotNullOrEmpty(param);
                definition = yield this.getEntityDefinitionAsync(param);
            }
            else {
                guard_1.default.notNullOrUndefined(param);
                definition = param;
            }
            const cultures = yield load_options_mapper_1.LoadOptionsMapper.mapCultureLoadOptionsAsync(cultureLoadOption, this._cultureLoader);
            const properties = property_factory_1.PropertyFactory.createProperties(definition.getPropertyDefinitions(), cultures);
            const relations = this._relationFactory.createRelations(definition.getRelationDefinitions());
            const args = new entity_construction_args_1.EntityConstructionArgs(definition.name, {
                cultures: cultures,
                properties: properties,
                relations: relations,
            });
            const entity = this._entityCreator.createTypedEntity(args);
            entity.startTracking();
            return entity;
        });
    }
    getEntityDefinitionAsync(definitionName) {
        return __awaiter(this, void 0, void 0, function* () {
            const definition = yield this._client.entityDefinitions.getCachedAsync(definitionName);
            if (definition == null) {
                const errorMessage = (0, string_format_1.default)(error_messages_1.default.EntityFactory.DefinitionDoesNotExist, definitionName);
                throw new argument_error_1.ArgumentError(errorMessage);
            }
            return definition;
        });
    }
}
exports.EntityFactoryBase = EntityFactoryBase;
class EntityFactory extends EntityFactoryBase {
    constructor(client, relationFactory, cultureLoader, entityCreator) {
        super(client, relationFactory, cultureLoader, entityCreator);
    }
}
exports.EntityFactory = EntityFactory;
//# sourceMappingURL=entity-factory.js.map