"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.writeTestFunction = writeTestFunction;
const utils_1 = require("@autobe/utils");
const NestiaMigrateImportProgrammer_1 = require("@nestia/migrate/lib/programmers/NestiaMigrateImportProgrammer");
const openapi_1 = require("@samchon/openapi");
const tstl_1 = require("tstl");
const typescript_1 = __importDefault(require("typescript"));
const FilePrinter_1 = require("../../utils/FilePrinter");
const AutoBeTestStatementProgrammer_1 = require("./AutoBeTestStatementProgrammer");
function writeTestFunction(props) {
    const ctx = {
        importer: new NestiaMigrateImportProgrammer_1.NestiaMigrateImportProgrammer(),
        document: props.document,
        endpoints: associate(props.document),
    };
    const decla = typescript_1.default.factory.createFunctionDeclaration([
        typescript_1.default.factory.createModifier(typescript_1.default.SyntaxKind.ExportKeyword),
        typescript_1.default.factory.createModifier(typescript_1.default.SyntaxKind.AsyncKeyword),
    ], undefined, props.scenario.functionName, undefined, [
        typescript_1.default.factory.createParameterDeclaration(undefined, undefined, "connection", undefined, typescript_1.default.factory.createTypeReferenceNode(`${ctx.importer.external({
            type: "default",
            name: "api",
            library: `@ORGANIZATION/PROJECT-api`,
        })}.IConnection`)),
    ], undefined, AutoBeTestStatementProgrammer_1.AutoBeTestStatementProgrammer.block(ctx, {
        type: "block",
        statements: props.function.statements,
    }));
    return FilePrinter_1.FilePrinter.write({
        statements: [
            ...ctx.importer.toStatements((key) => `@ORGANIZATION/PROJECT-api/lib/structures/${key}`),
            FilePrinter_1.FilePrinter.newLine(),
            FilePrinter_1.FilePrinter.description(decla, props.scenario.draft),
        ],
    });
}
function associate(document) {
    // associate operations and functions
    const operations = new tstl_1.HashMap(document.operations.map((o) => new tstl_1.Pair({
        method: o.method,
        path: o.path,
    }, o)), utils_1.AutoBeEndpointComparator.hashCode, utils_1.AutoBeEndpointComparator.equals);
    const functions = new tstl_1.HashMap(utils_1.AutoBeEndpointComparator.hashCode, utils_1.AutoBeEndpointComparator.equals);
    // from migrate application
    const migrate = openapi_1.HttpMigration.application((0, utils_1.transformOpenApiDocument)(document));
    for (const route of migrate.routes) {
        const endpoint = {
            path: route.path,
            method: route.method,
        };
        functions.emplace(endpoint, {
            accessor: "api.functional." + route.accessor.join("."),
            operation: operations.get(endpoint),
        });
    }
    return functions;
}
//# sourceMappingURL=writeTestFunction.js.map