"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.generate = void 0;
const ts_morph_1 = require("ts-morph");
const codehandler_1 = require("@syftdata/codehandler");
const utils_1 = require("@syftdata/common/lib/utils");
const fs = require("fs");
const path = require("path");
function generate(ast, destinationDir, pkg) {
    const project = new ts_morph_1.Project({
        compilerOptions: {
            target: ts_morph_1.ScriptTarget.ES2015,
            declaration: true,
            declarationMap: true,
            sourceMap: false,
            strictNullChecks: true,
            module: ts_morph_1.ModuleKind.CommonJS,
            noImplicitAny: false,
            removeComments: false
        }
    });
    const dir = project.createDirectory(destinationDir);
    const sourceFile = project.createSourceFile(path.join(destinationDir, 'lib', 'generated.ts'), (writer) => {
        writer.write((0, codehandler_1.generateSource)(ast));
    }, { overwrite: true });
    (0, utils_1.logVerbose)('Generated Client TS file: ');
    (0, utils_1.logVerbose)(sourceFile.getText());
    const relativePath = path.relative('.', destinationDir);
    (0, utils_1.logDetail)(`Generating Syft client (${pkg.json.version}) to ${relativePath}`);
    if (process.env.NODE_ENV !== 'test') {
        const emitResult = project.emitSync();
        (0, utils_1.logVerbose)(`Emit results are: ${emitResult.getDiagnostics()} ${emitResult.getEmitSkipped()}`);
        if (!fs.existsSync(dir.getPath())) {
            (0, utils_1.logFatal)(`Failed to create the output directory at ${destinationDir}`);
            return;
        }
        (0, utils_1.logInfo)(`:sparkles: You can now start using Syft Client in your code.\nRefer: ${utils_1.SYFT_DOCUMENTATION}`);
        (0, utils_1.logDetail)(`
\`\`\`
import Syft, { AmplitudePlugin } from '@syftdata/client'
...
const syft = new Syft({
  appVersion: "1.0.0",
  plugins: [new AmplitudePlugin(amplitude)]
})
syft.pageViewed({name: 'index'});
\`\`\``);
    }
    else {
        (0, utils_1.logVerbose)('Skipping js generation to keep unit-tests faster');
    }
}
exports.generate = generate;
