platform :ios, '16.0'

target 'suixinkan_ios' do
  use_frameworks!

  pod 'SnapKit', '~> 5.7'
  pod 'Kingfisher', '~> 8.0'

  # 高德地图 SDK 仅支持真机；模拟器通过下方 post_install 剥离链接
  pod 'AMap3DMap-NO-IDFA', '~> 11.1'
  pod 'AMapSearch-NO-IDFA', '~> 9.7'
  pod 'AMapLocation-NO-IDFA', '~> 2.11'

  target 'suixinkan_iosTests' do
    inherit! :search_paths
  end
end

# 模拟器构建时不链接高德（真机专用 framework），仅保留通用 Pod 依赖
SIMULATOR_OTHER_LDFLAGS = <<~FLAGS.squish
  -ObjC -l"c++" -l"swiftCoreGraphics" -l"z"
  -framework "Accelerate" -framework "CFNetwork"
  -framework "Kingfisher" -framework "SnapKit"
  -framework "CoreGraphics" -framework "CoreLocation" -framework "CoreTelephony"
  -framework "CoreText" -framework "ExternalAccessory" -framework "GLKit"
  -framework "OpenGLES" -framework "QuartzCore" -framework "Security"
  -framework "SystemConfiguration" -weak_framework "Combine" -weak_framework "SwiftUI"
FLAGS

post_install do |installer|
  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |config|
      config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '16.0'
    end

    next unless target.name.start_with?('AMap')

    target.build_configurations.each do |config|
      config.build_settings['SUPPORTED_PLATFORMS'] = 'iphoneos'
    end
  end

  %w[Pods-suixinkan_ios Pods-suixinkan_iosTests].each do |target_name|
    support_dir = File.join(installer.sandbox.root, 'Target Support Files', target_name)
    Dir.glob(File.join(support_dir, '*.xcconfig')).each do |path|
      content = File.read(path)
      content.gsub!("EXCLUDED_ARCHS[sdk=iphonesimulator*] = arm64\n", '')

      unless content.include?('OTHER_LDFLAGS[sdk=iphonesimulator*]')
        content << "\nOTHER_LDFLAGS[sdk=iphonesimulator*] = #{SIMULATOR_OTHER_LDFLAGS}\n"
        content << "FRAMEWORK_SEARCH_PATHS[sdk=iphonesimulator*] = $(inherited)\n"
      end

      File.write(path, content)
    end
  end
end
