/**
 * Pacifica平台检测器
 *
 * 基于Ed25519私钥格式检测Pacifica平台
 */

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

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

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

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

    // 即使没有声明类型，也可以基于privateKey格式推断
    if (this.hasProperty(credentials, 'privateKey') && !this.hasProperty(credentials, 'apiKey')) {
      if (this.isValidPacificaPrivateKey(credentials.privateKey)) {
        // 没有apiKey/secretKey，且privateKey符合Ed25519格式，很可能是Pacifica
        return Platform.PACIFICA;
      }
    }

    return null;
  }

  /**
   * 验证是否为有效的Pacifica私钥格式
   */
  private isValidPacificaPrivateKey(privateKey: any): boolean {
    if (!this.isNonEmptyString(privateKey)) {
      return false;
    }

    // Ed25519私钥：64字符十六进制字符串
    const ed25519Pattern = /^[0-9a-fA-F]{64}$/;
    return this.matchesPattern(privateKey, ed25519Pattern);
  }
}