platform :ios, '13.0'

source 'https://github.com/CocoaPods/Specs.git'
source 'https://github.com/volcengine/volcengine-specs.git'

# 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! :linkage => :static

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

  # 火山实时语音 SDK 仅提供 iphoneos arm64 / x86_64-simulator 切片
  # Apple Silicon Mac 的 arm64 模拟器不被支持。
  # 模拟器构建前请执行: SPEECH_SDK_ENABLED=0 pod install
  # 真机构建前执行:    pod install （默认引入）
  if ENV['SPEECH_SDK_ENABLED'] != '0'
    # 与火山 SpeechDemoIOS demo 保持一致，用全量包 SpeechEngineToB（含 APP_KEY/RESOURCE_ID 等字段）。
    # SpeechEngineDialogToB 0.0.1-rc.0 是更新的 RTC 重构版，参数体系不同且服务端尚未完全支持。
    pod 'SpeechEngineToB', '0.0.9'
  end

  target 'RunnerTests' do
    inherit! :search_paths
  end
end

post_install do |installer|
  speech_sdk_enabled = ENV['SPEECH_SDK_ENABLED'] != '0'

  installer.pods_project.targets.each do |target|
    flutter_additional_ios_build_settings(target)
    target.build_configurations.each do |config|
      config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '13.0'

      if speech_sdk_enabled
        # 火山 SDK 没有 arm64-simulator slice，需要全局排除以让真机/Intel 模拟器构建通过
        config.build_settings['EXCLUDED_ARCHS[sdk=iphonesimulator*]'] = 'arm64'
      else
        # 模拟器开发模式：强制取消 arm64 排除（覆盖 flutter_inappwebview 自带的排除），
        # 让 Apple Silicon Mac 的 arm64 模拟器能跑
        config.build_settings['EXCLUDED_ARCHS[sdk=iphonesimulator*]'] = ''
        config.build_settings.delete('EXCLUDED_ARCHS')
      end
    end
  end
end
