// WebCrypto API polyfill for Baileys
import crypto from 'crypto-browserify';

// Provide crypto as a global for Baileys
if (!global.crypto) {
  (global as any).crypto = crypto;
}

// Node.js 18+ has crypto but might be missing subtle
if (!global.crypto.subtle) {
  // Basic subtle crypto implementation for essential operations
  (global.crypto as any).subtle = {
    // Implement basic methods needed by Baileys
    digest: async (algorithm: string, data: Uint8Array) => {
      const hash = crypto.createHash(algorithm.toLowerCase().replace('-', ''));
      hash.update(Buffer.from(data));
      return Promise.resolve(hash.digest());
    }
  };
}

console.log('WebCrypto API polyfill loaded');
