import Foundation

// Server capability gate. The phone reads `metadata.config.features
// .halflight.tapestry` (or `metadata.halflight.features.tapestry`) from
// nodeinfo and persists per-host. The watch needs the same gate before
// mounting the tapestry view; it lives on a parallel cache (App Group,
// see Generated/Branding.swift::appGroupId) so a cold launch can render
// the empty-state without a network round-trip.

extension WatchAPIClient {
    func fetchSupportsTapestry() async -> Bool {
        guard let url = Endpoints.nodeinfo(instance) else { return false }
        do {
            let (info, _) = try await request(url, decode: NodeinfoResponse.self)
            let viaConfig = info.metadata?.config?.features?.halflight?.tapestry
            let viaTop = info.metadata?.halflight?.features?.tapestry
            return (viaConfig ?? viaTop) ?? false
        } catch {
            return false
        }
    }
}
