source 'https://github.com/CocoaPods/Specs.git'
source 'https://github.com/dev-adwhale/AdWhaleSDK_iOS.git'

# Uncomment this line to define a global platform for your project
platform :ios, '13.0'

# CocoaPods analytics sends network stats synchronously affecting flutter build latency.
ENV['COCOAPODS_DISABLE_STATS'] = 'true'

project 'Runner', {
  'Debug' => :debug,
  'Profile' => :release,
  'Release' => :release,
}

def flutter_root
  generated_xcode_build_settings_path = File.expand_path(File.join('..', 'Flutter', 'Generated.xcconfig'), __FILE__)
  unless File.exist?(generated_xcode_build_settings_path)
    raise "#{generated_xcode_build_settings_path} must exist. If you're running pod install manually, make sure flutter pub get is executed first"
  end

  File.foreach(generated_xcode_build_settings_path) do |line|
    matches = line.match(/FLUTTER_ROOT\=(.*)/)
    return matches[1].strip if matches
  end
  raise "FLUTTER_ROOT not found in #{generated_xcode_build_settings_path}. Try deleting Generated.xcconfig, then run flutter pub get"
end

require File.expand_path(File.join('packages', 'flutter_tools', 'bin', 'podhelper'), flutter_root)

flutter_ios_podfile_setup

target 'Runner' do
  use_frameworks!

  flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))

  # AdWhale iOS SDK 1.0.7 changelog 기준 파트너 네트워크 버전 고정
  # (※ 1.0.7로 올린 뒤 changelog에 맞춰 아래 어댑터 버전을 재확인할 것)
  pod 'GoogleMobileAdsMediationUnity', '4.16.6.1'
  pod 'GoogleMobileAdsMediationMoloco', '4.4.1.0'
  pod 'GoogleMobileAdsMediationAppLovin', '13.5.1.0'
  pod 'GoogleMobileAdsMediationInMobi', '11.1.1.1'
  pod 'GoogleMobileAdsMediationVungle', '7.7.0.0'
  pod 'GoogleMobileAdsMediationFyber', '8.4.4.1'
  pod 'GoogleMobileAdsMediationMintegral', '8.0.7.0'
  pod 'GoogleMobileAdsMediationPangle', '7.9.0.6.0'
  pod 'GoogleMobileAdsMediationIronSource', '9.3.0.0.1'

  target 'RunnerTests' do
    inherit! :search_paths
  end
end

post_install do |installer|
  installer.pods_project.targets.each do |target|
    flutter_additional_ios_build_settings(target)
  end

  # Mediation 어댑터(InMobi 등) 사용 시 필요: Runner 앱 타겟 Other Linker Flags에 -ObjC 추가
  runner_project_path = File.join(File.dirname(__FILE__), 'Runner.xcodeproj')
  if File.exist?(runner_project_path)
    require 'xcodeproj'
    project = Xcodeproj::Project.open(runner_project_path)
    project.native_targets.each do |target|
      if target.name == 'Runner'
        target.build_configurations.each do |config|
          flags = config.build_settings['OTHER_LDFLAGS'] || ['$(inherited)']
          flags = [flags] if flags.is_a?(String)
          config.build_settings['OTHER_LDFLAGS'] = (flags + ['-ObjC']).uniq
        end
        break
      end
    end
    project.save
  end
end
