import { getAddress, isAddress } from 'ethers';

/**
 * EVM 地址值对象
 */
export class EvmAddress {
  private readonly _value: string;

  private constructor(value: string) {
    this._value = value;
  }

  static create(value: string): EvmAddress {
    if (!isAddress(value)) {
      throw new Error(`Invalid EVM address: ${value}`);
    }
    // 使用 checksum 格式
    const checksumAddress = getAddress(value);
    return new EvmAddress(checksumAddress);
  }

  static fromUnchecked(value: string): EvmAddress {
    return new EvmAddress(value.toLowerCase());
  }

  get value(): string {
    return this._value;
  }

  get lowercase(): string {
    return this._value.toLowerCase();
  }

  equals(other: EvmAddress): boolean {
    return this._value.toLowerCase() === other._value.toLowerCase();
  }

  toString(): string {
    return this._value;
  }
}
