"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.defineEntityFixtures = defineEntityFixtures;
exports.prepareEntityFixtures = prepareEntityFixtures;
exports.loadEntityFixtures = loadEntityFixtures;
const reflection_1 = require("../reflection");
const database_1 = require("../database");
const SchemaSymbol = Symbol('Schema');
function defineEntityFixtures(cls, data) {
    for (const key in data) {
        Object.defineProperty(data[key], SchemaSymbol, { enumerable: false, value: cls });
    }
    return data;
}
function prepareEntityFixtures(entity, data) {
    const result = { ...data };
    const dateProperties = reflection_1.ReflectionClass.from(entity)
        .getProperties()
        .filter(property => isDatePropertyType(property.getType()))
        .map(property => property.getNameAsString());
    for (const property of dateProperties) {
        if (typeof result[property] === 'string')
            result[property] = new Date(result[property]);
    }
    return result;
}
async function loadEntityFixtures(entities) {
    for (const entity of entities) {
        const data = prepareEntityFixtures(entity[SchemaSymbol], entity);
        await (0, database_1.createPersistedEntity)(entity[SchemaSymbol], data);
    }
}
function isDatePropertyType(type) {
    if (isDate(type))
        return true;
    if (type.kind === reflection_1.ReflectionKind.intersection)
        return type.types.some(isDatePropertyType);
    if (type.kind !== reflection_1.ReflectionKind.union)
        return false;
    const concrete = type.types.filter(item => item.kind !== reflection_1.ReflectionKind.undefined && item.kind !== reflection_1.ReflectionKind.null);
    return concrete.length > 0 && concrete.every(isDatePropertyType);
}
function isDate(type) {
    return type.kind === reflection_1.ReflectionKind.class && (type.classType === Date || type.classType.name === 'Date');
}
//# sourceMappingURL=fixtures.js.map