require File.join(File.dirname(`node --print "require.resolve('expo/package.json')"`), "scripts/autolinking")
# 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, '17.4'
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

# Bumpt fmt version to 12.1.0 to avoid build failure with Xcode 26.4+
# Patch both fmt.podspec and RCT-Folly.podspec before CocoaPods resolves dependencies
# TODO: remove after updating RN
fmt_podspec_path = File.join(__dir__, '..', 'node_modules', 'react-native', 'third-party-podspecs', 'fmt.podspec')
folly_podspec_path = File.join(__dir__, '..', 'node_modules', 'react-native', 'third-party-podspecs', 'RCT-Folly.podspec')

if File.exist?(fmt_podspec_path)
  fmt_content = File.read(fmt_podspec_path)
  fmt_content.gsub!('"11.0.2"', '"12.1.0"')
  File.write(fmt_podspec_path, fmt_content)
end

if File.exist?(folly_podspec_path)
  folly_content = File.read(folly_podspec_path)
  folly_content.gsub!('spec.dependency "fmt", "11.0.2"', 'spec.dependency "fmt", "12.1.0"')
  File.write(folly_podspec_path, folly_content)
end

target 'BlitzWallet' do
  use_expo_modules!

  if ENV['EXPO_USE_COMMUNITY_AUTOLINKING'] == '1'
    config_command = ['node', '-e', "process.argv=['', '', 'config'];require('@react-native-community/cli').run()"];
  else
    config_command = [
      'node',
      '--no-warnings',
      '--eval',
      'require(require.resolve(\'expo-modules-autolinking\', { paths: [require.resolve(\'expo/package.json\')] }))(process.argv.slice(1))',
      'react-native-config',
      '--json',
      '--platform',
      'ios'
    ]
  end

  config = use_native_modules!(config_command)

  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}/.."
  )

  post_install do |installer|
    # temporary workaround for firebase
    installer.pods_project.targets.each do |target|
      if target.name == 'FirebaseFunctions' || target.name == 'FirebaseAuth'
        target.build_configurations.each do |config|
          if config.name == 'Release'
            config.build_settings['SWIFT_OPTIMIZATION_LEVEL'] = '-Onone'
            puts "Applied -Onone fix to #{target.name} for Swift 6.3 / Xcode 26.4 bug."
          end
        end
      end
    end

    # 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
    )
  end
end
