import { CLAVUE_CONFIG_FILE, CLAVUE_SETTINGS_FILE, CLAVUE_DIR, ClAUDE_CONFIG_FILE, SETTINGS_FILE, CLAUDE_DIR, isCodeToolType } from '../chunks/constants.mjs';
import { readZcfConfig } from '../chunks/ccjk-config.mjs';
import { j as join } from './ccjk.bQ7Dh1g4.mjs';

const TRUSTED_OPERATOR_ASK_RULES = [
  "Bash(git push:*)",
  "Bash(rm:*)",
  "Bash(rm -rf:*)",
  "Bash(git push --force:*)",
  "Bash(git reset --hard:*)",
  "Bash(npm publish:*)",
  "Bash(npm login:*)",
  "Bash(docker:*)",
  "Bash(kubectl:*)",
  "Bash(terraform apply:*)",
  "Bash(gh release:*)",
  "Bash(git tag:*)",
  "Bash(gh pr merge:*)"
];
function hasOwn(object, key) {
  return Object.prototype.hasOwnProperty.call(object, key);
}
function stringArray(value) {
  return Array.isArray(value) ? value.filter((item) => typeof item === "string") : [];
}
function mergeStringArray(existing, additions) {
  const merged = stringArray(existing);
  for (const item of additions) {
    if (!merged.includes(item)) {
      merged.push(item);
    }
  }
  return merged;
}
function isValidStatusLineConfig(value) {
  return Boolean(
    value && typeof value === "object" && !Array.isArray(value) && value.type === "command" && typeof value.command === "string"
  );
}
function hasInvalidStatusLineConfig(settings) {
  return Boolean(
    settings && typeof settings === "object" && !Array.isArray(settings) && hasOwn(settings, "statusLine") && !isValidStatusLineConfig(settings.statusLine)
  );
}
function normalizeClaudeFamilySettings(settings) {
  if (!settings || typeof settings !== "object" || Array.isArray(settings)) {
    return settings;
  }
  const mutableSettings = settings;
  if (hasOwn(mutableSettings, "statusLine")) {
    const statusLine = mutableSettings.statusLine;
    if (statusLine && typeof statusLine === "object" && !Array.isArray(statusLine) && typeof statusLine.command === "string") {
      mutableSettings.statusLine = {
        ...statusLine,
        type: "command"
      };
    } else {
      delete mutableSettings.statusLine;
    }
  }
  return settings;
}
function applyTrustedOperatorPermissions(settings) {
  if (!settings || typeof settings !== "object" || Array.isArray(settings)) {
    return settings;
  }
  const mutableSettings = settings;
  mutableSettings.permissions = mutableSettings.permissions && typeof mutableSettings.permissions === "object" && !Array.isArray(mutableSettings.permissions) ? mutableSettings.permissions : {};
  mutableSettings.permissions.defaultMode = "bypassPermissions";
  mutableSettings.permissions.trustedOperatorMode = true;
  mutableSettings.permissions.ask = mergeStringArray(mutableSettings.permissions.ask, TRUSTED_OPERATOR_ASK_RULES);
  return settings;
}

function resolveClaudeFamilySettingsTarget(codeTool) {
  const configuredTool = readZcfConfig()?.codeToolType;
  const resolvedTool = codeTool || (isCodeToolType(configuredTool) ? configuredTool : "claude-code");
  if (resolvedTool === "clavue") {
    return {
      codeTool: "clavue",
      configDir: CLAVUE_DIR,
      settingsFile: CLAVUE_SETTINGS_FILE,
      instructionsFile: join(CLAVUE_DIR, "clavue.md"),
      runtimeConfigFile: CLAVUE_CONFIG_FILE,
      runtimeBackupDirName: "backups",
      displayName: "Clavue"
    };
  }
  return {
    codeTool: "claude-code",
    configDir: CLAUDE_DIR,
    settingsFile: SETTINGS_FILE,
    instructionsFile: join(CLAUDE_DIR, "CLAUDE.md"),
    runtimeConfigFile: ClAUDE_CONFIG_FILE,
    runtimeBackupDirName: "backup",
    displayName: "Claude Code"
  };
}

export { applyTrustedOperatorPermissions as a, hasInvalidStatusLineConfig as h, normalizeClaudeFamilySettings as n, resolveClaudeFamilySettingsTarget as r };
