// Clipboard monitoring class
export class monitorClipboard {
  constructor() {
    this.copyListeners = new Set();
    this.pasteListeners = new Set();
    this.suspiciousPatterns = {
      // Credit card patterns
      creditCard: {
        patterns: [
          /\b\d{4}[- ]?\d{4}[- ]?\d{4}[- ]?\d{4}\b/,  // Standard credit card
          /\b\d{3}[- ]?\d{4}[- ]?\d{4}[- ]?\d{4}\b/,  // Amex
          /\b\d{4}[- ]?\d{6}[- ]?\d{5}\b/             // Diners Club
        ]
      },
      // Social Security Number patterns
      ssn: {
        patterns: [
          /\b\d{3}[- ]?\d{2}[- ]?\d{4}\b/,            // Standard SSN
          /\b\d{9}\b/                                  // No separators
        ]
      },
      // Password patterns
      password: {
        patterns: [
          /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/,  // Strong password
          /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[A-Za-z\d]{8,}$/                         // Medium password
        ]
      },
      // API key patterns
      apiKey: {
        patterns: [
          /^[a-zA-Z0-9_-]{32,}$/,                     // Generic API key
          /^[a-zA-Z0-9_-]{40,}$/,                     // Longer API key
          /^[a-zA-Z0-9_-]{64,}$/                      // Very long API key
        ]
      },
      // Private key patterns
      privateKey: {
        patterns: [
          /^-----BEGIN (RSA|DSA|EC|OPENSSH) PRIVATE KEY-----/,  // Private key header
          /^-----BEGIN PGP PRIVATE KEY BLOCK-----/             // PGP private key
        ]
      }
    };

    this.initialize();
  }

  initialize() {
    // Monitor copy events
    document.addEventListener('copy', (event) => {
      const selection = window.getSelection();
      const text = selection.toString();
      
      if (text) {
        const analysis = this.analyzeContent(text);
        if (analysis.isSuspicious) {
          this.notifyCopyListeners({
            text,
            analysis,
            timestamp: Date.now()
          });
        }
      }
    });

    // Monitor paste events
    document.addEventListener('paste', (event) => {
      const text = event.clipboardData.getData('text');
      
      if (text) {
        const analysis = this.analyzeContent(text);
        if (analysis.isSuspicious) {
          this.notifyPasteListeners({
            text,
            analysis,
            timestamp: Date.now()
          });
        }
      }
    });
  }

  analyzeContent(text) {
    const findings = [];
    let riskScore = 0;

    // Check each pattern category
    for (const [category, { patterns }] of Object.entries(this.suspiciousPatterns)) {
      for (const pattern of patterns) {
        const match = text.match(pattern);
        if (match) {
          findings.push({
            category,
            pattern: pattern.toString(),
            match: match[0]
          });
          riskScore += this.getCategoryWeight(category);
        }
      }
    }

    return {
      isSuspicious: riskScore > 0,
      riskScore,
      findings,
      riskLevel: this.getRiskLevel(riskScore)
    };
  }

  getCategoryWeight(category) {
    const weights = {
      creditCard: 4,
      ssn: 4,
      password: 3,
      apiKey: 3,
      privateKey: 5
    };
    return weights[category] || 1;
  }

  getRiskLevel(riskScore) {
    if (riskScore >= 5) return 'high';
    if (riskScore >= 3) return 'medium';
    return 'low';
  }

  onCopy(callback) {
    this.copyListeners.add(callback);
  }

  onPaste(callback) {
    this.pasteListeners.add(callback);
  }

  removeCopyListener(callback) {
    this.copyListeners.delete(callback);
  }

  removePasteListener(callback) {
    this.pasteListeners.delete(callback);
  }

  notifyCopyListeners(data) {
    this.copyListeners.forEach(callback => callback(data));
  }

  notifyPasteListeners(data) {
    this.pasteListeners.forEach(callback => callback(data));
  }
} 