"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.AutoBeTestCompiler = void 0;
const utils_1 = require("@autobe/utils");
const embed_typescript_1 = require("embed-typescript");
const tstl_1 = require("tstl");
const typescript_1 = __importDefault(require("typescript"));
const transform_1 = __importDefault(require("typia/lib/transform"));
const AutoBeCompilerInterfaceTemplate_1 = require("../raw/AutoBeCompilerInterfaceTemplate");
const AutoBeCompilerTestTemplate_1 = require("../raw/AutoBeCompilerTestTemplate");
const test_json_1 = __importDefault(require("../raw/test.json"));
const FilePrinter_1 = require("../utils/FilePrinter");
const writeTestFunction_1 = require("./programmers/writeTestFunction");
class AutoBeTestCompiler {
    compile(props) {
        return __awaiter(this, void 0, void 0, function* () {
            var _a;
            const alias = (_a = props.package) !== null && _a !== void 0 ? _a : "@ORGANIZATION/PROJECT-api";
            const compiler = new embed_typescript_1.EmbedTypeScript({
                external: test_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_1.default)(program, {}, {
                            addDiagnostic: (input) => diagnostics.push(input),
                        }),
                    ],
                }),
            });
            const result = yield compiler.compile(props.files);
            return result.type === "success"
                ? { type: "success" }
                : result.type === "failure"
                    ? { type: "failure", diagnostics: result.diagnostics }
                    : { type: "exception", error: result.error };
        });
    }
    validate(props) {
        return __awaiter(this, void 0, void 0, function* () {
            const errors = [];
            const endpoints = new tstl_1.HashMap(props.document.operations.map((op) => new tstl_1.Pair({
                method: op.method,
                path: op.path,
            }, op)), utils_1.AutoBeEndpointComparator.hashCode, utils_1.AutoBeEndpointComparator.equals);
            (0, utils_1.validateTestFunction)({
                function: props.function,
                document: props.document,
                endpoints: endpoints,
                errors,
            });
            return errors.length !== 0 ? errors : null;
        });
    }
    write(props) {
        return __awaiter(this, void 0, void 0, function* () {
            const content = (0, writeTestFunction_1.writeTestFunction)(props);
            return props.prettier === false ? content : FilePrinter_1.FilePrinter.beautify(content);
        });
    }
    getExternal() {
        return __awaiter(this, void 0, void 0, function* () {
            return test_json_1.default;
        });
    }
    getTemplate() {
        return __awaiter(this, void 0, void 0, function* () {
            return Object.assign(Object.assign({}, AutoBeCompilerInterfaceTemplate_1.AutoBeCompilerInterfaceTemplate), AutoBeCompilerTestTemplate_1.AutoBeCompilerTestTemplate);
        });
    }
}
exports.AutoBeTestCompiler = AutoBeTestCompiler;
//# sourceMappingURL=AutoBeTestCompiler.js.map