require 'json'

package = JSON.parse(File.read(File.join(__dir__, 'package.json')))

# Only evaluated when autolinking has already decided to link Appduct, so the line appearing at
# all is the proof. Ungated: a release build carrying Appduct by mistake is the failure worth
# catching, and this is grep-able in CI output. CocoaPods evaluates the podspec several times per
# install, hence the guard.
if defined?(Pod::UI) && !defined?($appduct_banner_shown)
  $appduct_banner_shown = true
  Pod::UI.puts "[appduct] native module INCLUDED in this build (v#{package['version']})"
end

Pod::Spec.new do |s|
  s.name           = 'Appduct'
  s.version        = package['version']
  s.summary        = package['description']
  s.description    = package['description']
  s.license        = package['license']
  s.author         = package['author']
  s.homepage       = package['homepage']
  s.platforms      = {
    :ios => '15.1',
    :tvos => '15.1'
  }
  s.swift_version  = '5.9'
  s.source         = { git: 'https://github.com/callstackincubator/appduct' }
  s.static_framework = true

  # 'ios/Core/**/*.swift' is vendored from packages/native/ios/Sources/AppductCore/Real by
  # scripts/sync-native-core.mjs (wired into this package's build/prepack scripts) -- not checked
  # into git, but present by the time CocoaPods reads this podspec, and included in the npm
  # tarball (see .npmignore) so an installed consumer has it too.
  s.source_files = 'ios/*.{m,mm,swift}', 'ios/Core/**/*.swift'

  s.pod_target_xcconfig = {
    'DEFINES_MODULE' => 'YES',
    # Unconditional: autolinking (react-native.config.js's `:configurations` restriction) is what
    # decides whether this pod links into a given build variant at all, so by the time these
    # sources are compiled the decision to include Appduct has already been made -- the vendored
    # ios/Core sources should always compile as the real implementation, never the
    # AppductCore SwiftPM package's Stub/ branch (which this pod never even sees -- only Real/ is
    # vendored). All connection state lives behind a single actor (see AppductConnectionManager);
    # `-strict-concurrency=complete` keeps it that way by failing the build on any new concurrency
    # violation.
    'OTHER_SWIFT_FLAGS' => '-DAPPDUCT_ENABLED -strict-concurrency=complete',
  }

  if defined?(install_modules_dependencies)
    install_modules_dependencies(s)
  else
    s.dependency 'React-Core'
  end

  # No test_spec: the Swift/Foundation-only unit tests that used to live in ios/AppductTests
  # moved with the sources to packages/native/ios/Tests/AppductCoreTests, and run via `swift
  # test` against the AppductCore SwiftPM package (repo-root Package.swift) instead of a
  # Pods-generated XCTest scheme -- see .github/workflows/test.yaml.
end
