// swift-tools-version: 6.2
import PackageDescription

// Swift Package Manager manifest for react-native-adapty.
//
// It is consumed by React Native's experimental SwiftPM integration (RN 0.87+,
// `npx react-native spm`). The autolinker detects this hand-written manifest,
// skips scaffolding one from the podspec, and references this directory through
// `ios/build/generated/autolinking/libs/ReactNativeAdapty` — a symlink whose
// depth is what the relative React package path below is resolved against.
//
// The product name must match the Swift name the autolinker derives from the
// npm package name ("react-native-adapty" -> "ReactNativeAdapty"), because the
// generated aggregator references it as
// `.product(name: "ReactNativeAdapty", package: "ReactNativeAdapty")`.
//
// CocoaPods remains the default integration path; see
// react-native-adapty-sdk.podspec. Keep the AdaptySDK-iOS version below in sync
// with the `spm_dependency` requirement declared there.

let package = Package(
    name: "ReactNativeAdapty",
    platforms: [.iOS(.v15)],
    products: [
        .library(
            name: "ReactNativeAdapty",
            targets: ["AdaptyReactNative", "AdaptyReactNativeObjC"])
    ],
    traits: [
        .default(enabledTraits: []),
        .trait(
            name: "AdaptyReactNativeKidsMode",
            description: "COPPA / App Store Kids Category build — enables the KidsMode trait of AdaptySDK-iOS (compiles out IDFA/AdSupport)."
        )
    ],
    dependencies: [
        // React Native's prebuilt header xcframeworks, generated per app by
        // `npx react-native spm` into <app>/ios/build/xcframeworks.
        .package(name: "ReactNative", path: "../../../../xcframeworks"),
        // The app's codegen output (`npx react-native spm`), resolved from the same symlink.
        .package(name: "React-GeneratedCode", path: "../../../ios"),
        .package(
            url: "https://github.com/adaptyteam/AdaptySDK-iOS.git",
            exact: "4.3.2",
            traits: [
                .defaults,
                .trait(name: "KidsMode", condition: .when(traits: ["AdaptyReactNativeKidsMode"]))
            ]
        )
    ],
    targets: [
        // The ObjC++ RNAdapty module wrapper and the view managers'
        // RCT_EXTERN_MODULE declarations, compiled on the new-architecture path
        // (Xcode applies the defines of both settings to every C-family file).
        // Both reach the Swift classes only by name (the wrapper declares
        // RNAdaptyImpl by hand), so this target has no compile-time dependency
        // on the Swift one — which is what lets the sources be split into two
        // targets (SwiftPM cannot mix languages).
        .target(
            name: "AdaptyReactNativeObjC",
            dependencies: [
                // Link-time only: the sources below reference the Swift classes,
                // so this target must not be linked without that one.
                "AdaptyReactNative",
                .product(name: "ReactHeaders", package: "ReactNative"),
                .product(name: "ReactNativeHeaders", package: "ReactNative"),
                .product(name: "ReactNativeDependenciesHeaders", package: "ReactNative"),
                .product(name: "ReactAppHeaders", package: "React-GeneratedCode")
            ],
            path: "ios",
            sources: [
                "AdaptyFlowComponentView.mm",
                "AdaptyFlowViewManager.m",
                "AdaptyOnboardingComponentView.mm",
                "AdaptyOnboardingViewManager.m",
                "RNAdapty.mm"
            ],
            publicHeadersPath: ".",
            cSettings: [.define("RCT_NEW_ARCH_ENABLED", to: "1")],
            cxxSettings: [
                .define("RCT_NEW_ARCH_ENABLED", to: "1"),
                .define("DEBUG", .when(configuration: .debug)),
                .define("NDEBUG", .when(configuration: .release))
            ]),
        .target(
            name: "AdaptyReactNative",
            dependencies: [
                .product(name: "Adapty", package: "AdaptySDK-iOS"),
                .product(name: "AdaptyUI", package: "AdaptySDK-iOS"),
                .product(name: "AdaptyPlugin", package: "AdaptySDK-iOS"),
                .product(name: "ReactHeaders", package: "ReactNative"),
                .product(name: "ReactNativeHeaders", package: "ReactNative"),
                .product(name: "ReactNativeDependenciesHeaders", package: "ReactNative")
            ],
            path: "ios",
            sources: [
                "AdaptyFlowViewManager.swift",
                "AdaptyFlowWrapperView.swift",
                "AdaptyOnboardingViewManager.swift",
                "AdaptyOnboardingWrapperView.swift",
                "RNAdaptyImpl.swift"
            ],
            swiftSettings: [.swiftLanguageMode(.v5)])
    ],
    cxxLanguageStandard: .cxx20
)
