/** RPA 业务错误分类（技术设计 §7.3 v0.6） */

export type RpaErrorCode =
  | "QUOTE_ENTRY_UNAVAILABLE"
  | "ADDRESS_SUGGESTION_NOT_FOUND"
  | "PAGE_LOAD_TIMEOUT"
  | "RPA_CAPTCHA"
  | "STRUCT_CHANGE"
  | "RPA_DATA_INVALID"
  | "SESSION_EXPIRED"
  | "PROVIDER_LOGIN_FAILED"
  | "CARRIER_NO_CAPACITY"
  | "ADDRESS_NOT_SUPPORTED"
  | "ADDRESS_NOT_CONFIRMED"
  | "CARGO_NOT_COMMITTED"
  | "CARGO_WEIGHT_MISMATCH"
  | "CARGO_RESET_FAILED";

export class RpaError extends Error {
  readonly code: RpaErrorCode;
  readonly retryable: boolean;
  readonly pauseWorker: boolean;

  constructor(
    code: RpaErrorCode,
    message: string,
    options?: { retryable?: boolean; pauseWorker?: boolean },
  ) {
    super(message);
    this.name = "RpaError";
    this.code = code;
    this.retryable = options?.retryable ?? true;
    this.pauseWorker = options?.pauseWorker ?? false;
  }
}

/** 不重试的错误码（含入口类错误 task-128） */
export const NO_RETRY_CODES: ReadonlySet<RpaErrorCode> = new Set([
  "QUOTE_ENTRY_UNAVAILABLE",
  "ADDRESS_SUGGESTION_NOT_FOUND",
  "STRUCT_CHANGE",
  "RPA_CAPTCHA",
  "PROVIDER_LOGIN_FAILED",
  "CARRIER_NO_CAPACITY",
  "ADDRESS_NOT_SUPPORTED",
  "ADDRESS_NOT_CONFIRMED",
  "RPA_DATA_INVALID",
]);
