require_relative '../../node_modules/@capacitor/ios/scripts/pods_helpers'

platform :ios, '15.0'
use_frameworks!

# workaround to avoid Xcode caching of Pods that requires
# Product -> Clean Build Folder after new Cordova plugins installed
# Requires CocoaPods 1.6 or newer
install! 'cocoapods', :disable_input_output_paths => false

def capacitor_pods
  pod 'Capacitor', :path => '../../node_modules/@capacitor/ios'
  pod 'CapacitorCordova', :path => '../../node_modules/@capacitor/ios'
  pod 'CapacitorApp', :path => '../../node_modules/@capacitor/app'
end

pre_install do |installer|
  # Fix script permissions before installation
  Dir.glob("Pods/Target Support Files/**/*.sh").each do |script|
    system('chmod', 'u+rx', script) if File.file?(script)
  end
end

target 'App' do
  capacitor_pods
  # Add your Pods here
end

post_install do |installer|
  assertDeploymentTarget(installer)

  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |config|
      # Disable sandbox restrictions
      config.build_settings['ENABLE_USER_SCRIPT_SANDBOXING'] = 'NO'
      config.build_settings['CODE_SIGNING_ALLOWED'] = 'NO'

      # Additional sandbox-related settings
      config.build_settings['SYSTEM_FRAMEWORK_SEARCH_PATHS'] = '$(inherited) $(PLATFORM_DIR)/Developer/Library/Frameworks'
      config.build_settings['ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES'] = 'NO'

      # Fix for Xcode 15
      config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)', 'COCOAPODS=1', 'DISABLE_SANDBOXING=1']

      # Suppress WKProcessPool deprecation warning in CapacitorCordova
      if target.name == 'CapacitorCordova'
        config.build_settings['GCC_WARN_DEPRECATED_DECLARATIONS'] = 'NO'
      end
    end
  end
  
  # Fix script permissions after installation
  Dir.glob(File.join(installer.sandbox.root, "**/*.sh")).each do |script|
    system('chmod', 'u+rx', script) if File.file?(script)
  end

  # Fix "[CP] Embed Pods Frameworks" warning by disabling dependency analysis
  installer.pods_project.targets.each do |target|
    target.build_phases.each do |phase|
      if phase.respond_to?(:name) && phase.name&.start_with?('[CP]')
        phase.always_out_of_date = '1'
      end
    end
  end
end
