"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BabelTransform = void 0;
/* eslint-disable no-new-func */
const core_1 = require("@babel/core");
function BabelTransform(code, options = {}) {
    var _a;
    const { IIFE = false } = options;
    try {
        const simpleWrapped = `(\n${code || 'undefined'}\n)`;
        const { code: expression } = (0, core_1.transformSync)(IIFE ? `(()=>${simpleWrapped})()` : simpleWrapped, {
            cwd: __dirname,
            sourceType: 'script',
            comments: false,
            // minified: true,
            presets: [
                [
                    '@babel/preset-env',
                    {
                        targets: {
                            /**
                             * 支持 proxy 的版本
                             * 低于这个版本完全无法支持
                             */
                            // browsers: ['chrome >= 49', 'iOS >= 10', 'safari >= 10'],
                            /**
                             * 一个相对较高的版本，包涵解构赋值，这样不用注入太多 polifill
                             * 但是不包涵 options chain
                             * 基本上是 es2019
                             */
                            browsers: ['chrome >= 67', 'iOS >= 12', 'safari >= 12'],
                        },
                        include: [
                            // es2015
                            '@babel/plugin-transform-arrow-functions',
                        ],
                    },
                ],
            ],
        }) || {};
        const res = ((_a = expression === null || expression === void 0 ? void 0 : expression.replace) === null || _a === void 0 ? void 0 : _a.call(expression, /;$/, '')) || code;
        if (expression) {
            new Function(`return (\n${res}\n)`);
        }
        return res;
    }
    catch (e) {
        console.error(`transform [${code}] fail:`, e === null || e === void 0 ? void 0 : e.message);
        return code;
    }
}
exports.BabelTransform = BabelTransform;
