"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AutoBeTestStatementProgrammer = void 0;
const typescript_1 = __importDefault(require("typescript"));
const FilePrinter_1 = require("../../utils/FilePrinter");
const writeTestExpression_1 = require("./writeTestExpression");
const writeTestStatement_1 = require("./writeTestStatement");
var AutoBeTestStatementProgrammer;
(function (AutoBeTestStatementProgrammer) {
    AutoBeTestStatementProgrammer.block = (ctx, stmt) => typescript_1.default.factory.createBlock(stmt.statements
        .map((child, i) => [
        ...(0, writeTestStatement_1.writeTestStatement)(ctx, child).filter((childStmt, j) => j === 0
            ? typescript_1.default.addSyntheticLeadingComment(childStmt, typescript_1.default.SyntaxKind.SingleLineCommentTrivia, JSON.stringify(child), true)
            : childStmt),
        ...(i !== 0 ? [FilePrinter_1.FilePrinter.newLine()] : []),
    ])
        .flat(), true);
    AutoBeTestStatementProgrammer.expressionStatement = (ctx, stmt) => typescript_1.default.factory.createExpressionStatement((0, writeTestExpression_1.writeTestExpression)(ctx, stmt.expression));
    // export const variableDeclaration = (
    //   ctx: IAutoBeTestProgrammerContext,
    //   stmt: AutoBeTest.IVariableDeclaration,
    // ): ts.VariableStatement => {
    //   const typeNode: ts.TypeNode = NestiaMigrateSchemaProgrammer.write({
    //     components: ctx.document.components,
    //     importer: ctx.importer,
    //     schema: OpenApiV3_1Emender.convertSchema(ctx.document.components)(
    //       stmt.schema,
    //     ),
    //   });
    //   return ts.factory.createVariableStatement(
    //     undefined,
    //     ts.factory.createVariableDeclarationList(
    //       [
    //         ts.factory.createVariableDeclaration(
    //           stmt.name,
    //           undefined,
    //           typeNode,
    //           undefined,
    //         ),
    //       ],
    //       stmt.mutability === "const" ? ts.NodeFlags.Constant : ts.NodeFlags.Let,
    //     ),
    //   );
    // };
    AutoBeTestStatementProgrammer.ifStatement = (ctx, stmt) => typescript_1.default.factory.createIfStatement((0, writeTestExpression_1.writeTestExpression)(ctx, stmt.condition), AutoBeTestStatementProgrammer.block(ctx, stmt.thenStatement), stmt.elseStatement
        ? stmt.elseStatement.type === "ifStatement"
            ? AutoBeTestStatementProgrammer.ifStatement(ctx, stmt.elseStatement)
            : AutoBeTestStatementProgrammer.block(ctx, stmt.elseStatement)
        : undefined);
    AutoBeTestStatementProgrammer.returnStatement = (ctx, stmt) => typescript_1.default.factory.createReturnStatement((0, writeTestExpression_1.writeTestExpression)(ctx, stmt.expression));
    AutoBeTestStatementProgrammer.throwStatement = (ctx, stmt) => typescript_1.default.factory.createThrowStatement((0, writeTestExpression_1.writeTestExpression)(ctx, stmt.expression));
    AutoBeTestStatementProgrammer.apiOperateStatement = (ctx, stmt) => {
        var _a;
        // find the function
        const func = ctx.endpoints.get(stmt.endpoint);
        if (!!((_a = stmt.variableName) === null || _a === void 0 ? void 0 : _a.length) && !!func.operation.responseBody)
            ctx.importer.dto(func.operation.responseBody.typeName);
        // make await function call expression
        const initializer = typescript_1.default.factory.createAwaitExpression(typescript_1.default.factory.createCallExpression(typescript_1.default.factory.createIdentifier(func.accessor), [], [
            typescript_1.default.factory.createIdentifier("connection"),
            ...(stmt.argument ? [(0, writeTestExpression_1.writeTestExpression)(ctx, stmt.argument)] : []),
        ]));
        if (stmt.variableName === null || stmt.variableName === undefined)
            return [typescript_1.default.factory.createExpressionStatement(initializer)];
        const variable = typescript_1.default.factory.createVariableStatement(undefined, typescript_1.default.factory.createVariableDeclarationList([
            typescript_1.default.factory.createVariableDeclaration(stmt.variableName, undefined, !!func.operation.responseBody
                ? typescript_1.default.factory.createTypeReferenceNode(func.operation.responseBody.typeName)
                : typescript_1.default.factory.createKeywordTypeNode(typescript_1.default.SyntaxKind.UndefinedKeyword), initializer),
        ], typescript_1.default.NodeFlags.Const));
        const assertion = typescript_1.default.factory.createCallExpression(typescript_1.default.factory.createPropertyAccessExpression(typescript_1.default.factory.createIdentifier(ctx.importer.external({
            type: "default",
            library: "typia",
            name: "typia",
        })), "assert"), undefined, [typescript_1.default.factory.createIdentifier(stmt.variableName)]);
        return [variable, typescript_1.default.factory.createExpressionStatement(assertion)];
    };
})(AutoBeTestStatementProgrammer || (exports.AutoBeTestStatementProgrammer = AutoBeTestStatementProgrammer = {}));
//# sourceMappingURL=AutoBeTestStatementProgrammer.js.map