import a from './index5.mjs';
import { x as K } from './main.mjs';
import { i18n } from './index2.mjs';
import '../shared/ccjk.BAGoDD49.mjs';
import 'module';
import 'node:child_process';
import 'node:path';
import 'node:process';
import 'node:stream';
import 'node:readline';
import 'node:fs';
import 'node:url';
import '../shared/ccjk.bQ7Dh1g4.mjs';

async function autoFix(results, options = {}) {
  const isZh = i18n.language === "zh-CN";
  const fixable = results.filter((r) => r.status !== "pass" && r.command);
  const report = { attempted: 0, succeeded: 0, failed: 0, results: [] };
  if (fixable.length === 0)
    return report;
  const { default: inquirer } = await import('./index6.mjs').then(function (n) { return n.c; });
  for (const result of fixable) {
    const cmd = result.command;
    console.log("");
    console.log(a.yellow(`  [!] ${result.name}: ${result.message}`));
    if (result.fix) {
      console.log(a.dim(`      ${result.fix}`));
    }
    console.log(a.cyan(`      ${isZh ? "\u4FEE\u590D\u547D\u4EE4" : "Fix command"}: ${cmd}`));
    if (options.dryRun) {
      console.log(a.dim(`      [dry-run] ${isZh ? "\u8DF3\u8FC7\u6267\u884C" : "skipped"}`));
      continue;
    }
    let shouldRun = options.autoApprove ?? false;
    if (!shouldRun) {
      const { confirm } = await inquirer.prompt([{
        type: "confirm",
        name: "confirm",
        message: isZh ? "\u662F\u5426\u6267\u884C\u4FEE\u590D\uFF1F" : "Run fix?",
        default: true
      }]);
      shouldRun = confirm;
    }
    if (!shouldRun)
      continue;
    report.attempted++;
    const start = Date.now();
    try {
      const [bin, ...args] = cmd.split(" ");
      const proc = await K(bin, args, { throwOnError: false });
      const durationMs = Date.now() - start;
      const exitCode = proc.exitCode ?? 0;
      const fixResult = {
        checkName: result.name,
        commandExecuted: cmd,
        exitCode,
        stdout: proc.stdout || "",
        stderr: proc.stderr || "",
        durationMs
      };
      report.results.push(fixResult);
      if (exitCode === 0) {
        report.succeeded++;
        console.log(a.green(`      \u2714 ${isZh ? "\u4FEE\u590D\u6210\u529F" : "Fixed"} (${durationMs}ms)`));
      } else {
        report.failed++;
        console.log(a.red(`      \u2716 ${isZh ? "\u4FEE\u590D\u5931\u8D25" : "Failed"} (exit ${exitCode})`));
        if (proc.stderr) {
          console.log(a.dim(`        ${proc.stderr.trim().split("\n")[0]}`));
        }
      }
    } catch (err) {
      report.failed++;
      const durationMs = Date.now() - start;
      report.results.push({
        checkName: result.name,
        commandExecuted: cmd,
        exitCode: 1,
        stdout: "",
        stderr: String(err),
        durationMs
      });
      console.log(a.red(`      \u2716 ${isZh ? "\u6267\u884C\u51FA\u9519" : "Error"}: ${String(err)}`));
    }
  }
  if (report.attempted > 0) {
    console.log("");
    console.log(a.bold(
      `  ${isZh ? "\u4FEE\u590D\u7ED3\u679C" : "Fix summary"}: ${report.succeeded}/${report.attempted} ${isZh ? "\u6210\u529F" : "succeeded"}`
    ));
  }
  return report;
}

export { autoFix };
