"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AutoBeTypeScriptCompiler = void 0;
const transform_1 = __importDefault(require("@nestia/core/lib/transform"));
const embed_typescript_1 = require("embed-typescript");
const typescript_1 = __importDefault(require("typescript"));
const transform_2 = __importDefault(require("typia/lib/transform"));
const nestjs_json_1 = __importDefault(require("./raw/nestjs.json"));
const FilePrinter_1 = require("./utils/FilePrinter");
/**
 * Official TypeScript compiler for final code validation and quality assurance.
 *
 * This compiler provides the ultimate validation layer that ensures all
 * generated code meets production standards and integrates seamlessly with the
 * TypeScript ecosystem. While the AST-based approach eliminates most potential
 * errors before code generation, the AutoBeTypeScriptCompiler serves as the
 * final quality gate for perfect integration verification throughout the vibe
 * coding pipeline.
 *
 * The implementation validates framework integration with NestJS APIs, type
 * system integrity for complex relationships, dependency resolution across
 * modules, and build system compatibility with standard toolchains. It provides
 * comprehensive IDE support including intelligent autocomplete, real-time error
 * detection, sophisticated refactoring capabilities, and complete navigation
 * features.
 *
 * The compiler enables critical feedback loops necessary for AI self-correction
 * when implementation or test code contains compilation errors requiring
 * iterative refinement. This ensures that generated applications are
 * immediately deployable without manual debugging cycles while maintaining the
 * reliability of the automated development process.
 *
 * @author Samchon
 */
class AutoBeTypeScriptCompiler {
    compile(props) {
        return __awaiter(this, void 0, void 0, function* () {
            var _a, _b;
            const alias = (_a = props.package) !== null && _a !== void 0 ? _a : "@ORGANIZATION/PROJECT-api";
            const compiler = new embed_typescript_1.EmbedTypeScript({
                external: nestjs_json_1.default,
                compilerOptions: {
                    target: typescript_1.default.ScriptTarget.ESNext,
                    module: typescript_1.default.ModuleKind.CommonJS,
                    downlevelIteration: true,
                    baseUrl: "./",
                    paths: {
                        [alias]: ["./src/api"],
                        [`${alias}/lib/*`]: ["./src/api/*"],
                    },
                    strict: true,
                    skipLibCheck: true,
                    esModuleInterop: true,
                    experimentalDecorators: true,
                    emitDecoratorMetadata: true,
                },
                transformers: (program, diagnostics) => ({
                    before: [
                        (0, transform_2.default)(program, {}, {
                            addDiagnostic: (input) => diagnostics.push(input),
                        }),
                        (0, transform_1.default)(program, {}, {
                            addDiagnostic: (input) => diagnostics.push(input),
                        }),
                    ],
                }),
            });
            const result = yield compiler.compile(Object.assign(Object.assign({}, props.files), ((_b = props.prisma) !== null && _b !== void 0 ? _b : {})));
            return result.type === "success"
                ? { type: "success" }
                : result.type === "failure"
                    ? { type: "failure", diagnostics: result.diagnostics }
                    : { type: "exception", error: result.error };
        });
    }
    getExternal(location) {
        return __awaiter(this, void 0, void 0, function* () {
            var _a;
            return (_a = nestjs_json_1.default[location]) !== null && _a !== void 0 ? _a : undefined;
        });
    }
    beautify(script) {
        return FilePrinter_1.FilePrinter.beautify(script);
    }
}
exports.AutoBeTypeScriptCompiler = AutoBeTypeScriptCompiler;
//# sourceMappingURL=AutoBeTypeScriptCompiler.js.map