# source 'https://github.com/CocoaPods/Specs.git'
workspace 'WatermelonTester.xcworkspace'

require_relative '../../node_modules/react-native/scripts/react_native_pods'
require_relative '../../node_modules/@react-native-community/cli-platform-ios/native_modules'

platform :ios, '15.0'

hermes_enabled = true
should_use_frameworks = false

if should_use_frameworks
  use_frameworks! linkage: :static

  $static_framework = [
    'WatermelonDB',
    'simdjson',
  ]
end

Pod::Sandbox::FileAccessor.send(:define_method, :docs) do
  # work around https://github.com/CocoaPods/CocoaPods/issues/11753#issuecomment-1425802717
  # p "Monkey-patched docs :)"
  return []
end

target 'WatermelonTester' do
  config = use_native_modules!
  flags = get_default_flags()

  use_react_native!(
    path: '../../node_modules/react-native',
    hermes_enabled: hermes_enabled,
    fabric_enabled: false,
    app_path: "#{Pod::Config.instance.installation_root}/../.."
  )

  pod 'WatermelonDB', path: '../../'
  pod 'simdjson', path: '../../node_modules/@nozbe/simdjson', modular_headers: true, inhibit_warnings: true

  target 'WatermelonTesterTests' do
    inherit! :complete
  end
end

if should_use_frameworks
  pre_install do |installer|
    Pod::Installer::Xcode::TargetValidator.send(:define_method, :verify_no_static_framework_transitive_dependencies) {}
      installer.pod_targets.each do |pod|
        if $static_framework.include?(pod.name)
          def pod.build_type;
          Pod::BuildType.static_library # >= 1.9
        end
      end
    end
  end
end

post_install do |installer|
  react_native_post_install(
    installer,
    '../../node_modules/react-native',
    mac_catalyst_enabled: false,
  )

  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |config|
      config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= %w[
        $(inherited)
        CCACHE_HACK_TOOLCHAIN_DIR="$(TOOLCHAIN_DIR)"
      ]
      # ccache bails out of caching if clang modules are enabled, but this breaks some packages
      # you also have to be careful about PCHs
      # sometimes you might have to manually add a system framework to project Link phase
      # more info: https://pspdfkit.com/blog/2015/ccache-for-fun-and-profit/

      # TODO: Bring back CC
      # config.build_settings['CC'] ||= ['$(SRCROOT)/../../../scripts/ccache-clang']

      # case target.name
      # when 'Nimble'
      #   config.build_settings['CLANG_ENABLE_MODULES'] ||= ['YES']
      # else
        config.build_settings['CLANG_ENABLE_MODULES'] ||= ['NO']
      # end

      # Fixes https://github.com/facebook/react-native/issues/34106
      if target.name == 'React-Codegen'
        config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '12.4'
      end
    end
  end
end
