// tools/browser/errors.ts
var MorphError = class extends Error {
  /** Error code for programmatic handling */
  code;
  /** Original cause of the error, if any */
  cause;
  constructor(message, code, cause) {
    super(message);
    this.name = "MorphError";
    this.code = code;
    this.cause = cause;
    if (Error.captureStackTrace) {
      Error.captureStackTrace(this, this.constructor);
    }
  }
  /**
   * Returns a JSON representation of the error for logging.
   */
  toJSON() {
    return {
      name: this.name,
      message: this.message,
      code: this.code,
      cause: this.cause?.message
    };
  }
};
var MorphValidationError = class extends MorphError {
  /** The field that failed validation */
  field;
  constructor(message, field) {
    super(message, "validation_error");
    this.name = "MorphValidationError";
    this.field = field;
  }
  toJSON() {
    return {
      ...super.toJSON(),
      field: this.field
    };
  }
};
var MorphAPIError = class extends MorphError {
  /** HTTP status code */
  statusCode;
  /** Request ID for debugging (if available) */
  requestId;
  /** Raw response body */
  rawResponse;
  constructor(message, code, statusCode, options) {
    super(message, code, options?.cause);
    this.name = "MorphAPIError";
    this.statusCode = statusCode;
    this.requestId = options?.requestId;
    this.rawResponse = options?.rawResponse;
  }
  toJSON() {
    return {
      ...super.toJSON(),
      statusCode: this.statusCode,
      requestId: this.requestId
    };
  }
};
var MorphAuthenticationError = class extends MorphAPIError {
  constructor(message = "Authentication required. Please provide a valid API key.") {
    super(message, "authentication_required", 401);
    this.name = "MorphAuthenticationError";
  }
};
var MorphRateLimitError = class extends MorphAPIError {
  /** When the rate limit resets (Unix timestamp) */
  resetAt;
  /** Number of seconds until reset */
  retryAfter;
  constructor(message = "Rate limit exceeded. Please retry later.", options) {
    super(message, "rate_limit_exceeded", 429, { requestId: options?.requestId });
    this.name = "MorphRateLimitError";
    this.resetAt = options?.resetAt;
    this.retryAfter = options?.retryAfter;
  }
  toJSON() {
    return {
      ...super.toJSON(),
      resetAt: this.resetAt,
      retryAfter: this.retryAfter
    };
  }
};
var MorphNotFoundError = class extends MorphAPIError {
  /** The type of resource that was not found */
  resourceType;
  /** The ID of the resource that was not found */
  resourceId;
  constructor(resourceType, resourceId) {
    const message = resourceId ? `${resourceType} '${resourceId}' not found` : `${resourceType} not found`;
    super(message, "resource_not_found", 404);
    this.name = "MorphNotFoundError";
    this.resourceType = resourceType;
    this.resourceId = resourceId;
  }
  toJSON() {
    return {
      ...super.toJSON(),
      resourceType: this.resourceType,
      resourceId: this.resourceId
    };
  }
};
var MorphProfileLimitError = class extends MorphAPIError {
  /** Current number of profiles */
  currentCount;
  /** Maximum allowed profiles for the plan */
  maxAllowed;
  constructor(message = "Profile limit exceeded for your plan.", options) {
    super(message, "profile_limit_exceeded", 403, { requestId: options?.requestId });
    this.name = "MorphProfileLimitError";
    this.currentCount = options?.currentCount;
    this.maxAllowed = options?.maxAllowed;
  }
  toJSON() {
    return {
      ...super.toJSON(),
      currentCount: this.currentCount,
      maxAllowed: this.maxAllowed
    };
  }
};
function parseAPIError(statusCode, responseText, requestId) {
  let errorData = {};
  try {
    errorData = JSON.parse(responseText);
  } catch {
  }
  const message = errorData.detail || errorData.message || responseText || "Unknown error";
  const code = errorData.code;
  switch (statusCode) {
    case 401:
      return new MorphAuthenticationError(message);
    case 403:
      if (code === "profile_limit_exceeded" || message.toLowerCase().includes("limit")) {
        return new MorphProfileLimitError(message, { requestId });
      }
      return new MorphAPIError(message, "insufficient_permissions", statusCode, { requestId, rawResponse: responseText });
    case 404:
      if (message.toLowerCase().includes("profile")) {
        return new MorphNotFoundError("Profile", void 0);
      }
      if (message.toLowerCase().includes("session")) {
        return new MorphNotFoundError("Session", void 0);
      }
      return new MorphAPIError(message, "resource_not_found", statusCode, { requestId, rawResponse: responseText });
    case 429:
      return new MorphRateLimitError(message, { requestId });
    case 422:
      return new MorphAPIError(message, "validation_error", statusCode, { requestId, rawResponse: responseText });
    case 500:
    case 502:
    case 503:
    case 504:
      return new MorphAPIError(message, "service_unavailable", statusCode, { requestId, rawResponse: responseText });
    default:
      return new MorphAPIError(message, "network_error", statusCode, { requestId, rawResponse: responseText });
  }
}

export {
  MorphError,
  MorphValidationError,
  MorphAPIError,
  MorphAuthenticationError,
  MorphRateLimitError,
  MorphNotFoundError,
  MorphProfileLimitError,
  parseAPIError
};
//# sourceMappingURL=chunk-2VERUKO2.js.map