"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;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeMultilineJSIntoSingleLine = makeMultilineJSIntoSingleLine;
const babel = __importStar(require("@babel/core"));
function lineCommentToBlockComment() {
    const visitedComments = new Map();
    function turnCommentIntoBlock(original) {
        const existing = visitedComments.get(original);
        if (existing) {
            return existing;
        }
        const replacement = {
            ...original,
            type: 'CommentBlock',
        };
        visitedComments.set(original, replacement);
        return replacement;
    }
    function transformComments(path) {
        var _a, _b;
        const node = path.node;
        const keys = [
            'leadingComments',
            'trailingComments',
            'innerComments',
        ];
        for (const key of keys) {
            node[key] = (_b = (_a = node[key]) === null || _a === void 0 ? void 0 : _a.map(turnCommentIntoBlock)) !== null && _b !== void 0 ? _b : null;
        }
    }
    return {
        visitor: {
            Program(path) {
                transformComments(path);
                path.traverse({
                    enter(path) {
                        transformComments(path);
                    },
                });
            },
        },
    };
}
function multilineTemplateStringToSingleLine({ types: t, }) {
    return {
        visitor: {
            TemplateLiteral(path) {
                var _a, _b;
                if (!path.node.quasis.some(({ value }) => /[\r\n]/.exec(value.raw))) {
                    return;
                }
                if (path.parentPath.isTaggedTemplateExpression()) {
                    path.parentPath.replaceWith(t.callExpression(t.identifier('eval'), [
                        t.stringLiteral(this.file.code.slice((_a = path.parent.start) !== null && _a !== void 0 ? _a : undefined, (_b = path.parent.end) !== null && _b !== void 0 ? _b : undefined)),
                    ]));
                    return;
                }
                path.replaceWith(t.templateLiteral(path.node.quasis.map((el) => t.templateElement({
                    raw: el.value.raw.replace(/\n|\r\n?/g, '\\n'),
                }, el.tail)), path.node.expressions));
            },
        },
    };
}
function makeMultilineJSIntoSingleLine(src) {
    var _a, _b;
    let postASI;
    try {
        postASI =
            (_b = (_a = babel.transformSync(src, {
                retainLines: true,
                compact: false,
                code: true,
                comments: true,
                configFile: false,
                babelrc: false,
                browserslistConfigFile: false,
                plugins: [
                    lineCommentToBlockComment,
                    multilineTemplateStringToSingleLine,
                ],
                sourceType: 'script',
            })) === null || _a === void 0 ? void 0 : _a.code) !== null && _b !== void 0 ? _b : src;
    }
    catch (_c) {
        postASI = src;
    }
    const asSingleLine = postASI
        .split(/[\r\n]+/g)
        .map((line) => line.trim())
        .join(' ')
        .trim();
    return src.trim().endsWith(';')
        ? asSingleLine
        : asSingleLine.replace(/;$/, '');
}
//# sourceMappingURL=index.js.map