'use strict';

var chunkAF3E2D32_cjs = require('./chunk-AF3E2D32.cjs');
var esbuild = require('esbuild');

var DECLARATION_RE = /\.d\.[cm]?ts$/;
var CM_LETTER_RE = /(?<=\.)(?:c|m)(?=[jt]s$)/;
var KNOWN_EXT_RE = /\.(?:c|m)?[jt]sx?$/;
var TS_EXTS = /* @__PURE__ */ new Set([
  ".ts",
  ".mts",
  ".cts"
]);
var getUnbuildLoader = /* @__PURE__ */ chunkAF3E2D32_cjs.__name((context) => {
  return async (input, { options }) => {
    if (!KNOWN_EXT_RE.test(input.path) || DECLARATION_RE.test(input.path)) {
      return;
    }
    const output = [];
    let contents = await input.getContents();
    if (options.declaration && !input.srcPath?.match(DECLARATION_RE)) {
      const cm = input.srcPath?.match(CM_LETTER_RE)?.[0] || "";
      const extension2 = `.d.${cm}ts`;
      output.push({
        contents,
        srcPath: input.srcPath,
        path: input.path,
        extension: extension2,
        declaration: true
      });
    }
    if (TS_EXTS.has(input.extension)) {
      contents = await esbuild.transform(await context.compiler.compile(context, input.path, contents), {
        ...options.esbuild,
        loader: "ts"
      }).then((r) => r.code);
    } else if ([
      ".tsx",
      ".jsx"
    ].includes(input.extension)) {
      contents = await esbuild.transform(await context.compiler.compile(context, input.path, contents), {
        loader: input.extension === ".tsx" ? "tsx" : "jsx",
        ...options.esbuild
      }).then((r) => r.code);
    }
    const isCjs = options.format === "cjs";
    if (isCjs) {
      contents = context.resolver.transform({
        source: contents,
        retainLines: false
      }).replace(/^exports.default = /gm, "module.exports = ").replace(/^var _default = exports.default = /gm, "module.exports = ").replace("module.exports = void 0;", "");
    }
    let extension = isCjs ? ".js" : ".mjs";
    if (options.ext) {
      extension = options.ext.startsWith(".") ? options.ext : `.${options.ext}`;
    }
    output.push({
      contents,
      path: input.path,
      extension
    });
    return output;
  };
}, "getUnbuildLoader");

exports.getUnbuildLoader = getUnbuildLoader;
//# sourceMappingURL=chunk-LP4HYSF4.cjs.map
//# sourceMappingURL=chunk-LP4HYSF4.cjs.map