/* eslint-disable @typescript-eslint/no-require-imports -- Jest setup runs in Node with CommonJS */
require('@testing-library/jest-dom')

process.env.NEXT_PUBLIC_BACK_END_DOMAIN =
  process.env.NEXT_PUBLIC_BACK_END_DOMAIN || 'http://localhost/api/v1'
process.env.NEXT_PUBLIC_TIME_OUT_API =
  process.env.NEXT_PUBLIC_TIME_OUT_API || '60000'

if (
  (typeof globalThis.crypto === 'undefined' ||
    typeof globalThis.crypto.randomUUID !== 'function') &&
  typeof require('crypto').randomUUID === 'function'
) {
  const nodeCrypto = require('crypto')
  globalThis.crypto = globalThis.crypto || {}
  globalThis.crypto.randomUUID = nodeCrypto.randomUUID.bind(nodeCrypto)
}

const mockLocalStorage = {
  getItem: jest.fn(),
  setItem: jest.fn(),
  removeItem: jest.fn(),
  clear: jest.fn(),
}
global.localStorage = mockLocalStorage
if (typeof window !== 'undefined') {
  window.localStorage = mockLocalStorage
}

class MockResizeObserver {
  observe = jest.fn()
  unobserve = jest.fn()
  disconnect = jest.fn()
}

class MockBroadcastChannel {
  postMessage = jest.fn()
  close = jest.fn()
  onmessage = null
}

global.ResizeObserver = global.ResizeObserver || MockResizeObserver
global.BroadcastChannel = global.BroadcastChannel || MockBroadcastChannel

if (typeof window !== 'undefined') {
  window.ResizeObserver = window.ResizeObserver || MockResizeObserver
  window.BroadcastChannel = window.BroadcastChannel || MockBroadcastChannel
}

if (!Element.prototype.scrollIntoView) {
  Element.prototype.scrollIntoView = jest.fn()
}
