source 'https://github.com/CocoaPods/Specs.git'
# source 'https://github.com/complycube/complycube-sdk-ios-spec-dev'
source 'https://cdn.cocoapods.org/'
# plugin 'cocoapods-art', :sources => [
#   'cc-cocoapods-release-local'
# ]
# Add your dev repo only if the flag is set
if ENV['USE_DEV_LIB'] == 'true'
  source 'https://github.com/complycube/complycube-sdk-ios-spec-dev'
  puts "🚧 Using ComplyCube dev spec repo"
end
# source 'https://github.com/complycube/complycube-sdk-ios-spec-dev'
# Uncomment this line to define a global platform for your project
platform :ios, '15.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

if ENV['USE_JFROG'] == 'true'
  plugin 'cocoapods-art', :sources => [
    'cc-cocoapods-release-local',
  ]
end


target 'Runner' do
  use_frameworks!
  use_modular_headers!

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

  target 'RunnerTests' do
    inherit! :search_paths
  end
end


post_install do |installer|
  ios_13_pods = [
    'Sentry',
    'Analytics',
    'Alamofire',
    'SwiftyJSON',
    'JWTDecode',
    'GooglePlaces',
    'lottie-ios'
  ]

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

    target.build_configurations.each do |config|
      # Optional: patch xcconfig
      if config.respond_to?(:base_configuration_reference) && config.base_configuration_reference
        xcconfig_path = config.base_configuration_reference.real_path
        if File.exist?(xcconfig_path)
          xcconfig = File.read(xcconfig_path)
          xcconfig_mod = xcconfig.gsub(/DT_TOOLCHAIN_DIR/, "TOOLCHAIN_DIR")
          File.write(xcconfig_path, xcconfig_mod)
        end
      end

      # Build settings
      config.build_settings['ENABLE_BITCODE'] = 'NO'
      config.build_settings['BUILD_LIBRARY_FOR_DISTRIBUTION'] = 'YES'
      config.build_settings['MACOSX_DEPLOYMENT_TARGET'] = '10.15'
      config.build_settings['SWIFT_VERSION'] = '5.0'
      config.build_settings['EXCLUDED_ARCHS[sdk=iphonesimulator*]'] = 'i386'
      config.build_settings['CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER'] = 'NO'

      # Set deployment target
      if ios_13_pods.include?(target.name)
        config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '15.0'
      else
        config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '15.0'
      end

      # Optional dev team
      config.build_settings['DEVELOPMENT_TEAM'] = 'Q7VU8P7DTS'

      # Permission handler flags
      config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [
        '$(inherited)',
        'PERMISSION_NOTIFICATIONS=1'
      ]
    end
  end

  # Force Flutter framework to use iOS 15.0
  installer.pods_project.targets.each do |target|
    next unless target.name == 'Flutter'
    target.build_configurations.each do |config|
      config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '15.0'
    end
  end

  # Fix Flutter.podspec to use iOS 15.0
  flutter_podspec_path = File.join(File.dirname(__FILE__), 'Flutter', 'Flutter.podspec')
  if File.exist?(flutter_podspec_path)
    flutter_podspec_content = File.read(flutter_podspec_path)
    flutter_podspec_content.gsub!(/s\.ios\.deployment_target = ['"][\d.]+['"]/, "s.ios.deployment_target = '15.0'")
    File.write(flutter_podspec_path, flutter_podspec_content)
  end
end

