/**
 * Binance平台检测器
 *
 * 基于API密钥对格式检测Binance平台
 */

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

export class BinanceDetector extends BaseDetector {
  public readonly confidence = 0.95; // 最高置信度，因为API密钥对格式很明确

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

    // 检查是否声明为Binance类型
    if (this.hasProperty(credentials, 'type') && credentials.type === 'binance') {
      // 验证API密钥对格式
      if (this.isValidBinanceCredentials(credentials)) {
        return Platform.BINANCE;
      }
    }

    // 基于API密钥对格式推断
    if (this.isValidBinanceCredentials(credentials)) {
      return Platform.BINANCE;
    }

    return null;
  }

  /**
   * 验证是否为有效的Binance凭证格式
   */
  private isValidBinanceCredentials(credentials: any): boolean {
    // 必须同时有apiKey和secretKey
    if (!this.hasProperty(credentials, 'apiKey') || !this.hasProperty(credentials, 'secretKey')) {
      return false;
    }

    // 验证apiKey格式
    if (!this.isNonEmptyString(credentials.apiKey)) {
      return false;
    }

    // 验证secretKey格式
    if (!this.isNonEmptyString(credentials.secretKey)) {
      return false;
    }

    // 确保不是私钥格式（排除其他平台）
    if (this.hasProperty(credentials, 'privateKey')) {
      return false;
    }

    return true;
  }
}