import { __name } from './chunk-6AXVLTU5.js';
import { readJsonFile } from '@stryke/fs/read-file';
import { joinPaths, existsSync } from '@stryke/path';
import defu from 'defu';
import ts from 'typescript';

function getTsconfigFilePath(context) {
  const tsconfigFilePath = context.options.tsconfig || joinPaths(context.options.projectRoot, "tsconfig.json");
  if (!existsSync(tsconfigFilePath)) {
    throw new Error(`Cannot find the \`tsconfig.json\` configuration file at ${tsconfigFilePath}`);
  }
  return tsconfigFilePath;
}
__name(getTsconfigFilePath, "getTsconfigFilePath");
async function getParsedTypeScriptConfig(context) {
  const tsconfigFilePath = getTsconfigFilePath(context);
  let tsconfigJson = await readJsonFile(tsconfigFilePath);
  if (context.tsconfigRaw) {
    tsconfigJson = defu(tsconfigJson, context.tsconfigRaw);
  }
  const tsconfig = ts.parseJsonConfigFileContent(tsconfigJson, ts.sys, context.projectRoot);
  if (tsconfig.errors.length > 0) {
    const errorMessage = `Cannot parse the compiler options provided in the \`tsconfigRaw\` option. Please investigate the following issues:
${tsconfig.errors.map((error) => `- ${(error.category !== void 0 && error.code ? `[${error.category}-${error.code}]: ` : "") + error.messageText.toString()}`).join("\n")}
      `;
    throw new Error(errorMessage);
  }
  return {
    ...tsconfig,
    tsconfigJson
  };
}
__name(getParsedTypeScriptConfig, "getParsedTypeScriptConfig");
async function getTsconfigChanges(context) {
  const tsconfig = await getParsedTypeScriptConfig(context);
  const tsconfigJson = await readJsonFile(getTsconfigFilePath(context));
  tsconfigJson.compilerOptions ??= {};
  if (tsconfigJson.reflection !== true) {
    tsconfigJson.reflection = true;
  }
  if (tsconfig.options.experimentalDecorators !== true) {
    tsconfigJson.compilerOptions.experimentalDecorators = true;
  }
  if (tsconfig.options.emitDecoratorMetadata !== true) {
    tsconfigJson.compilerOptions.emitDecoratorMetadata = true;
  }
  if (!tsconfig.options.types?.some((type) => type.toLowerCase() === "@storm-stack/types")) {
    tsconfigJson.compilerOptions.types ??= [];
    tsconfigJson.compilerOptions.types.push("@storm-stack/types");
  }
  if (!tsconfig.options.lib?.some((lib) => lib.toLowerCase() !== "esnext" && lib.toLowerCase() !== "es2021" && lib.toLowerCase() !== "es2022" && lib.toLowerCase() !== "es2023")) {
    tsconfigJson.compilerOptions.lib ??= [];
    tsconfigJson.compilerOptions.lib.push("esnext");
  }
  if (tsconfig.options.module !== ts.ModuleKind.ESNext) {
    tsconfigJson.compilerOptions.module = "ESNext";
  }
  if (!tsconfig.options.target || ![
    ts.ScriptTarget.ESNext,
    ts.ScriptTarget.ES2024,
    ts.ScriptTarget.ES2023,
    ts.ScriptTarget.ES2022,
    ts.ScriptTarget.ES2021
  ].includes(tsconfig.options.target)) {
    tsconfigJson.compilerOptions.target = "ESNext";
  }
  if (tsconfig.options.moduleResolution !== ts.ModuleResolutionKind.Bundler) {
    tsconfigJson.compilerOptions.moduleResolution = "Bundler";
  }
  if (tsconfig.options.moduleDetection !== ts.ModuleDetectionKind.Force) {
    tsconfigJson.compilerOptions.moduleDetection = "force";
  }
  if (tsconfig.options.allowSyntheticDefaultImports !== true) {
    tsconfigJson.compilerOptions.allowSyntheticDefaultImports = true;
  }
  if (tsconfig.options.noImplicitOverride !== true) {
    tsconfigJson.compilerOptions.noImplicitOverride = true;
  }
  if (tsconfig.options.noUncheckedIndexedAccess !== true) {
    tsconfigJson.compilerOptions.noUncheckedIndexedAccess = true;
  }
  if (tsconfig.options.skipLibCheck !== true) {
    tsconfigJson.compilerOptions.skipLibCheck = true;
  }
  if (tsconfig.options.resolveJsonModule !== true) {
    tsconfigJson.compilerOptions.resolveJsonModule = true;
  }
  if (tsconfig.options.isolatedModules !== true) {
    tsconfigJson.compilerOptions.isolatedModules = true;
  }
  if (tsconfig.options.verbatimModuleSyntax !== false) {
    tsconfigJson.compilerOptions.verbatimModuleSyntax = false;
  }
  if (tsconfig.options.allowJs !== true) {
    tsconfigJson.compilerOptions.allowJs = true;
  }
  if (tsconfig.options.esModuleInterop !== true) {
    tsconfigJson.compilerOptions.esModuleInterop = true;
  }
  if (tsconfig.options.declaration !== true) {
    tsconfigJson.compilerOptions.declaration = true;
  }
  if (context.platform === "browser") {
    if (tsconfig.options.jsx !== ts.JsxEmit.ReactJSX) {
      tsconfigJson.compilerOptions.jsx = "react-jsx";
    }
    if (!tsconfig.options.lib?.some((lib) => lib.toLowerCase() !== "dom")) {
      tsconfigJson.compilerOptions.lib ??= [];
      tsconfigJson.compilerOptions.lib.push("dom");
    }
    if (!tsconfig.options.lib?.some((lib) => lib.toLowerCase() !== "dom.iterable")) {
      tsconfigJson.compilerOptions.lib ??= [];
      tsconfigJson.compilerOptions.lib.push("dom.iterable");
    }
  }
  return tsconfigJson;
}
__name(getTsconfigChanges, "getTsconfigChanges");

export { getParsedTypeScriptConfig, getTsconfigChanges, getTsconfigFilePath };
//# sourceMappingURL=chunk-VNY3ILRH.js.map
//# sourceMappingURL=chunk-VNY3ILRH.js.map