// src/lib/initCwd.ts
function INIT_CWD() {
  return process.env.INIT_CWD ?? process.cwd();
}

// src/lib/reporting/runWithEnvelope.ts
import { performance as performance2 } from "node:perf_hooks";

// src/lib/reporting/xyReporter.ts
import { performance } from "node:perf_hooks";
var jsonMode = false;
var originalStdoutWrite;
var envelopeEmitted = false;
var currentCommand = "unknown";
var startTime = 0;
function isJsonMode() {
  return jsonMode;
}
function setCurrentCommand(name) {
  currentCommand = name;
  startTime = performance.now();
  envelopeEmitted = false;
}
function emitJson(envelope) {
  if (envelopeEmitted) return;
  envelopeEmitted = true;
  const json = JSON.stringify(envelope) + "\n";
  if (originalStdoutWrite) {
    originalStdoutWrite(json);
  } else {
    process.stdout.write(json);
  }
}

// src/lib/reporting/runWithEnvelope.ts
async function runWithEnvelope(command, action) {
  setCurrentCommand(command);
  const start = performance2.now();
  let exitCode;
  let envelope;
  try {
    const ret = await action();
    if (typeof ret === "number") {
      exitCode = ret;
    } else {
      exitCode = ret.exitCode;
      envelope = ret.envelope;
    }
  } catch (ex) {
    exitCode = 1;
    const message = ex instanceof Error ? ex.message : String(ex);
    envelope = {
      errorCount: 1,
      errors: [{ message }]
    };
    if (!isJsonMode()) {
      console.error(message);
    }
  }
  if (isJsonMode()) {
    emitJson({
      command,
      data: envelope?.data,
      durationMs: Math.round(performance2.now() - start),
      errorCount: envelope?.errorCount ?? (exitCode === 0 ? 0 : 1),
      errors: envelope?.errors,
      exitCode,
      schemaVersion: 1,
      success: exitCode === 0,
      warningCount: envelope?.warningCount ?? 0,
      warnings: envelope?.warnings
    });
  }
  return exitCode;
}
async function withEnvelope(command, action) {
  process.exitCode = await runWithEnvelope(command, action);
}

// src/actions/claude-settings.ts
import {
  existsSync,
  mkdirSync,
  writeFileSync
} from "node:fs";
import PATH from "node:path";
import { createInterface } from "node:readline";
import chalk from "chalk";
var DENY_LIST = [
  "Bash(git push --force *)",
  "Bash(git reset --hard *)",
  "Bash(rm -rf /*)"
];
var RESTRICTIVE_ALLOW = [
  "Bash(git *)",
  "Bash(yarn *)",
  "Bash(pnpm *)",
  "Bash(npx *)",
  "Bash(ls *)",
  "Bash(mkdir *)",
  "Bash(cp *)",
  "Bash(mv *)",
  "Bash(rm *)",
  "Bash(cat *)",
  "Bash(head *)",
  "Bash(tail *)",
  "Bash(echo *)",
  "Bash(pwd)",
  "Bash(which *)",
  "Bash(grep *)",
  "Bash(find *)",
  "Bash(wc *)",
  "Bash(npm view *)",
  "Bash(gh *)",
  "Read",
  "Edit",
  "Write",
  "Glob",
  "Grep",
  "Skill"
];
var PERMISSIVE_ALLOW = [
  ...RESTRICTIVE_ALLOW,
  "Bash(node *)",
  "Bash(python3 *)",
  "Bash(claude *)",
  "Bash(sed *)",
  "Bash(xargs *)",
  "Bash(tee *)"
];
function buildSettings(level) {
  return {
    permissions: {
      allow: level === "restrictive" ? RESTRICTIVE_ALLOW : PERMISSIVE_ALLOW,
      deny: DENY_LIST
    }
  };
}
function askConfirmation(question) {
  const rl = createInterface({ input: process.stdin, output: process.stdout });
  return new Promise((resolve) => {
    rl.question(question, (answer) => {
      rl.close();
      resolve(answer.toLowerCase() === "y" || answer.toLowerCase() === "yes");
    });
  });
}
async function claudeSettings({ level = "permissive" } = {}) {
  const cwd = INIT_CWD();
  const claudeDir = PATH.resolve(cwd, ".claude");
  const settingsPath = PATH.resolve(claudeDir, "settings.local.json");
  mkdirSync(claudeDir, { recursive: true });
  if (existsSync(settingsPath)) {
    const confirmed = await askConfirmation(
      chalk.yellow(`${settingsPath} already exists. Replace it? (y/N) `)
    );
    if (!confirmed) {
      console.log(chalk.gray("Skipped \u2014 existing settings.local.json preserved"));
      return 0;
    }
  }
  const settings = buildSettings(level);
  writeFileSync(settingsPath, `${JSON.stringify(settings, null, 2)}
`, "utf8");
  console.log(chalk.green(`Generated .claude/settings.local.json (${level})`));
  return 0;
}

// src/xy/common/claude/settingsCommand.ts
var settingsCommand = {
  builder: (yargs) => {
    return yargs.option("level", {
      choices: ["restrictive", "permissive"],
      default: "permissive",
      description: "Permission level (restrictive omits node, python3, claude, sed, xargs, tee)",
      type: "string"
    });
  },
  command: "settings",
  describe: "Initialize .claude/settings.local.json with XY Labs defaults",
  handler: async (argv) => {
    if (argv.verbose) console.log("Claude Settings");
    await withEnvelope("claude settings", () => claudeSettings({ level: argv.level }));
  }
};
export {
  settingsCommand
};
//# sourceMappingURL=settingsCommand.mjs.map
