"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const babel = __importStar(require("@babel/core"));
const runtime_support_out_nocov_1 = __importDefault(require("./runtime-support.out.nocov"));
const wrap_as_iife_1 = __importDefault(require("./stages/wrap-as-iife"));
const uncatchable_exceptions_1 = __importDefault(require("./stages/uncatchable-exceptions"));
const transform_maybe_await_1 = __importDefault(require("./stages/transform-maybe-await"));
const error_codes_1 = require("./error-codes");
class AsyncWriter {
    step(ast, originalSource, plugins = [], opts = {}) {
        const transform = (opts) => ast
            ? babel.transformFromAstSync(ast, originalSource, opts)
            : babel.transformSync(originalSource, opts);
        return transform({
            plugins,
            code: false,
            ast: true,
            cloneInputAst: false,
            configFile: false,
            babelrc: false,
            browserslistConfigFile: false,
            compact: originalSource.length > 10000,
            sourceType: 'script',
            ...opts,
        });
    }
    process(code) {
        var _a, _b, _c, _d;
        try {
            let ast = (_a = this.step(undefined, code, [
                require('@babel/plugin-transform-shorthand-properties').default,
                require('@babel/plugin-transform-parameters').default,
                require('@babel/plugin-transform-destructuring').default,
            ])) === null || _a === void 0 ? void 0 : _a.ast;
            ast = (_b = this.step(ast, code, [wrap_as_iife_1.default])) === null || _b === void 0 ? void 0 : _b.ast;
            ast = (_c = this.step(ast, code, [uncatchable_exceptions_1.default])) === null || _c === void 0 ? void 0 : _c.ast;
            return (_d = this.step(ast, code, [
                [
                    transform_maybe_await_1.default,
                    {
                        customErrorBuilder: babel.types.identifier('MongoshAsyncWriterError'),
                    },
                ],
            ], { code: true, ast: false })) === null || _d === void 0 ? void 0 : _d.code;
        }
        catch (e) {
            const { message } = e;
            delete e.message;
            e.message = message.replace('unknown: ', '');
            throw e;
        }
    }
    unprocessedRuntimeSupportCode() {
        return this.process(`
    class MongoshAsyncWriterError extends Error {
      constructor(message, codeIdentifier) {
        const code = (${JSON.stringify(error_codes_1.AsyncRewriterErrors)})[codeIdentifier];
        super(\`[\${code}] \${message}\`);
        this.code = code;
      }
    }`);
    }
    runtimeSupportCode() {
        return runtime_support_out_nocov_1.default;
    }
}
exports.default = AsyncWriter;
//# sourceMappingURL=async-writer-babel.js.map