'use strict';

var chunkAF3E2D32_cjs = require('./chunk-AF3E2D32.cjs');
var readFile = require('@stryke/fs/read-file');
var path = require('@stryke/path');
var defu = require('defu');
var ts = require('typescript');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var defu__default = /*#__PURE__*/_interopDefault(defu);
var ts__default = /*#__PURE__*/_interopDefault(ts);

function getTsconfigFilePath(context) {
  const tsconfigFilePath = context.options.tsconfig || path.joinPaths(context.options.projectRoot, "tsconfig.json");
  if (!path.existsSync(tsconfigFilePath)) {
    throw new Error(`Cannot find the \`tsconfig.json\` configuration file at ${tsconfigFilePath}`);
  }
  return tsconfigFilePath;
}
chunkAF3E2D32_cjs.__name(getTsconfigFilePath, "getTsconfigFilePath");
async function getParsedTypeScriptConfig(context) {
  const tsconfigFilePath = getTsconfigFilePath(context);
  let tsconfigJson = await readFile.readJsonFile(tsconfigFilePath);
  if (context.tsconfigRaw) {
    tsconfigJson = defu__default.default(tsconfigJson, context.tsconfigRaw);
  }
  const tsconfig = ts__default.default.parseJsonConfigFileContent(tsconfigJson, ts__default.default.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
  };
}
chunkAF3E2D32_cjs.__name(getParsedTypeScriptConfig, "getParsedTypeScriptConfig");
async function getTsconfigChanges(context) {
  const tsconfig = await getParsedTypeScriptConfig(context);
  const tsconfigJson = await readFile.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__default.default.ModuleKind.ESNext) {
    tsconfigJson.compilerOptions.module = "ESNext";
  }
  if (!tsconfig.options.target || ![
    ts__default.default.ScriptTarget.ESNext,
    ts__default.default.ScriptTarget.ES2024,
    ts__default.default.ScriptTarget.ES2023,
    ts__default.default.ScriptTarget.ES2022,
    ts__default.default.ScriptTarget.ES2021
  ].includes(tsconfig.options.target)) {
    tsconfigJson.compilerOptions.target = "ESNext";
  }
  if (tsconfig.options.moduleResolution !== ts__default.default.ModuleResolutionKind.Bundler) {
    tsconfigJson.compilerOptions.moduleResolution = "Bundler";
  }
  if (tsconfig.options.moduleDetection !== ts__default.default.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__default.default.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;
}
chunkAF3E2D32_cjs.__name(getTsconfigChanges, "getTsconfigChanges");

exports.getParsedTypeScriptConfig = getParsedTypeScriptConfig;
exports.getTsconfigChanges = getTsconfigChanges;
exports.getTsconfigFilePath = getTsconfigFilePath;
//# sourceMappingURL=chunk-4JZ27NWL.cjs.map
//# sourceMappingURL=chunk-4JZ27NWL.cjs.map