/** * useMapProvider.js — resolve the active map engine ("provider"). * * Resolution order (first defined wins): * 1. profile-config `provider` — per-map, set in the profile-config JSON * 2. window.MAP_PROVIDER — runtime override (e.g. a host reads ?provider=…) * 3. 'mapbox' — default; existing behaviour, never regresses * * Valid values: 'mapbox' | 'maplibre'. Anything else falls back to the default. */ import { inject } from 'vue' export const MAP_PROVIDERS = ['mapbox', 'maplibre'] export const DEFAULT_PROVIDER = 'mapbox' /** Unwrap a ref/computed to its config object; pass plain objects through. */ function unwrapConfig(profileConfig) { if (profileConfig && typeof profileConfig === 'object' && 'value' in profileConfig && !('provider' in profileConfig) && !('profile' in profileConfig)) { return profileConfig.value } return profileConfig } /** * Resolve the provider from a profile-config (object or ref) + window fallback. * Pure — safe to call outside a Vue setup(). */ export function resolveMapProvider(profileConfig) { const cfg = unwrapConfig(profileConfig) const fromConfig = cfg?.provider const fromUrl = typeof location !== 'undefined' ? new URLSearchParams(location.search).get('provider') : undefined const fromWindow = typeof window !== 'undefined' ? window.MAP_PROVIDER : undefined const raw = String(fromConfig || fromUrl || fromWindow || DEFAULT_PROVIDER).toLowerCase().trim() return MAP_PROVIDERS.includes(raw) ? raw : DEFAULT_PROVIDER } /** Human-readable engine label for a badge / UI. */ export function providerLabel(provider) { return provider === 'maplibre' ? 'MapLibre' : 'Mapbox' } /** Convenience for use inside a Vue setup() — injects the provided profileConfig. */ export function useMapProvider() { const profileConfig = inject('profileConfig', null) return resolveMapProvider(profileConfig) } /** True when this map instance is a Mapbox GL map (vs MapLibre). Per-instance runtime * sniff — works even on dual-engine pages where both globals are loaded. */ export function isMapboxMap(map) { return typeof window !== 'undefined' && !!window.mapboxgl && map instanceof window.mapboxgl.Map } /** Mapbox-Standard-only lighting paint props, per engine. MapLibre rejects layers whose * paint carries unknown properties (the whole layer is dropped → invisible pins), so * spread this instead of writing 'circle-emissive-strength' literally. */ export function emissivePaint(map) { return isMapboxMap(map) ? { 'circle-emissive-strength': 1 } : {} }