doxa-maps/library/composables/useMapProvider.js

49 lines
1.9 KiB
JavaScript

/**
* 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)
}