# 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

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
end

target 'SampleApp' do
  config = use_native_modules!

  # Added for op-sqlite library - https://ospfranco.notion.site/Installation-93044890aa3d4d14b6c525ba4ba8686f#acdc7a6723234f8b8440acb1ae0e7d87
  # Also force the @react-native-firebase shim pods to build as plain static
  # libraries. Under `use_frameworks! :linkage => :static`, each RNFB pod
  # otherwise emits its own modulemap and ends up "owning" React-Core
  # declarations like RCTPromiseRejectBlock that it merely #imports — which
  # clang then rejects when a second RNFB pod (RNFBMessaging) tries to use
  # the same type. Static-library mode removes the framework module wrapper
  # so React-Core's module remains the authoritative owner.
  pre_install do |installer|
    installer.pod_targets.each do |pod|
      if pod.name.eql?('op-sqlite') || pod.name.start_with?('RNFB')
        def pod.build_type
          Pod::BuildType.static_library
        end
      end
    end
  end

  # Beginning with firebase-ios-sdk v9+ (react-native-firebase v15+) we must tell CocoaPods to use frameworks.
  # https://rnfirebase.io/#altering-cocoapods-to-use-frameworks
  use_frameworks! :linkage => :static
  $RNFirebaseAsStaticFramework = true

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

  target 'SampleAppTests' do
    inherit! :complete
    # Pods for testing
  end
  # Pin Firebase iOS SDK to match what @react-native-firebase 24.x declares
  # (sdkVersions.ios.firebase = 12.10.0). RNFBMessaging's podspec declares
  # `FirebaseCoreExtension` without a version, so CocoaPods otherwise resolves
  # it to the latest spec-repo entry (often older 11.x), which then conflicts
  # with RNFBApp's Firebase/CoreOnly 12.10.0.
  pod 'Firebase/Crashlytics', '12.10.0'
  pod 'Firebase/Analytics', '12.10.0'
  pod 'Firebase/AppDistribution', '12.10.0'
  pod 'FirebaseCoreExtension', '12.10.0'

  post_install do |installer|
    # https://github.com/facebook/react-native/blob/main/packages/react-native/scripts/react_native_pods.rb#L197-L202
    react_native_post_install(
      installer,
      config[:reactNativePath],
      :mac_catalyst_enabled => false,
      # :ccache_enabled => true
    )

    # RNFBApp's RCTConvert+FIRApp.h imports <React/RCTConvert.h> non-modularly.
    # Under `use_frameworks! :linkage => :static`, clang treats that as an
    # error unless the importing framework opts into non-modular includes.
    installer.pods_project.targets.each do |target|
      target.build_configurations.each do |config|
        config.build_settings['CLANG_ALLOW_NON_MODULAR_INCLUDES_IN_FRAMEWORK_MODULES'] = 'YES'
        # Some pods pin an iOS deployment target (e.g. 13.0) below the range
        # supported by current Xcode. Clamp them up to the app's minimum.
        if config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'].to_f < min_ios_version_supported.to_f
          config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = min_ios_version_supported
        end
      end
    end

    # The Pods project itself can also carry a stale project-level setting.
    installer.pods_project.build_configurations.each do |config|
      if config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'].to_f < min_ios_version_supported.to_f
        config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = min_ios_version_supported
      end
    end
  end
end