# Resolve react_native_pods.rb with node to allow for hoisting
require Pod::Executable.execute_command('node', ['-p',
  'require.resolve(
    "react-native/scripts/react_native_pods.rb",
    {paths: [process.argv[1]]},
  )', __dir__]).strip

# In the monorepo, the SDK podspec resolves react-native to its own devDependency.
# Point it to the sample app's react-native so it detects the correct version for build flags.
ENV['REACT_NATIVE_NODE_MODULES_DIR'] ||= File.expand_path(File.join(__dir__, '..', 'node_modules', 'react-native'))

platform :ios, min_ios_version_supported
prepare_react_native_project!

linkage = ENV['USE_FRAMEWORKS']
if linkage != nil
  Pod::UI.puts "Configuring Pod with #{linkage}ally linked Frameworks".green
  use_frameworks! :linkage => linkage.to_sym

  pre_install do |installer|
    installer.pod_targets.each do |pod|
      # RNScreens and RNReanimated don't work when dynamically linked
      # Fix from https://github.com/software-mansion/react-native-screens/issues/1963#issuecomment-1848246731
      if pod.name.eql?('RNReanimated') || pod.name.eql?('RNScreens')
        def pod.build_type;
          Pod::BuildType.static_library
        end
      end
    end
  end

end

target 'sentryreactnativesample' do
  config = use_native_modules!

  use_react_native!(
    :path => config[:reactNativePath],
    # An absolute path to your application root.
    :app_path => "#{Pod::Config.instance.installation_root}/.."
  )

  if ENV['RCT_NEW_ARCH_ENABLED'] == '1'
    pod 'AppTurboModules', :path => "./../tm"
  end

  target 'sentryreactnativesampleTests' do
    inherit! :complete
    # Pods for testing
  end

  post_install do |installer|
    # Fix for Coroutine.h being not found: https://github.com/wix/react-native-navigation/issues/7928#issuecomment-2447543509
    # Remove it once the proper fix is done by react-native-navigation
    system("chmod -R u+w Pods/RCT-Folly")
    Dir.glob("Pods/RCT-Folly/folly/Portability.h").each do |file|
      text = File.read(file)
      new_contents = text.gsub('#define FOLLY_HAS_COROUTINES 1', '#define FOLLY_HAS_COROUTINES 0')
      File.open(file, "w") { |file| file.puts new_contents }
    end
    react_native_post_install(
      installer,
      config[:reactNativePath],
      :mac_catalyst_enabled => ENV['ENABLE_MAC_CATALYST'] == '1',
      # :ccache_enabled => true
    )
  end
end
