"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const typescript_1 = __importDefault(require("typescript"));
const fs_1 = __importDefault(require("fs"));
const printer = typescript_1.default.createPrinter();
const prepareFabricFile = (folder) => {
    const staticImports = [
        typescript_1.default.createImportDeclaration(undefined, undefined, typescript_1.default.createImportClause(undefined, typescript_1.default.createNamedImports([
            typescript_1.default.createImportSpecifier(undefined, typescript_1.default.createIdentifier("AbstractTable")),
            typescript_1.default.createImportSpecifier(undefined, typescript_1.default.createIdentifier("DB")),
        ])), typescript_1.default.createStringLiteral("@lambda-team/ltdl")),
        typescript_1.default.createImportDeclaration(undefined, undefined, typescript_1.default.createImportClause(undefined, typescript_1.default.createNamedImports([
            typescript_1.default.createImportSpecifier(undefined, typescript_1.default.createIdentifier("Pool")),
        ])), typescript_1.default.createStringLiteral("pg")),
        typescript_1.default.createImportDeclaration(undefined, undefined, typescript_1.default.createImportClause(typescript_1.default.createIdentifier("Serializer"), undefined), typescript_1.default.createStringLiteral("./serializer")),
    ];
    const dynamicImports = [];
    const filenames = fs_1.default.readdirSync(folder);
    for (let i = 0; i < filenames.length; i++) {
        const filename = filenames[i];
        const importPath = `${folder}/${filename.split(".")[0]}/`;
        dynamicImports.push(typescript_1.default.createImportDeclaration(undefined, undefined, typescript_1.default.createImportClause(undefined, typescript_1.default.createNamespaceImport(typescript_1.default.createIdentifier(`i${i}`))), typescript_1.default.createStringLiteral(importPath)));
    }
    const variablesStatements = [
        typescript_1.default.createVariableStatement(undefined, typescript_1.default.createVariableDeclarationList([
            typescript_1.default.createVariableDeclaration(typescript_1.default.createIdentifier("db"), undefined, typescript_1.default.createNew(typescript_1.default.createIdentifier("DB"), undefined, [
                typescript_1.default.createNew(typescript_1.default.createIdentifier("Pool"), undefined, []),
            ])),
        ], typescript_1.default.NodeFlags.Const)),
        typescript_1.default.createVariableStatement(undefined, typescript_1.default.createVariableDeclarationList([
            typescript_1.default.createVariableDeclaration(typescript_1.default.createIdentifier("serializer"), undefined, typescript_1.default.createNew(typescript_1.default.createIdentifier("Serializer"), undefined, [])),
        ], typescript_1.default.NodeFlags.Const)),
    ];
    const blockStatements = [];
    // const tables = []
    blockStatements.push(typescript_1.default.createVariableStatement(undefined, typescript_1.default.createVariableDeclarationList([
        typescript_1.default.createVariableDeclaration(typescript_1.default.createIdentifier("tables"), undefined, typescript_1.default.createArrayLiteral([], false)),
    ], typescript_1.default.NodeFlags.Const)));
    for (let i = 0; i < filenames.length; i++) {
        // const t1 = (new i1.default(db) as unknown as AbstractTable<any>);
        // tables.push(t1)
        const blockStatement = [
            typescript_1.default.createVariableStatement(undefined, typescript_1.default.createVariableDeclarationList([
                typescript_1.default.createVariableDeclaration(typescript_1.default.createIdentifier("t" + i), undefined, typescript_1.default.createParen(typescript_1.default.createAsExpression(typescript_1.default.createAsExpression(typescript_1.default.createNew(typescript_1.default.createPropertyAccess(typescript_1.default.createIdentifier("i" + i), typescript_1.default.createIdentifier("default")), undefined, [typescript_1.default.createIdentifier("db")]), typescript_1.default.createKeywordTypeNode(typescript_1.default.SyntaxKind.UnknownKeyword)), typescript_1.default.createTypeReferenceNode(typescript_1.default.createIdentifier("AbstractTable"), [
                    typescript_1.default.createKeywordTypeNode(typescript_1.default.SyntaxKind.AnyKeyword),
                ])))),
            ], typescript_1.default.NodeFlags.Const)),
            typescript_1.default.createExpressionStatement(typescript_1.default.createCall(typescript_1.default.createPropertyAccess(typescript_1.default.createIdentifier("tables"), typescript_1.default.createIdentifier("push")), undefined, [typescript_1.default.createIdentifier("t" + i)])),
        ];
        blockStatements.push(...blockStatement);
    }
    // return serializer.generate(tables)
    blockStatements.push(typescript_1.default.createReturn(typescript_1.default.createCall(typescript_1.default.createPropertyAccess(typescript_1.default.createIdentifier("serializer"), typescript_1.default.createIdentifier("generate")), undefined, [typescript_1.default.createIdentifier("tables")])));
    const funcStatement = [
        typescript_1.default.createVariableStatement(undefined, typescript_1.default.createVariableDeclarationList([
            typescript_1.default.createVariableDeclaration(typescript_1.default.createIdentifier("testFun"), undefined, typescript_1.default.createArrowFunction(undefined, undefined, [], undefined, typescript_1.default.createToken(typescript_1.default.SyntaxKind.EqualsGreaterThanToken), 
            // function body
            typescript_1.default.createBlock(blockStatements, true))),
        ], typescript_1.default.NodeFlags.Const)),
    ];
    const invocationStatement = [
        typescript_1.default.createExpressionStatement(typescript_1.default.createCall(typescript_1.default.createIdentifier("testFun"), undefined, [])),
    ];
    const outFile = typescript_1.default.createSourceFile("outfile.ts", "", typescript_1.default.ScriptTarget.ES2015, true, typescript_1.default.ScriptKind.TS);
    const source = [];
    source.push(...staticImports);
    source.push(...dynamicImports);
    source.push(...variablesStatements);
    source.push(...funcStatement);
    source.push(...invocationStatement);
    const newFile = typescript_1.default.factory.updateSourceFile(outFile, source);
    return printer.printFile(newFile);
};
exports.default = prepareFabricFile;
//# sourceMappingURL=factory.js.map