import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone';
setupZoneTestEnv();

Object.defineProperty(window, 'CSS', { value: null });
Object.defineProperty(window, 'getComputedStyle', {
  value: () => {
    return {
      display: 'none',
      appearance: ['-webkit-appearance'],
    };
  },
});

Object.defineProperty(document, 'doctype', {
  value: '<!DOCTYPE html>',
});
Object.defineProperty(document.body.style, 'transform', {
  value: () => {
    return {
      enumerable: true,
      configurable: true,
    };
  },
});

// Simule la fonction fetch pour éviter les erreurs dans les tests Jest
global.fetch = jest.fn(() =>
  Promise.resolve({
    ok: true,
    json: () => Promise.resolve({}),
    blob: () => Promise.resolve(new Blob()),
  })
) as jest.Mock;

// 🟡 Ignore les warnings pdfjs-dist
const originalWarn = console.warn;
console.warn = (...args) => {
  const message = typeof args[0] === 'string' ? args[0] : '';

  // Liste des warnings à ignorer
  const ignoredWarnings = [
    'ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING_FLAG',
    'loadPackages',
    'pdfjs-dist',
  ];

  if (ignoredWarnings.some((warning) => message.includes(warning))) {
    return;
  }

  originalWarn(...args);
};

// 🟡 Ignore les logs angularx-qrcode
const originalLog = console.log;
console.log = (...args) => {
  const message = typeof args[0] === 'string' ? args[0] : '';

  const ignoredLogs = ['Warning: loadPackages', 'ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING_FLAG'];

  if (ignoredLogs.some((log) => message.includes(log))) {
    return;
  }

  originalLog(...args);
};

// 🔴 Ignore les errors angularx-qrcode
const originalError = console.error;
console.error = (...args) => {
  const message = typeof args[0] === 'string' ? args[0] : '';

  if (message.includes('[angularx-qrcode]')) {
    return;
  }

  originalError(...args);
};
