"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.handler = exports.builder = exports.desc = exports.command = void 0;
const path = require("path");
const fs = require("fs");
const utils_1 = require("@syftdata/common/lib/utils");
const codehandler_1 = require("@syftdata/codehandler");
const yaml_1 = require("yaml");
const client_1 = require("@syftdata/client");
exports.command = 'analyze';
exports.desc = 'Analyze source directory and extracts schema';
const builder = (y) => {
    return y
        .option('output', {
        describe: 'output folder to create events.yaml file.',
        type: 'string'
    })
        .option('srcDir', {
        describe: 'Directory to find project source code',
        type: 'string',
        default: '.'
    });
};
exports.builder = builder;
async function handler({ output, srcDir }) {
    const tsProject = (0, codehandler_1.readTSProject)(srcDir);
    if (tsProject == null) {
        return;
    }
    const project = tsProject.project;
    (0, utils_1.logInfo)('Analyzing the code to find usages..');
    const ast = (0, codehandler_1.analyzeAST)(project);
    const yaml = (0, yaml_1.stringify)(ast.eventSchemas, (k, v) => {
        if (k === 'eventType') {
            return client_1.SyftEventType[v];
        }
        if (k === 'zodType')
            return;
        if (k === 'type') {
            return v.name;
        }
        return v;
    }, 2);
    if (output == null) {
        (0, utils_1.logInfo)(yaml);
    }
    else {
        const yamlFile = path.join(output, 'events.yaml');
        fs.writeFileSync(yamlFile, yaml, 'utf-8');
    }
}
exports.handler = handler;
