"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.defineEntityFixtures = exports.__ΩMockData = exports.__ΩTestFields = exports.__ΩStringifiedDates = void 0;
exports.prepareEntityFixtures = prepareEntityFixtures;
exports.loadEntityFixtures = loadEntityFixtures;
const __ΩInstanceType = ['T', 'args', 'new', 'InstanceType', 'l>e"!R"RPde#!P"@2"h"!/#qk#\'QRb!Pde"!p)w$y'];
const __ΩNonNullable = ['T', 'NonNullable', 'b!Pe"!PMKw"y'];
function __assignType(fn, args) {
    fn.__type = args;
    return fn;
}
/*@ts-ignore*/
var { __ΩActiveRecordClassType } = require('@deepkit/orm');
const sql_1 = require("@deepkit/sql");
const type_1 = require("@deepkit/type");
/*@ts-ignore*/
var { __ΩNewEntityFields } = require('../database');
const database_1 = require("../database");
const __ΩStringifiedDates = ['T', () => __ΩNonNullable, 'StringifiedDates', 'lG&Re\'!e&!fRPe#!Tqk#%QRPde$!e#!fo""p-Rb!Pde"!gN8!w#y'];
exports.__ΩStringifiedDates = __ΩStringifiedDates;
const __ΩIDefinedMock = ['T', () => __ΩActiveRecordClassType, () => SchemaSymbol, 'IDefinedMock', 'b!Pe"!Pn"4#MKw$y'];
const __ΩTestFields = ['T', () => __ΩStringifiedDates, () => __ΩNewEntityFields, () => __ΩInstanceType, 'TestFields', 'b!e!!o$"o#"o""w%y'];
exports.__ΩTestFields = __ΩTestFields;
const __ΩMockData = ['T', () => __ΩIDefinedMock, () => __ΩTestFields, 'MockData', 'b!e!!o#"o""w$y'];
exports.__ΩMockData = __ΩMockData;
const SchemaSymbol = Symbol('Schema');
exports.defineEntityFixtures = __assignType((cls, data) => {
    for (const key in data) {
        Object.defineProperty(data[key], SchemaSymbol, { enumerable: false, value: cls });
    }
    return data;
}, ['cls', () => __ΩTestFields, 'data', () => __ΩMockData, '', 'l-"o""R"o$"RP"2!Pd"N#!2#Pd"N(!/%']);
const isDate = __assignType((t) => t.kind === type_1.ReflectionKind.class && t.classType.name === 'Date', ['Type', 't', '', 'P"w!2""/#']);
function prepareEntityFixtures(entity, data) {
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    const result = { ...data };
    const type = (0, type_1.resolveRuntimeType)(entity);
    if (type.kind === type_1.ReflectionKind.class) {
        const props = type.types.filter(__assignType(t => t.kind === type_1.ReflectionKind.property, ['t', '', 'P"2!"/"']));
        const dateProps = props
            .filter(__assignType(p => {
            if (p.type.kind === type_1.ReflectionKind.class) {
                return isDate(p.type);
            }
            else if (p.type.kind === type_1.ReflectionKind.union) {
                return p.type.types.filter(sql_1.isNonUndefined).every(isDate);
            }
        }, ['p', '', 'P"2!"/"']))
            .map(__assignType(p => p.name, ['p', '', 'P"2!"/"']));
        for (const prop of dateProps) {
            if (typeof result[prop] === 'string') {
                // eslint-disable-next-line @typescript-eslint/no-explicit-any
                result[prop] = new Date(data[prop]);
            }
        }
    }
    return result;
}
prepareEntityFixtures.__type = ['entity', () => __ΩNewEntityFields, () => __ΩInstanceType, 'data', 'prepareEntityFixtures', 'P"2!"o#"o""2$"/%'];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
async function loadEntityFixtures(entities) {
    // todo: group by type & inject in bulk per type
    for (const entity of entities) {
        const data = prepareEntityFixtures(entity[SchemaSymbol], entity);
        await (0, database_1.createPersistedEntity)(entity[SchemaSymbol], data);
    }
}
loadEntityFixtures.__type = [() => __ΩIDefinedMock, 'entities', 'loadEntityFixtures', 'P"o!"F2""/#'];
//# sourceMappingURL=fixtures.js.map