import { i18n } from '../chunks/index2.mjs';

var CloudErrorCode = /* @__PURE__ */ ((CloudErrorCode2) => {
  CloudErrorCode2["AUTH_ERROR"] = "AUTH_ERROR";
  CloudErrorCode2["RATE_LIMIT"] = "RATE_LIMIT";
  CloudErrorCode2["SCHEMA_MISMATCH"] = "SCHEMA_MISMATCH";
  CloudErrorCode2["NETWORK_ERROR"] = "NETWORK_ERROR";
  CloudErrorCode2["TIMEOUT"] = "TIMEOUT";
  CloudErrorCode2["SERVER_ERROR"] = "SERVER_ERROR";
  CloudErrorCode2["NOT_FOUND"] = "NOT_FOUND";
  CloudErrorCode2["API_ERROR"] = "API_ERROR";
  CloudErrorCode2["VALIDATION_ERROR"] = "VALIDATION_ERROR";
  CloudErrorCode2["UNKNOWN_ERROR"] = "UNKNOWN_ERROR";
  return CloudErrorCode2;
})(CloudErrorCode || {});
class CloudError extends Error {
  /** Error code */
  code;
  /** HTTP status code if applicable */
  statusCode;
  /** Original error */
  originalError;
  /** Additional context */
  context;
  /** Retry attempt number */
  retryAttempt;
  /** Request ID */
  requestId;
  /** Whether this error is retryable */
  isRetryable;
  constructor(code, message, metadata) {
    super(message);
    this.name = "CloudError";
    this.code = code;
    this.statusCode = metadata?.statusCode;
    this.originalError = metadata?.originalError;
    this.context = metadata?.context;
    this.retryAttempt = metadata?.retryAttempt;
    this.requestId = metadata?.requestId;
    this.isRetryable = isRetryableErrorCode(code);
    if (Error.captureStackTrace) {
      Error.captureStackTrace(this, CloudError);
    }
  }
  /**
   * Get localized error message
   */
  getLocalizedMessage(language) {
    const lang = language || i18n.language;
    return i18n.t(`cloud:errors.${this.code}`, { lng: lang }) || this.message;
  }
  /**
   * Get user-friendly error message with context
   */
  getUserMessage(language) {
    const localizedMsg = this.getLocalizedMessage(language);
    if (this.statusCode) {
      return `${localizedMsg} (HTTP ${this.statusCode})`;
    }
    return localizedMsg;
  }
  /**
   * Convert to JSON for logging
   */
  toJSON() {
    return {
      name: this.name,
      code: this.code,
      message: this.message,
      statusCode: this.statusCode,
      isRetryable: this.isRetryable,
      retryAttempt: this.retryAttempt,
      requestId: this.requestId,
      context: this.context,
      stack: this.stack
    };
  }
}
const CloudErrorFactory = {
  /**
   * Create error from HTTP response
   */
  fromHttpStatus(statusCode, message, metadata) {
    let code;
    if (statusCode === 401 || statusCode === 403) {
      code = "AUTH_ERROR" /* AUTH_ERROR */;
    } else if (statusCode === 404) {
      code = "NOT_FOUND" /* NOT_FOUND */;
    } else if (statusCode === 429) {
      code = "RATE_LIMIT" /* RATE_LIMIT */;
    } else if (statusCode >= 500) {
      code = "SERVER_ERROR" /* SERVER_ERROR */;
    } else if (statusCode >= 400) {
      code = "API_ERROR" /* API_ERROR */;
    } else {
      code = "UNKNOWN_ERROR" /* UNKNOWN_ERROR */;
    }
    return new CloudError(code, message, { ...metadata, statusCode });
  },
  /**
   * Create authentication error
   */
  auth(message, metadata) {
    return new CloudError(
      "AUTH_ERROR" /* AUTH_ERROR */,
      message || "Authentication failed",
      { ...metadata, statusCode: metadata?.statusCode || 401 }
    );
  },
  /**
   * Create rate limit error
   */
  rateLimit(message, metadata) {
    return new CloudError(
      "RATE_LIMIT" /* RATE_LIMIT */,
      message || "Rate limit exceeded",
      { ...metadata, statusCode: metadata?.statusCode || 429 }
    );
  },
  /**
   * Create schema mismatch error
   */
  schemaMismatch(message, metadata) {
    return new CloudError(
      "SCHEMA_MISMATCH" /* SCHEMA_MISMATCH */,
      message || "Response schema validation failed",
      metadata
    );
  },
  /**
   * Create network error
   */
  network(error, metadata) {
    const message = error instanceof Error ? error.message : "Network connection failed";
    return new CloudError(
      "NETWORK_ERROR" /* NETWORK_ERROR */,
      message,
      { ...metadata, originalError: error }
    );
  },
  /**
   * Create timeout error
   */
  timeout(timeoutMs, metadata) {
    return new CloudError(
      "TIMEOUT" /* TIMEOUT */,
      `Request timeout after ${timeoutMs}ms`,
      metadata
    );
  },
  /**
   * Create server error
   */
  server(message, metadata) {
    return new CloudError(
      "SERVER_ERROR" /* SERVER_ERROR */,
      message || "Server error occurred",
      { ...metadata, statusCode: metadata?.statusCode || 500 }
    );
  },
  /**
   * Create not found error
   */
  notFound(resource, metadata) {
    const message = resource ? `Resource not found: ${resource}` : "Resource not found";
    return new CloudError(
      "NOT_FOUND" /* NOT_FOUND */,
      message,
      { ...metadata, statusCode: metadata?.statusCode || 404 }
    );
  },
  /**
   * Create validation error
   */
  validation(message, metadata) {
    return new CloudError(
      "VALIDATION_ERROR" /* VALIDATION_ERROR */,
      message,
      { ...metadata, statusCode: metadata?.statusCode || 400 }
    );
  },
  /**
   * Create unknown error
   */
  unknown(error, metadata) {
    const message = error instanceof Error ? error.message : "An unknown error occurred";
    return new CloudError(
      "UNKNOWN_ERROR" /* UNKNOWN_ERROR */,
      message,
      { ...metadata, originalError: error }
    );
  }
};
function isRetryableErrorCode(code) {
  return [
    "NETWORK_ERROR" /* NETWORK_ERROR */,
    "TIMEOUT" /* TIMEOUT */,
    "SERVER_ERROR" /* SERVER_ERROR */,
    "RATE_LIMIT" /* RATE_LIMIT */
  ].includes(code);
}
function isRetryableError(error) {
  if (error instanceof CloudError) {
    return error.isRetryable;
  }
  if (error instanceof Error) {
    const message = error.message.toLowerCase();
    return message.includes("network") || message.includes("connection") || message.includes("timeout") || message.includes("econnrefused") || message.includes("enotfound") || message.includes("etimedout");
  }
  return false;
}
function isAuthError(error) {
  if (error instanceof CloudError) {
    return error.code === "AUTH_ERROR" /* AUTH_ERROR */;
  }
  return false;
}
function isRateLimitError(error) {
  if (error instanceof CloudError) {
    return error.code === "RATE_LIMIT" /* RATE_LIMIT */;
  }
  return false;
}
function handleCloudError(error, context, _language) {
  if (error instanceof CloudError) {
    return error;
  }
  let cloudError;
  if (error instanceof Error) {
    const message = error.message.toLowerCase();
    if (message.includes("timeout") || message.includes("timed out")) {
      cloudError = CloudErrorFactory.timeout(1e4, { originalError: error });
    } else if (message.includes("econnrefused") || message.includes("enotfound") || message.includes("network")) {
      cloudError = CloudErrorFactory.network(error);
    } else if (message.includes("401") || message.includes("unauthorized")) {
      cloudError = CloudErrorFactory.auth(error.message, { originalError: error });
    } else if (message.includes("429") || message.includes("rate limit")) {
      cloudError = CloudErrorFactory.rateLimit(error.message, { originalError: error });
    } else if (message.includes("404") || message.includes("not found")) {
      cloudError = CloudErrorFactory.notFound(void 0, { originalError: error });
    } else {
      cloudError = CloudErrorFactory.unknown(error);
    }
  } else {
    cloudError = CloudErrorFactory.unknown(error);
  }
  if (context && !cloudError.context) {
    return new CloudError(
      cloudError.code,
      cloudError.message,
      {
        statusCode: cloudError.statusCode,
        originalError: cloudError.originalError,
        context: { operation: context },
        retryAttempt: cloudError.retryAttempt,
        requestId: cloudError.requestId
      }
    );
  } else if (context && cloudError.context) {
    return new CloudError(
      cloudError.code,
      cloudError.message,
      {
        statusCode: cloudError.statusCode,
        originalError: cloudError.originalError,
        context: { ...cloudError.context, operation: context },
        retryAttempt: cloudError.retryAttempt,
        requestId: cloudError.requestId
      }
    );
  }
  return cloudError;
}
function getRetryDelay(error, attempt, config) {
  const initialDelay = config?.initialDelay || 100;
  const multiplier = config?.multiplier || 2;
  const maxDelay = config?.maxDelay || 1e4;
  if (error.code === "RATE_LIMIT" /* RATE_LIMIT */) {
    return Math.min(initialDelay * 10 * multiplier ** attempt, maxDelay);
  }
  return Math.min(initialDelay * multiplier ** attempt, maxDelay);
}
function formatErrorForLogging(error) {
  if (error instanceof CloudError) {
    return error.toJSON();
  }
  if (error instanceof Error) {
    return {
      name: error.name,
      message: error.message,
      stack: error.stack
    };
  }
  return {
    error: String(error)
  };
}

export { CloudErrorFactory as C, CloudError as a, CloudErrorCode as b, isRateLimitError as c, isRetryableError as d, isRetryableErrorCode as e, formatErrorForLogging as f, getRetryDelay as g, handleCloudError as h, isAuthError as i };
