/**
 * Aster平台检测器
 *
 * 基于以太坊私钥格式检测Aster平台
 */

import { Platform } from '@/core/types';
import { BaseDetector } from '@/core/PlatformDetector';

export class AsterDetector extends BaseDetector {
  public readonly confidence = 0.9;

  public detect(credentials: any): Platform | null {
    if (!credentials || typeof credentials !== 'object') {
      return null;
    }

    // 检查是否声明为Aster类型
    if (this.hasProperty(credentials, 'type') && credentials.type === 'aster') {
      // 验证privateKey格式
      if (this.hasProperty(credentials, 'privateKey')) {
        if (this.isValidAsterPrivateKey(credentials.privateKey)) {
          return Platform.ASTER;
        }
      }
    }

    // 基于privateKey格式推断（以太坊格式）
    if (this.hasProperty(credentials, 'privateKey') && !this.hasProperty(credentials, 'apiKey')) {
      if (this.isValidAsterPrivateKey(credentials.privateKey)) {
        return Platform.ASTER;
      }
    }

    return null;
  }

  /**
   * 验证是否为有效的Aster私钥格式（以太坊格式）
   */
  private isValidAsterPrivateKey(privateKey: any): boolean {
    if (!this.isNonEmptyString(privateKey)) {
      return false;
    }

    // 以太坊私钥：0x前缀 + 64字符十六进制
    const ethPrivateKeyPattern = /^0x[0-9a-fA-F]{64}$/i;
    return this.matchesPattern(privateKey, ethPrivateKeyPattern);
  }
}