ENV['RCT_NEW_ARCH_ENABLED'] = '1'

# LaTeX math rendering (RaTeX). Set to '0' to disable and remove the dependency.
# RaTeX is a vendored prebuilt static XCFramework, so math works under CocoaPods
# default static linkage -- no `use_frameworks! :linkage => :dynamic` required.
# ENV['ENRICHED_MARKDOWN_ENABLE_MATH'] = '0'

# Keep project default to full list of languages
ENV['ENRICHED_MARKDOWN_CODE_HIGHLIGHT_LANGUAGES'] = 'json,html,css,markdown,yaml,go,java,javascript,python,c,rust,bash,typescript,tsx,cpp,swift,php,ruby,c-sharp'
# ENV['ENRICHED_MARKDOWN_ENABLE_CODE_HIGHLIGHT'] = '0'

# Resolve react_native_pods.rb with node to allow for hoisting
require Pod::Executable.execute_command('node', ['-p',
  'require.resolve(
    "react-native/scripts/react_native_pods.rb",
    {paths: [process.argv[1]]},
  )', __dir__]).strip

def is_ci_enabled?
  %w[1 true].include?(ENV['CI'].to_s.downcase)
end

# ENV['DISABLE_RNREPO'] = "1" # Uncomment to disable RNRepo even in CI
def is_rnrepo_enabled?
  ENV['DISABLE_RNREPO'].nil?
end

# Use RNRepo in CI builds. Set DISABLE_RNREPO to any value to disable RNRepo.
if is_ci_enabled? && is_rnrepo_enabled?
  require Pod::Executable.execute_command('node', ['-p',
    'require.resolve(
      "@rnrepo/build-tools/cocoapods-plugin/lib/plugin.rb",
      {paths: [process.argv[1]]},
    )', __dir__]).strip
end

platform :ios, min_ios_version_supported
prepare_react_native_project!

linkage = ENV['USE_FRAMEWORKS']
if linkage != nil
  Pod::UI.puts "Configuring Pod with #{linkage}ally linked Frameworks".green
  use_frameworks! :linkage => linkage.to_sym
end

target 'EnrichedMarkdownExample' do
  config = use_native_modules!

  use_react_native!(
    :path => config[:reactNativePath],
    # An absolute path to your application root.
    :app_path => "#{Pod::Config.instance.installation_root}/.."
  )

  # App-hosted XCTest target for the library's iOS unit tests. `inherit! :complete`
  # links the same pods as the host app (including ReactNativeEnrichedMarkdown), so
  # the tests compile and run against the exact code the example app ships. The
  # XCTest sources live in ../../../packages/react-native-enriched-markdown/__tests__/ios
  # and are added to the EnrichedMarkdownExampleTests target in the Xcode project.
  target 'EnrichedMarkdownExampleTests' do
    inherit! :complete
  end

  post_install do |installer|
    if is_ci_enabled? && is_rnrepo_enabled?
      rnrepo_post_install(installer)
    end
    react_native_post_install(
      installer,
      config[:reactNativePath],
      :mac_catalyst_enabled => false,
      # :ccache_enabled => true
    )

    # Xcode 27 rejects deployment targets below iOS 15.0. Some pods' resource
    # bundle targets still declare older values, so bump anything below the
    # supported minimum.
    installer.pods_project.targets.each do |target|
      target.build_configurations.each do |config|
        deployment_target = config.build_settings['IPHONEOS_DEPLOYMENT_TARGET']
        if deployment_target && Gem::Version.new(deployment_target) < Gem::Version.new(min_ios_version_supported)
          config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = min_ios_version_supported
        end
      end
    end
  end
end
