"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 tsconfig_exports = {};
__export(tsconfig_exports, {
  ensureTsconfigTemplates: () => ensureTsconfigTemplates,
  syncAllTsconfigs: () => syncAllTsconfigs,
  syncPackageTsconfigs: () => syncPackageTsconfigs
});
module.exports = __toCommonJS(tsconfig_exports);
var import_promises = require("node:fs/promises");
var import_node_path = require("node:path");
var import_package = require("../util/package.js");
/**
 * @license
 * Copyright 2022-2026 Greg Lauckhart <greg@lauckhart.com>
 * SPDX-License-Identifier: Apache-2.0
 */
const TEMPLATE_DIR = "tsc";
const TEMPLATES = ["tsconfig.base.json", "tsconfig.lib.json", "tsconfig.app.json", "tsconfig.test.json"];
const MANAGED_HEADER = "// Managed by nacho-build.  Nacho will update references automatically but otherwise preserves your edits.\n// Use `nacho-build configure` to overwrite with defaults.\n";
async function ensureTsconfigTemplates(workspace, force = false) {
  const srcDir = import_package.Package.tools.resolve(TEMPLATE_DIR);
  const destDir = workspace.resolve(TEMPLATE_DIR);
  await (0, import_promises.mkdir)(destDir, { recursive: true });
  for (const name of TEMPLATES) {
    const dest = (0, import_node_path.resolve)(destDir, name);
    if (force || !workspace.hasFile(dest)) {
      await (0, import_promises.copyFile)((0, import_node_path.resolve)(srcDir, name), dest);
    }
  }
}
async function syncAllTsconfigs(graph, force = false) {
  const workspace = graph.nodes[0].pkg.workspace;
  const rootTsconfig = await workspace.readJson("tsconfig.json");
  const originalReferences = rootTsconfig.references;
  rootTsconfig.references = [];
  for (const node of graph.nodes) {
    await syncPackageTsconfigs(graph, node, force);
    rootTsconfig.references.push({
      path: workspace.relative(node.pkg.path).replace(/\\/g, "/")
    });
  }
  if (referencesChanged(originalReferences, rootTsconfig.references)) {
    await workspace.writeJson("tsconfig.json", rootTsconfig);
  }
}
function referencesChanged(originalReferences, newReferences) {
  return JSON.stringify(originalReferences) !== JSON.stringify(newReferences);
}
async function syncPackageTsconfigs(_graph, node, force = false) {
  const workspace = node.pkg.workspace;
  const srcEmitsDeclarations = node.pkg.isLibrary || node.pkg.hasTests;
  await syncSubproject(
    workspace,
    node,
    "src",
    srcEmitsDeclarations ? "tsconfig.lib.json" : "tsconfig.app.json",
    force
  );
  await syncSubproject(
    workspace,
    node,
    "test",
    "tsconfig.test.json",
    force,
    node.pkg.resolve("src")
  );
}
async function syncSubproject(workspace, node, path, baseConfig, force, ...extraRefs) {
  path = node.pkg.resolve(path);
  const tsconfigPath = (0, import_node_path.resolve)(path, "tsconfig.json");
  if (!node.pkg.hasFile(tsconfigPath)) {
    return;
  }
  const tsconfig = await node.pkg.readJson(tsconfigPath);
  if (force) {
    tsconfig.extends = (0, import_node_path.relative)(path, workspace.resolve(TEMPLATE_DIR, baseConfig)).replace(/\\/g, "/");
  }
  const deps = node.dependencies.filter((dep) => dep.pkg.isLibrary).map((dep) => dep.pkg.resolve("src"));
  const desired = [.../* @__PURE__ */ new Set([...deps, ...extraRefs])];
  const newReferences = desired.map((ref) => ({ path: (0, import_node_path.relative)(path, ref).replace(/\\/g, "/") })).sort((ref1, ref2) => ref1.path.localeCompare(ref2.path));
  tsconfig.references = newReferences;
  await writeTsconfig(tsconfigPath, tsconfig);
}
async function writeTsconfig(path, value) {
  const content = MANAGED_HEADER + JSON.stringify(value, void 0, 4) + "\n";
  try {
    if (await (0, import_promises.readFile)(path, "utf-8") === content) {
      return;
    }
  } catch {
  }
  await (0, import_promises.writeFile)(path, content);
}
//# sourceMappingURL=tsconfig.js.map
