import Combine
import Dependencies

// Follows the TCA protocol dependency pattern for not having to link
// our telemetry service client directly at the call site.
// See https://github.com/suno-ai/app-ios/pull/1410#issuecomment-2712244993

public protocol TelemetryClientProtocol {
    func record(error: Error, message: String?)
    func record(message: String)

    func trace(_ named: String) -> AnyCancellable
}

private struct NoOpTelemetryClient: TelemetryClientProtocol {
    func record(error _: Error, message _: String?) {
        // no-op
    }

    func record(message _: String) {
        // no-op
    }

    /// Use the overload in `FirebaseClient` to enforce uniqueness
    func trace(_: String) -> AnyCancellable {
        AnyCancellable {
            // no-op
        }
    }
}

private enum TelemetryClientKey: DependencyKey {
    static let liveValue: TelemetryClientProtocol = NoOpTelemetryClient()
    static let previewValue: TelemetryClientProtocol = NoOpTelemetryClient()
    static let testValue: TelemetryClientProtocol = NoOpTelemetryClient()
}

public extension DependencyValues {
    var telemetryClient: TelemetryClientProtocol {
        get { self[TelemetryClientKey.self] }
        set { self[TelemetryClientKey.self] = newValue }
    }
}
