import type { PrefsDimension } from '$libs/prefs'; import { unitSystemFromLocales, UNIT_SYSTEMS, type UnitSystem } from '$libs/units'; export interface UnitSystemDimensionOptions { readonly catalog?: readonly UnitSystem[]; readonly default?: UnitSystem; } export function unitSystemDimension( options: UnitSystemDimensionOptions = {} ): PrefsDimension { const catalog = options.catalog ?? UNIT_SYSTEMS; const fallback = options.default ?? 'metric'; return { defaultValue: fallback, validate(value) { if (typeof value !== 'string') return { ok: false, reason: 'unsupported_unit_system' }; if (!catalog.includes(value as UnitSystem)) { return { ok: false, reason: 'unsupported_unit_system' }; } return { ok: true, value: value as UnitSystem }; }, resolve(intent, env) { if (intent !== undefined) return intent; if (env.unitSystem !== undefined && catalog.includes(env.unitSystem)) { return env.unitSystem; } return unitSystemFromLocales(env.locales ?? [], catalog, fallback); }, catalog: () => catalog }; }