import { DEFAULT_PRESET, applyThemeOverrideWithRuntime, type ApplyThemeOverrideOptions, type ApplyThemeOverrideResult, type UpdateCssVariables, } from './applyThemeOverrideRuntime'; import type { Platform } from './types'; export { DEFAULT_PRESET }; export type { ApplyThemeOverrideOptions, ApplyThemeOverrideResult, UpdateCssVariables, } from './applyThemeOverrideRuntime'; interface CommonJsModule { readonly require?: (id: string) => unknown; } function getCommonJsLoader(): ((id: string) => unknown) | undefined { if (typeof module === 'undefined') return undefined; const commonJsModule = module as CommonJsModule; const { require: load } = commonJsModule; return typeof load === 'function' ? load.bind(commonJsModule) : undefined; } function loadCommonJsPeer(id: string): unknown { const load = getCommonJsLoader(); if (!load) return undefined; try { return load(id); } catch { return undefined; } } function detectPlatform(): Platform { const reactNative = loadCommonJsPeer('react-native') as | { readonly Platform?: { readonly OS?: string; }; } | undefined; const os = reactNative?.Platform?.OS; return os === 'ios' || os === 'android' ? os : 'web'; } function getUpdateCssVariables(): UpdateCssVariables { const uniwind = loadCommonJsPeer('uniwind') as | { readonly Uniwind?: { readonly updateCSSVariables?: UpdateCssVariables; }; } | undefined; const uniwindRuntime = uniwind?.Uniwind; const updateCSSVariables = uniwindRuntime?.updateCSSVariables; if (typeof updateCSSVariables !== 'function') { throw new Error( 'applyThemeOverride could not access the optional "uniwind" peer from this module format. ' + 'Use the browser or react-native package condition, or pass updateCSSVariables in the options.', ); } return updateCSSVariables.bind(uniwindRuntime); } /** Parse, expand, validate, and sparsely apply one target ThemeOverride. */ export function applyThemeOverride( override: unknown, options: ApplyThemeOverrideOptions = {}, ): ApplyThemeOverrideResult { return applyThemeOverrideWithRuntime(override, { runtimeMap: options.runtimeMap, runtimePlatform: options.runtimePlatform ?? detectPlatform(), getUpdateCSSVariables: () => options.updateCSSVariables ?? getUpdateCssVariables(), }); }