// ../core/dist/error-handler.js
import path from "path";
var UNKNOWN_ERROR_MESSAGE = "Unknown error";
function hasMessage(value) {
  return typeof value === "object" && value !== null && "message" in value && typeof value.message === "string";
}
var ProcessExitError = class _ProcessExitError extends Error {
  /** Process exit code to use when caught at CLI boundary */
  exitCode;
  /**
   * Create a process exit error
   * @param {string} message - Error message (already formatted by die() or caller)
   * @param {number} [exitCode=1] - Process exit code
   */
  constructor(message, exitCode = 1) {
    super(message);
    this.name = "ProcessExitError";
    this.exitCode = exitCode;
    if (Error.captureStackTrace) {
      Error.captureStackTrace(this, _ProcessExitError);
    }
  }
};
var WUError = class _WUError extends Error {
  /** Error code (e.g., 'WU_NOT_FOUND') */
  code;
  /** Additional error context */
  details;
  /** Suggested commands or actions to try (optional) */
  tryNext;
  /** Alias for details for agent-friendly access */
  context;
  /**
   * Create a WU error
   * @param {string} code - Error code (e.g., 'WU_NOT_FOUND')
   * @param {string} message - Human-readable error message
   * @param {object} [details={}] - Additional error context
   */
  constructor(code, message, details = {}) {
    super(message);
    this.name = "WUError";
    this.code = code;
    this.details = details;
    if (Error.captureStackTrace) {
      Error.captureStackTrace(this, _WUError);
    }
  }
};
function toError(error, fallbackMessage = UNKNOWN_ERROR_MESSAGE) {
  if (error instanceof Error) {
    return error;
  }
  if (typeof error === "string" && error.length > 0) {
    return new Error(error);
  }
  if (hasMessage(error)) {
    return new Error(error.message);
  }
  return new Error(fallbackMessage);
}
function getErrorMessage(error, fallbackMessage = UNKNOWN_ERROR_MESSAGE) {
  return toError(error, fallbackMessage).message;
}
function die(message, exitCode = 1) {
  const scriptPath = process.argv[1] || "unknown";
  const scriptName = path.basename(scriptPath, ".js");
  const formattedMessage = `[${scriptName}] ${message}`;
  console.error(formattedMessage);
  throw new ProcessExitError(formattedMessage, exitCode);
}
function createError(code, message, details = {}) {
  return new WUError(code, message, details);
}
var ErrorCodes = {
  PROCESS_EXIT: "PROCESS_EXIT",
  // WU-1538: Typed exit signal from library code
  WU_NOT_FOUND: "WU_NOT_FOUND",
  WU_ALREADY_CLAIMED: "WU_ALREADY_CLAIMED",
  WU_NOT_CLAIMED: "WU_NOT_CLAIMED",
  INVALID_WU_ID: "INVALID_WU_ID",
  INVALID_LANE: "INVALID_LANE",
  GATES_FAILED: "GATES_FAILED",
  GIT_ERROR: "GIT_ERROR",
  VALIDATION_ERROR: "VALIDATION_ERROR",
  FILE_NOT_FOUND: "FILE_NOT_FOUND",
  YAML_PARSE_ERROR: "YAML_PARSE_ERROR",
  PARSE_ERROR: "PARSE_ERROR",
  WORKTREE_ERROR: "WORKTREE_ERROR",
  BRANCH_ERROR: "BRANCH_ERROR",
  STATE_ERROR: "STATE_ERROR",
  SECTION_NOT_FOUND: "SECTION_NOT_FOUND",
  INTERNAL_ERROR: "INTERNAL_ERROR",
  RECOVERY_ERROR: "RECOVERY_ERROR",
  TRANSACTION_ERROR: "TRANSACTION_ERROR",
  // WU-1369: Atomic transaction errors
  LOCK_ERROR: "LOCK_ERROR",
  // WU-1747: Merge lock errors
  // Initiative system error codes (WU-1247)
  INIT_NOT_FOUND: "INIT_NOT_FOUND",
  INIT_ALREADY_EXISTS: "INIT_ALREADY_EXISTS",
  INVALID_INIT_ID: "INVALID_INIT_ID",
  INVALID_SLUG: "INVALID_SLUG",
  INVALID_PHASE: "INVALID_PHASE",
  DEPENDENCY_CYCLE: "DEPENDENCY_CYCLE",
  // WU-2116: Structured error codes for migrated throw new Error() sites
  MERGE_EXHAUSTION: "MERGE_EXHAUSTION",
  REBASE_CONFLICT: "REBASE_CONFLICT",
  DELEGATION_NOT_FOUND: "DELEGATION_NOT_FOUND",
  DELEGATION_ALREADY_ESCALATED: "DELEGATION_ALREADY_ESCALATED",
  SIGNAL_UNAVAILABLE: "SIGNAL_UNAVAILABLE",
  ID_GENERATION_FAILED: "ID_GENERATION_FAILED",
  SCOPE_VIOLATION: "SCOPE_VIOLATION",
  TOOL_ALREADY_REGISTERED: "TOOL_ALREADY_REGISTERED",
  INVALID_ARGUMENT: "INVALID_ARGUMENT",
  DEPRECATED_API: "DEPRECATED_API",
  CONFIG_ERROR: "CONFIG_ERROR",
  TEMPLATE_VALIDATION_ERROR: "TEMPLATE_VALIDATION_ERROR",
  INVARIANT_ERROR: "INVARIANT_ERROR",
  RETRY_EXHAUSTION: "RETRY_EXHAUSTION",
  PREFLIGHT_ERROR: "PREFLIGHT_ERROR",
  INVALID_DURATION: "INVALID_DURATION",
  // WU-2118: Structured error codes for memory/agent/mcp package migrations
  NODE_NOT_FOUND: "NODE_NOT_FOUND",
  SESSION_ERROR: "SESSION_ERROR",
  ALREADY_EXISTS: "ALREADY_EXISTS",
  // WU-2117: Structured error codes for CLI throw new Error() migration
  COMMAND_EXECUTION_FAILED: "COMMAND_EXECUTION_FAILED",
  PACK_ALREADY_EXISTS: "PACK_ALREADY_EXISTS",
  CANCELLED_BY_USER: "CANCELLED_BY_USER",
  REGISTRY_UPLOAD_FAILED: "REGISTRY_UPLOAD_FAILED",
  WORKSPACE_NOT_FOUND: "WORKSPACE_NOT_FOUND",
  WORKSPACE_MALFORMED: "WORKSPACE_MALFORMED",
  ORPHAN_WU_ERROR: "ORPHAN_WU_ERROR",
  REPAIR_FAILED: "REPAIR_FAILED",
  ONBOARD_FAILED: "ONBOARD_FAILED",
  DIRECTORY_NOT_EMPTY: "DIRECTORY_NOT_EMPTY",
  AMBIGUOUS_MATCH: "AMBIGUOUS_MATCH",
  STRING_NOT_FOUND: "STRING_NOT_FOUND",
  FILE_SIZE_EXCEEDED: "FILE_SIZE_EXCEEDED",
  PARENT_DIR_NOT_FOUND: "PARENT_DIR_NOT_FOUND"
};

export {
  ProcessExitError,
  getErrorMessage,
  die,
  createError,
  ErrorCodes
};
