require_relative './podfile_helpers.rb'
require_relative '../react-native-lab/react-native/packages/react-native/scripts/react_native_pods'
require File.join(File.dirname(`node --print "require.resolve('expo/package.json')"`), "scripts/autolinking")

install! 'cocoapods',
         :generate_multiple_pod_projects => true,
         :incremental_installation => true
platform :ios, '13.0'
inhibit_all_warnings!

# Disable expo-updates auto create manifest in podspec script_phase
$expo_updates_create_manifest = false

abstract_target 'Expo Go' do
  pod 'CocoaLumberjack', '~> 3.5.3'
  pod 'GoogleMaps', '~> 7.3.0'
  pod 'Google-Maps-iOS-Utils', :git => 'https://github.com/googlemaps/google-maps-ios-utils.git'
  pod 'JKBigInteger', :podspec => 'vendored/common/JKBigInteger.podspec.json'
  pod 'MBProgressHUD', '~> 1.2.0'

  # transitive dependency of React-Core and we use it to get the `RCTInspectorPackagerConnection` state
  pod 'SocketRocket'

  # Required by firebase core versions 9.x / 10.x (included with SDK 47)
  # See https://github.com/invertase/react-native-firebase/issues/6332#issuecomment-1189734581
  pod 'FirebaseCore', :modular_headers => true
  pod 'GoogleUtilities', :modular_headers => true

  # Expo modules
  use_expo_modules!({
    exclude: [
      'expo-module-template',
      'expo-module-template-local',
      'expo-in-app-purchases',
      'expo-dev-menu',
      'expo-dev-menu-interface',
      'expo-dev-launcher',
      'expo-dev-client',
      'expo-maps',
      'expo-network-addons',
      'expo-insights',
      'expo-face-detector'
    ],
    includeTests: true,
    flags: {
      :inhibit_warnings => false
    }
  })

  # Install vendored pods.
  use_pods! 'vendored/unversioned/**/*.podspec.json'

  # Unversioned React Native
  use_react_native!(
    :path => '../react-native-lab/react-native/packages/react-native',
    :hermes_enabled => true,
    :fabric_enabled => false,
  )
  setup_jsc!(
    :react_native_path => '../react-native-lab/react-native/packages/react-native',
    :fabric_enabled => false,
  )

  post_install do |installer|
    # Workaround build error for Folly
    __apply_Xcode_12_5_M1_post_install_workaround(installer) if installer.pods_project

    # Disabled as of CocoaPods 1.8.0.beta1 since pods_project seems to be nil
    # installer.pods_project.main_group.tab_width = '2';
    # installer.pods_project.main_group.indent_width = '2';

    installer.target_installation_results.pod_target_installation_results
      .each do |pod_name, target_installation_result|

      # Run postinstalls actions for versioned dependencies.
      # These actions are specified in `versioned-react-native/ABI*/postinstalls.rb` files.
      run_versioned_postinstalls!(pod_name, target_installation_result)

      # This is necessary for Xcode 14 that by default signs resource bundles when building for the device.
      target_installation_result.resource_bundle_targets.each do |resource_bundle_target|
        resource_bundle_target.build_configurations.each do |config|
          config.build_settings['CODE_SIGNING_ALLOWED'] = 'NO'
        end
      end

      # Xcode 15 build error workaround. Remove after updating RN
      target_installation_result.native_target.build_configurations.each do |config|
        # unary_function and binary_function are no longer provided in C++17 and newer standard modes as part of Xcode 15. They can be re-enabled with setting _LIBCPP_ENABLE_CXX17_REMOVED_UNARY_BINARY_FUNCTION
        # Ref: https://developer.apple.com/documentation/xcode-release-notes/xcode-15-release-notes#Deprecations
        config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)']
        config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] << '"_LIBCPP_ENABLE_CXX17_REMOVED_UNARY_BINARY_FUNCTION" '
      end

      target_installation_result.native_target.build_configurations.each do |config|
        config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '13.0'

      end

      if pod_name == 'Branch'
        target_installation_result.native_target.build_configurations.each do |config|
          config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)']
          config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] << 'BRANCH_EXCLUDE_IDFA_CODE=1'
        end
      end

      # On iOS, the Stripe dependency StripePaymentsUI can't seem to find a header that should be there.
      if pod_name == 'StripePaymentsUI'
        target_installation_result.native_target.build_configurations.each do |config|
          config.build_settings['HEADER_SEARCH_PATHS'] ||= ['$(inherited)']
          config.build_settings['HEADER_SEARCH_PATHS'] << '"$(PODS_ROOT)/Headers/Public/StripePayments"'
        end
      end

      if pod_name.end_with?('EXUpdates')
        target_installation_result.native_target.build_configurations.each do |config|
          config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)']
          config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] << 'SUPPRESS_EXPO_UPDATES_SERVICE=1'
        end
      end

      # Build React Native with RCT_DEV enabled and RCT_ENABLE_INSPECTOR and
      # RCT_ENABLE_PACKAGER_CONNECTION disabled
      next unless pod_name.start_with?('React')
      target_installation_result.native_target.build_configurations.each do |config|
        config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)']
        config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] << 'RCT_DEV=1'
        config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] << 'RCT_ENABLE_INSPECTOR=0'
        config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] << 'RCT_REMOTE_PROFILE=0'
        config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] << 'RCT_DEV_SETTINGS_ENABLE_PACKAGER_CONNECTION=0'
      end
    end
  end

  # Target for development, contains only unversioned code
  target 'Expo Go (unversioned)' do
  end

  # Release target additionally includes versioned SDKs
  target 'Expo Go (versioned)' do
    # Evaluate all files matching `versioned-react-native/ABI*/dependencies.rb` glob pattern
    # and install ReactABIXX_0_0 pods with all versioned unimodules.
    use_versioned_abis!
  end

  # Test targets
  target 'ExponentIntegrationTests' do
    inherit! :search_paths
  end

  target 'Tests' do
    # `ExpoModulesTestCore` has implicit dependency to `React-Core` which has a resource bundle.
    # To prevent CocoaPods generating new `React-Core` resource bundle and the strange `React-Core-60309c9c` target,
    # this test target should inherit all properties from parents.
    inherit! :complete

    pod 'ExpoModulesTestCore', :path => "../packages/expo-modules-test-core/ios"
    pod 'Nimble', :podspec => './Nimble.podspec'
  end
end
