"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var context_exports = {};
__export(context_exports, {
  TypescriptContext: () => TypescriptContext
});
module.exports = __toCommonJS(context_exports);
var import_node_path = require("node:path");
var import_typescript = require("typescript");
var import_error = require("../error.js");
/**
 * @license
 * Copyright 2022-2026 Greg Lauckhart <greg@lauckhart.com>
 * SPDX-License-Identifier: Apache-2.0
 */
var TypescriptContext;
((TypescriptContext2) => {
  function diagnose(diagnostics) {
    if (diagnostics === void 0) {
      return;
    }
    if (Array.isArray(diagnostics)) {
      if (!diagnostics.length) {
        return;
      }
    } else {
      diagnostics = [diagnostics];
    }
    const host = (0, import_typescript.createCompilerHost)({});
    let formatted = (0, import_typescript.formatDiagnosticsWithColorAndContext)(diagnostics, host);
    formatted = formatted.replace(/\u001b\[96m/g, "\n\x1B[96m");
    throw new import_error.BuildError(formatted);
  }
  TypescriptContext2.diagnose = diagnose;
  function compilerOptionsFor(path, ignoreErrors = false) {
    const file = (0, import_typescript.readConfigFile)(path, import_typescript.sys.readFile);
    diagnose(file.error);
    const config = (0, import_typescript.parseJsonConfigFileContent)(file.config, import_typescript.sys, (0, import_node_path.dirname)(path));
    if (!ignoreErrors) {
      diagnose(config.errors);
    }
    return config.options;
  }
  TypescriptContext2.compilerOptionsFor = compilerOptionsFor;
  function instrumentHostForSpinner(host, refreshCallback) {
    if (!refreshCallback) {
      return;
    }
    for (const name of ["readFile", "writeFile", "getSourceFile"]) {
      if (!(name in host)) {
        continue;
      }
      const fn = host[name];
      if (typeof fn !== "function") {
        continue;
      }
      host[name] = (...args) => {
        const result = fn.apply(host, args);
        refreshCallback();
        return result;
      };
    }
  }
  TypescriptContext2.instrumentHostForSpinner = instrumentHostForSpinner;
})(TypescriptContext || (TypescriptContext = {}));
//# sourceMappingURL=context.js.map
