"use strict";
// ---------------------------------------------------------------------------
// esbuild Plugin for SimpleSteps Transformer
//
// Wraps the TypeScript transformer for use with esbuild builds.
// Uses esbuild's onLoad hook to transform files before bundling.
// ---------------------------------------------------------------------------
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.simpleStepsEsbuildPlugin = simpleStepsEsbuildPlugin;
const typescript_1 = __importDefault(require("typescript"));
const fs_1 = __importDefault(require("fs"));
const index_js_1 = require("../index.js");
/**
 * esbuild plugin that applies the SimpleSteps TypeScript transformer.
 *
 * @example
 * ```ts
 * import { simpleStepsEsbuildPlugin } from '@simplesteps/core/transformer/plugins/esbuild';
 *
 * await esbuild.build({
 *   entryPoints: ['src/index.ts'],
 *   plugins: [simpleStepsEsbuildPlugin()],
 * });
 * ```
 */
function simpleStepsEsbuildPlugin(options = {}) {
    const tsconfigPath = options.tsconfig ?? './tsconfig.json';
    let program;
    return {
        name: 'simplesteps-transformer',
        setup(build) {
            build.onLoad({ filter: /\.tsx?$/ }, (args) => {
                const code = fs_1.default.readFileSync(args.path, 'utf-8');
                // Quick check: skip files that don't reference SimpleStepsStateMachine
                if (!code.includes('SimpleStepsStateMachine'))
                    return null;
                // Build or reuse the program
                if (!program) {
                    const configFile = typescript_1.default.readConfigFile(tsconfigPath, typescript_1.default.sys.readFile);
                    if (configFile.error)
                        return null;
                    const parsed = typescript_1.default.parseJsonConfigFileContent(configFile.config, typescript_1.default.sys, process.cwd());
                    program = typescript_1.default.createProgram(parsed.fileNames, parsed.options);
                }
                // Apply the transformer
                const sourceFile = program.getSourceFile(args.path);
                if (!sourceFile)
                    return null;
                const transformer = (0, index_js_1.createSimpleStepsTransformer)(program);
                const result = typescript_1.default.transform(sourceFile, [transformer]);
                const transformedFile = result.transformed[0];
                const printer = typescript_1.default.createPrinter({ newLine: typescript_1.default.NewLineKind.LineFeed });
                const output = printer.printFile(transformedFile);
                result.dispose();
                // Only return if the code actually changed
                if (output === code)
                    return null;
                return {
                    contents: output,
                    loader: args.path.endsWith('.tsx') ? 'tsx' : 'ts',
                };
            });
        },
    };
}
exports.default = simpleStepsEsbuildPlugin;
//# sourceMappingURL=esbuild.js.map