platform :ios, '16.0'

target 'suixinkan_ios' do
  use_frameworks!

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

  # 高德地图 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

SIMULATOR_OTHER_LDFLAGS = <<~FLAGS.squish
  -ObjC -l"c++" -l"swiftCoreGraphics" -l"z"
  -framework "Accelerate" -framework "CFNetwork" -framework "CYLTabBarController"
  -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
  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

  installer.pods_project.targets.each do |target|
    next unless target.name.start_with?('AMap')

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

  resources_script = File.join(
    installer.sandbox.root,
    'Target Support Files/Pods-suixinkan_ios/Pods-suixinkan_ios-resources.sh'
  )
  if File.exist?(resources_script)
    content = File.read(resources_script)
    unless content.include?('SKIP_AMAP_RESOURCES_FOR_SIMULATOR')
      content.sub!(
        "RESOURCES_TO_COPY=${PODS_ROOT}/resources-to-copy-${TARGETNAME}.txt",
        <<~SHELL
          # SKIP_AMAP_RESOURCES_FOR_SIMULATOR
          if [ "${EFFECTIVE_PLATFORM_NAME}" = "-iphonesimulator" ]; then
            exit 0
          fi

          RESOURCES_TO_COPY=${PODS_ROOT}/resources-to-copy-${TARGETNAME}.txt
        SHELL
      )
      File.write(resources_script, content)
    end
  end

  installer.aggregate_targets.each do |aggregate_target|
    aggregate_target.user_project.native_targets.each do |target|
      target.build_configurations.each do |config|
        config.build_settings['EXCLUDED_ARCHS[sdk=iphonesimulator*]'] = ''
        config.build_settings['ENABLE_USER_SCRIPT_SANDBOXING'] = 'NO'

        next unless target.name == 'suixinkan_ios'

        existing = config.build_settings['SWIFT_ACTIVE_COMPILATION_CONDITIONS[sdk=iphoneos*]'] || '$(inherited)'
        unless existing.to_s.include?('AMAP_ENABLED')
          config.build_settings['SWIFT_ACTIVE_COMPILATION_CONDITIONS[sdk=iphoneos*]'] = "#{existing} AMAP_ENABLED"
        end
      end
    end
    aggregate_target.user_project.save
  end
end
