import { readFileSync, existsSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
import { exec as q } from './main.mjs';
import { r as resolveClaudeFamilySettingsTarget, n as normalizeClaudeFamilySettings } from '../shared/ccjk.DDL-4C-k.mjs';
import { ensureDir, writeFileAtomic } from './fs-operations.mjs';
import { m as mergeAndCleanPermissions } from './config.mjs';
import { a as getPlatform } from './platform.mjs';
import { j as join, d as dirname } from '../shared/ccjk.bQ7Dh1g4.mjs';
import 'module';
import 'node:child_process';
import 'node:path';
import 'node:process';
import 'node:stream';
import 'node:readline';
import './constants.mjs';
import 'node:os';
import './index2.mjs';
import './ccjk-config.mjs';
import '../shared/ccjk.BBtCGd_g.mjs';
import './index3.mjs';
import './json-config.mjs';
import '../shared/ccjk.RyizuzOI.mjs';
import '../shared/ccjk.BAGoDD49.mjs';
import 'node:crypto';
import 'node:fs/promises';
import './index5.mjs';
import './index6.mjs';
import 'stream';
import 'node:tty';
import 'node:async_hooks';
import '../shared/ccjk.Cjgrln_h.mjs';
import 'node:util';
import 'tty';
import 'fs';
import 'child_process';
import 'buffer';
import 'string_decoder';
import './claude-config.mjs';

const __dirname = dirname(fileURLToPath(import.meta.url));
function getTemplateSettings() {
  const templatePath = join(__dirname, "../../templates/claude-code/common/settings.json");
  const content = readFileSync(templatePath, "utf-8");
  return JSON.parse(content);
}
function loadCurrentSettings(target = resolveClaudeFamilySettingsTarget()) {
  if (!existsSync(target.settingsFile)) {
    return {};
  }
  try {
    const content = readFileSync(target.settingsFile, "utf-8");
    return JSON.parse(content);
  } catch {
    return {};
  }
}
function saveSettings(settings, target = resolveClaudeFamilySettingsTarget()) {
  ensureDir(target.configDir);
  normalizeClaudeFamilySettings(settings);
  writeFileAtomic(target.settingsFile, JSON.stringify(settings, null, 2));
}
async function importRecommendedEnv() {
  const target = resolveClaudeFamilySettingsTarget();
  const templateSettings = getTemplateSettings();
  const currentSettings = loadCurrentSettings(target);
  currentSettings.env = {
    ...currentSettings.env,
    ...templateSettings.env
  };
  saveSettings(currentSettings, target);
}
async function importRecommendedPermissions() {
  const target = resolveClaudeFamilySettingsTarget();
  const templateSettings = getTemplateSettings();
  const currentSettings = loadCurrentSettings(target);
  if (templateSettings.permissions && templateSettings.permissions.allow) {
    currentSettings.permissions = {
      ...templateSettings.permissions,
      allow: mergeAndCleanPermissions(
        templateSettings.permissions.allow,
        currentSettings.permissions?.allow
      )
    };
  } else {
    currentSettings.permissions = templateSettings.permissions;
  }
  saveSettings(currentSettings, target);
}
async function openSettingsJson() {
  const target = resolveClaudeFamilySettingsTarget();
  ensureDir(target.configDir);
  if (!existsSync(target.settingsFile)) {
    saveSettings({}, target);
  }
  const platform = getPlatform();
  let command;
  switch (platform) {
    case "macos":
      command = "open";
      break;
    case "windows":
      command = "start";
      break;
    default:
      command = "xdg-open";
  }
  try {
    await q(command, [target.settingsFile]);
  } catch {
    try {
      await q("code", [target.settingsFile]);
    } catch {
      try {
        await q("vim", [target.settingsFile]);
      } catch {
        await q("nano", [target.settingsFile]);
      }
    }
  }
}

export { importRecommendedEnv, importRecommendedPermissions, openSettingsJson };
