// src/actions/package/compile/packageCompileTscTypes.ts
import path from "path";
import { cwd as cwd2 } from "process";
import chalk from "chalk";
import { rollup } from "rollup";
import dts from "rollup-plugin-dts";
import nodeExternals from "rollup-plugin-node-externals";

// src/actions/package/compile/getCompilerOptions.ts
import { cwd } from "process";
import deepmerge from "deepmerge";
import { getTsconfig } from "get-tsconfig";
var getCompilerOptions = (options = {}, fileName = "tsconfig.json") => {
  const resolvedTsConfigCompilerOptions = getTsconfig(cwd(), fileName)?.config?.compilerOptions ?? {};
  return deepmerge(resolvedTsConfigCompilerOptions, options);
};

// src/actions/package/compile/packageCompileTscTypes.ts
var ignoredWarningCodes = /* @__PURE__ */ new Set(["EMPTY_BUNDLE", "UNRESOLVED_IMPORT"]);
async function bundleDts(inputPath, outputPath, platform, options, verbose = false) {
  const pkg = cwd2();
  const tsconfigPath = path.resolve(pkg, "tsconfig.json");
  const nodePlugIns = platform === "node" ? [nodeExternals()] : [];
  try {
    const bundle = await rollup({
      input: inputPath,
      plugins: [dts({
        ...options,
        tsconfig: tsconfigPath,
        compilerOptions: {
          emitDeclarationOnly: true,
          noEmit: false,
          declarationMap: true
        }
      }), ...nodePlugIns],
      onwarn(warning, warn) {
        if (ignoredWarningCodes.has(warning.code ?? "")) {
          return;
        }
        console.warn(chalk.yellow(`[${warning.code}] ${warning.message}`));
        console.warn(chalk.gray(inputPath));
        warn(warning);
      }
    });
    await bundle.write({
      file: outputPath,
      format: "es"
    });
  } catch (ex) {
    const error = ex;
    console.warn(chalk.red(error));
    console.warn(chalk.gray(inputPath));
  }
  if (verbose) {
    console.log(`Bundled declarations written to ${outputPath}`);
  }
}
var packageCompileTscTypes = async (entries, outDir, platform, srcDir = "build", verbose = false) => {
  if (verbose) {
    console.log(chalk.cyan(`Compiling Types START [${platform}]: ${entries.length} files to ${outDir} from ${srcDir}`));
    console.log(`Entries: ${entries.join(", ")}`);
  }
  const pkg = cwd2();
  const srcRoot = `${pkg}/${srcDir}/${platform}`;
  const entryNameToTypeName = (entry) => {
    const splitEntryName = entry.split(".");
    const newEntryExtension = "d." + splitEntryName.at(-1);
    return [...splitEntryName.slice(0, -1), newEntryExtension].join(".");
  };
  const compilerOptions = getCompilerOptions({
    removeComments: false,
    skipDefaultLibCheck: true,
    skipLibCheck: true,
    declarationMap: true,
    emitDeclarationOnly: true,
    noEmit: true
  });
  const entryNames = entries.map((entry) => entry.split(`${srcDir}/`).at(-1) ?? entry);
  await Promise.all(entryNames.map(async (entryName) => {
    const entryTypeName = entryNameToTypeName(entryName);
    await bundleDts(`${srcRoot}/${entryTypeName}`, `${outDir}/${entryTypeName}`, platform, { compilerOptions }, verbose);
  }));
  if (verbose) {
    console.log(chalk.cyan(`Compiling Types FINISH: ${entries.length} files to ${outDir} from ${srcDir}`));
  }
  return 0;
};
export {
  bundleDts,
  packageCompileTscTypes
};
//# sourceMappingURL=packageCompileTscTypes.mjs.map