// src/actions/package/compileTargetsPublint.ts
import fs from "node:fs";
import path from "node:path";
import chalk from "chalk";
import ts from "typescript";
var COMPILE_TARGET_NAMES = /* @__PURE__ */ new Set(["browser", "neutral", "node"]);
var CONFIG_FILE_NAMES = [
  "xy.config.ts",
  "xy.config.mts",
  "xy.config.cts",
  "xy.config.js",
  "xy.config.mjs",
  "xy.config.cjs"
];
function emptyResult() {
  return {
    errors: 0,
    modified: false,
    warnings: 0
  };
}
function findConfigFile(pkgDir) {
  for (const name of CONFIG_FILE_NAMES) {
    const candidate = path.join(pkgDir, name);
    if (fs.existsSync(candidate)) return candidate;
  }
  return void 0;
}
function getPropertyName(prop) {
  if (!ts.isPropertyAssignment(prop)) return void 0;
  const { name } = prop;
  if (ts.isIdentifier(name) || ts.isStringLiteral(name)) return name.text;
  return void 0;
}
function collectEmptyTargetProps(sourceFile) {
  const found = [];
  function visit(node) {
    if (ts.isPropertyAssignment(node) && getPropertyName(node) === "compile" && ts.isObjectLiteralExpression(node.initializer)) {
      for (const prop of node.initializer.properties) {
        if (ts.isPropertyAssignment(prop) && COMPILE_TARGET_NAMES.has(getPropertyName(prop) ?? "") && ts.isObjectLiteralExpression(prop.initializer) && prop.initializer.properties.length === 0) {
          found.push(prop);
        }
      }
    }
    ts.forEachChild(node, visit);
  }
  visit(sourceFile);
  return found;
}
function isWhitespace(char) {
  return char !== void 0 && /\s/.test(char);
}
function removeProperties(text, props, sourceFile) {
  const sorted = props.toSorted((a, b) => b.getStart(sourceFile) - a.getStart(sourceFile));
  let result = text;
  for (const prop of sorted) {
    let start = prop.getFullStart();
    let end = prop.getEnd();
    let cursor = end;
    while (isWhitespace(result[cursor])) cursor++;
    if (result[cursor] === ",") {
      end = cursor + 1;
    } else {
      let back = prop.getStart(sourceFile) - 1;
      while (back >= 0 && isWhitespace(result[back])) back--;
      if (result[back] === ",") start = back;
    }
    result = result.slice(0, start) + result.slice(end);
  }
  return result;
}
function checkCompileTargets(pkgDir, shouldFix) {
  const result = emptyResult();
  const configPath = findConfigFile(pkgDir);
  if (configPath === void 0) return result;
  const text = fs.readFileSync(configPath, "utf8");
  const sourceFile = ts.createSourceFile(path.basename(configPath), text, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS);
  const emptyProps = collectEmptyTargetProps(sourceFile);
  if (emptyProps.length === 0) return result;
  const configName = path.basename(configPath);
  if (shouldFix) {
    fs.writeFileSync(configPath, removeProperties(text, emptyProps, sourceFile));
    for (const prop of emptyProps) {
      console.warn(chalk.yellow(`Publint [compileTargets]: removed deprecated empty "compile.${getPropertyName(prop)}" target from ${configName}`));
    }
    result.modified = true;
  } else {
    for (const prop of emptyProps) {
      console.warn(chalk.yellow(
        `Publint [compileTargets]: "compile.${getPropertyName(prop)} = {}" is treated as "do not build" \u2014 set it to \`false\` or remove it (use --shouldFix to remove) in ${configName}`
      ));
    }
  }
  result.warnings += emptyProps.length;
  return result;
}
export {
  checkCompileTargets
};
//# sourceMappingURL=compileTargetsPublint.mjs.map
