// src/actions/package/compile/packageCompileTsc.ts
import { cwd as cwd2 } from "process";
import chalk from "chalk";
import { createProgramFromConfig } from "tsc-prog";
import ts, {
  DiagnosticCategory,
  formatDiagnosticsWithColorAndContext,
  getPreEmitDiagnostics,
  sys
} from "typescript";

// 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/packageCompileTsc.ts
var packageCompileTsc = (platform, entries, srcDir = "src", outDir = "dist", compilerOptionsParam, verbose = false) => {
  const pkg = cwd2();
  if (verbose) {
    console.log(chalk.cyan(`Validating code START: ${entries.length} files to ${outDir} from ${srcDir}`));
  }
  const configFilePath = ts.findConfigFile(
    pkg,
    (fileName) => ts.sys.fileExists(fileName),
    "tsconfig.json"
  );
  if (configFilePath === void 0) {
    throw new Error("Could not find tsconfig.json");
  }
  const compilerOptions = {
    ...getCompilerOptions({
      removeComments: false,
      skipDefaultLibCheck: true,
      skipLibCheck: true,
      sourceMap: false
    }),
    ...compilerOptionsParam,
    outDir: `${outDir}/${platform}`,
    emitDeclarationOnly: true,
    noEmit: false
  };
  console.log(chalk.cyan(`Validating Files: ${entries.length}`));
  if (verbose) {
    for (const entry of entries) {
      console.log(chalk.grey(`Validating: ${entry}`));
    }
  }
  try {
    if (entries.length > 0) {
      const program = createProgramFromConfig({
        configFilePath,
        basePath: pkg,
        compilerOptions,
        files: entries.map((entry) => `${srcDir}/${entry}`),
        include: [`${srcDir}/**/*.*`]
      });
      const diagnostics = getPreEmitDiagnostics(program);
      if (diagnostics.length > 0) {
        const formattedDiagnostics = formatDiagnosticsWithColorAndContext(
          diagnostics,
          {
            getCanonicalFileName: (fileName) => fileName,
            getCurrentDirectory: () => srcDir,
            getNewLine: () => sys.newLine
          }
        );
        console.error(formattedDiagnostics);
      }
      const nonEmitPatterns = [".stories.", "/stories/", "/stories.", ".spec.", "/spec/", "/spec.", ".example."];
      program.emit(void 0, (fileName, text, writeByteOrderMark) => {
        if (nonEmitPatterns.some((pattern) => fileName.includes(pattern))) {
          return;
        }
        ts.sys.writeFile(fileName, text, writeByteOrderMark);
      });
      return diagnostics.reduce((acc, diag) => acc + (diag.category === DiagnosticCategory.Error ? 1 : 0), 0);
    }
    return 0;
  } finally {
    if (verbose) {
      console.log(chalk.cyan(`Validating code FINISH: ${entries.length} files to ${outDir} from ${srcDir}`));
    }
  }
};
export {
  packageCompileTsc
};
//# sourceMappingURL=packageCompileTsc.mjs.map