require 'json'
require 'pathname'
require_relative 'ios/copy-airbridge-json.rb'
require_relative 'ios/create-library-info-json.rb'

current_directory=File.dirname(__FILE__)
package = JSON.parse(File.read(File.join(current_directory, 'package.json')))

source = Pathname.new(Dir.pwd).relative_path_from(Pathname.new(__dir__)).to_s

Pod::Spec.new do |s|
    s.name                  = package['name']
    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.source                = { :http => 'file:' + source }

    s.requires_arc          = true
    s.platform              = :ios, '12.0'

    s.preserve_paths        = 'README.md', 'package.json', 'index.js'

    s.source_files          = 'ios/AirbridgeReactNative/**/*.{h,m,swift}'

    s.header_dir            = 'AirbridgeReactNative'
    s.public_header_files   = 'ios/AirbridgeReactNative/AirbridgeReactNative.h'

    s.dependency 'React'

    # Airbridge iOS SDK is distributed as a prebuilt dynamic XCFramework.
    # The XCFramework is downloaded by ios/download-airbridge-framework.sh and
    # consumed here as a vendored framework, so CocoaPods handles embedding and
    # code signing into the host app via its own "[CP] Embed Pods Frameworks"
    # phase. The weak frameworks are the ones the airbridge-ios-sdk pod declared.
    #
    # `prepare_command` runs for BOTH remote and `:path` pods: CocoaPods calls
    # PodSourcePreparer#prepare! for local pods (PodSourceInstaller#install!),
    # and runs it inside the downloader for remote ones.
    #
    # It runs only when CocoaPods actually INSTALLS the pod ("Installing
    # airbridge-react-native-sdk"). Once the sandbox marks the pod as installed,
    # `pod install` reports "Using airbridge-react-native-sdk" and skips it —
    # verified against CocoaPods 1.16.2. So this is a first-install safety net,
    # not a repair mechanism: deleting ios/Airbridge.xcframework and re-running
    # `pod install` will NOT bring it back until the pod is reinstalled.
    #
    # --if-missing keeps it from replacing the internal build that qa/ios/Podfile
    # has just swapped in: CocoaPods installs the pod after evaluating the
    # Podfile, and this command runs without the QA variable.
    #
    # The XCFramework therefore ships inside the npm tarball (package.json
    # `files` + `prepack`), and qa/ios/Podfile invokes the download script
    # explicitly. Keep all three — do not remove this as "redundant".
    s.prepare_command       = 'sh ios/download-airbridge-framework.sh --if-missing'
    s.vendored_frameworks   = 'ios/Airbridge.xcframework'
    s.weak_frameworks       = 'AdSupport', 'AppTrackingTransparency', 'AdServices', 'CoreTelephony', 'StoreKit', 'WebKit'

    s.pod_target_xcconfig = {
        'DEFINES_MODULE' => 'YES'
    }
end
