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__))
  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

  # 微信 SDK podspec 对模拟器保守排除 arm64（其 xcframework 实际含 arm64 slice），
  # 不清除会导致 M 系 Mac 上模拟器构建被压成 x86_64 而无法安装运行。
  wechat = installer.pods_project.targets.find { |t| t.name == 'WechatOpenSDK-XCFramework' }
  wechat&.build_configurations&.each do |config|
    config.build_settings['EXCLUDED_ARCHS[sdk=iphonesimulator*]'] = ''
  end

  # WechatOpenSDK-XCFramework 的 Headers 不会自动进入依赖方搜索路径
  # （CocoaPods 对 vendored XCFramework 的已知行为），fluwx 以引号引入
  # WXApi.h 需要显式补充头文件搜索路径。
  wechat_headers = Dir.glob(
    File.join(Pod::Config.instance.project_root, 'Pods', 'WechatOpenSDK-XCFramework',
              'WechatOpenSDK.xcframework', '*', 'WechatOpenSDK.framework', 'Headers')
  )
  unless wechat_headers.empty?
    fluwx = installer.pods_project.targets.find { |t| t.name == 'fluwx' }
    fluwx&.build_configurations&.each do |config|
      config.build_settings['HEADER_SEARCH_PATHS'] = [
        '$(inherited)',
        *wechat_headers.map { |p| "\"#{p}\"" },
      ].join(' ')
    end
  end
end
