// swift-tools-version:5.9
import PackageDescription

// ─────────────────────────────────────────────────────────────────────────────
// Freshdesk React Native — Swift Package manifest (TRANSITIONAL / EXPERIMENTAL)
//
// Why this exists
// ---------------
// CocoaPods is entering read-only/maintenance mode (specs trunk goes read-only on
// 2026-12-02) and React Native is moving its iOS integration toward Swift Package
// Manager (SPM). This manifest is a starting point for distributing the iOS side of
// `@freshworks/react-native-freshdesk-sdk` via SPM, alongside the existing
// `FreshdeskReactNative.podspec`. Keep BOTH during the transition.
//
// What it does
// ------------
//  • Vendors the native Freshdesk iOS SDK as a binary target (the same
//    `FreshdeskSDK.xcframework` the podspec falls back to on RN < 0.75; on RN 0.75+
//    the podspec instead consumes the Freshdesk Swift Package via `spm_dependency`).
//  • Exposes the React Native bridging layer (`ios/FreshdeskNativeModule.*`).
//
// Known constraints (read before relying on this)
// ------------------------------------------------
//  1. MIXED LANGUAGE: SwiftPM does NOT allow Swift + Objective-C in one target, but
//     the bridge ships both `FreshdeskNativeModule.swift` and `FreshdeskNativeModule.m`.
//     They are therefore split into two targets that share the `ios/` directory via
//     explicit `sources` lists. The Objective-C target depends on the Swift target so
//     the generated `FreshdeskReactNative-Swift.h` umbrella header resolves.
//  2. REACT DEPENDENCY: The bridge imports `React` (Swift) and `<React/RCTBridgeModule.h>`,
//     `<React/RCTEventEmitter.h>` (Obj-C). React-Core is NOT a standalone SPM package; the
//     symbols/headers are provided by the host app's React Native build graph (this is what
//     RN's emerging SPM autolinking injects). As a result, a bare `swift build` of this
//     package WILL NOT compile in isolation — it is expected to be resolved inside an app
//     that already brings React. The header search path below is a placeholder for that wiring.
//  3. NEW ARCHITECTURE: codegen output under `ios/generated` (the podspec's `NewArch`
//     subspec) is intentionally not modeled here yet. Add a target for it once RN's SPM
//     autolinking story for codegen stabilizes.
//
// TODO before this can replace the podspec (all blocked on React Native core, not this repo):
//  • Wait for React Native's SPM migration (react-native-community/discussions-and-proposals#587)
//    to expose React/React-Core as a consumable Swift package + SPM autolinking.
//  • Then declare React as a real package/target dependency here.
//  • Model the New Architecture codegen target.
//
// IMPLEMENTED TODAY (Path 1): the podspec consumes the upstream Freshdesk iOS Swift Package
// via the `spm_dependency` helper (RN 0.75+ — this repo now targets 0.75). That keeps CocoaPods
// as the install path; it does NOT make this library installable purely via SPM (that is Path 2,
// blocked on React Native core above).
// ─────────────────────────────────────────────────────────────────────────────

let package = Package(
    name: "FreshdeskReactNative",
    platforms: [
        // Matches the podspec: the vendored Freshdesk iOS SDK requires iOS 15.0+.
        .iOS(.v15)
    ],
    products: [
        .library(
            name: "FreshdeskReactNative",
            targets: ["FreshdeskReactNativeObjC"]
        )
    ],
    targets: [
        // The native Freshdesk iOS SDK, vendored as a binary xcframework.
        // Same artifact the podspec uses as its RN < 0.75 fallback.
        .binaryTarget(
            name: "FreshdeskSDK",
            path: "ios/Frameworks/FreshdeskSDK.xcframework"
        ),

        // Swift half of the bridge. Produces the `FreshdeskReactNative` Swift module
        // (and the generated `FreshdeskReactNative-Swift.h` the Obj-C half imports).
        .target(
            name: "FreshdeskReactNativeSwift",
            dependencies: ["FreshdeskSDK"],
            path: "ios",
            // Don't let this target pick up the binary or the Obj-C sources.
            exclude: [
                "Frameworks",
                "FreshdeskNativeModule.mm",
                "FreshdeskNativeModule.h",
                "generated"
            ],
            // Only the Swift source belongs to this target.
            sources: ["FreshdeskNativeModule.swift"]
            // NOTE: `import React` is resolved by the host React Native build graph.
            // React-Core is not an SPM package yet (RN SPM migration: react-native-community
            // /discussions-and-proposals#587), so this target cannot be built by a bare
            // `swift build`. Do NOT add `unsafeFlags` here — they make the package
            // non-consumable as a dependency. The React dependency must be declared properly
            // once RN ships SPM autolinking.
        ),

        // Objective-C half of the bridge (RCTBridgeModule / RCTEventEmitter glue).
        // Depends on the Swift target to see `FreshdeskReactNative-Swift.h`.
        .target(
            name: "FreshdeskReactNativeObjC",
            dependencies: ["FreshdeskReactNativeSwift"],
            path: "ios",
            exclude: [
                "Frameworks",
                "FreshdeskNativeModule.swift",
                "generated"
            ],
            sources: ["FreshdeskNativeModule.mm"],
            // Expose FreshdeskNativeModule.h to consumers.
            publicHeadersPath: "."
            // NOTE: `<React/RCTBridgeModule.h>` / `<React/RCTEventEmitter.h>` are provided
            // by the host React Native build graph, not by SPM. See the Swift target note.
        )
    ]
)
