/**
 * Node.js --require preload script
 *
 * Patches Module._resolveFilename so that ANY require('react-native') call
 * (including CJS requires from @testing-library/react-native's build files)
 * gets redirected to our compiled mock at react-native.cjs.
 *
 * This runs before vitest starts, so it intercepts at the earliest possible point.
 */
'use strict'

const Module = require('module')
const path = require('path')

const mockPath = path.resolve(__dirname, 'react-native.cjs')
const originalResolveFilename = Module._resolveFilename

Module._resolveFilename = function (request, parent, isMain, options) {
  // Intercept 'react-native' and any subpath like 'react-native/index'
  if (request === 'react-native' || request.startsWith('react-native/')) {
    return mockPath
  }
  return originalResolveFilename.call(this, request, parent, isMain, options)
}
