import type { ImageSourcePropType, ImageURISource } from 'react-native'; import type { SurfaceImageSource } from '../../../types/image'; /*** Derives a stable runtime key for one Surface image source. */ export function getSourceKey(source: SurfaceImageSource | null | undefined): string | null { if (source === null || source === undefined) return null; if (typeof source === 'string') return `uri:${source}`; if (typeof source === 'number') return `asset:${source}`; if (isImageUriSourceList(source)) { const parts = source .map((item) => item.uri) .filter((value): value is string => typeof value === 'string'); return `uri-set:${parts.join('|')}`; } const uri = getUriFromSource(source); if (uri) return `uri:${uri}`; try { return `src:${JSON.stringify(source)}`; } catch { return 'src:unknown'; } } /*** Reads the first URI represented by a React Native image source. */ function getUriFromSource(source: ImageSourcePropType): string | undefined { if (isImageUriSourceList(source)) return source[0]?.uri; if (typeof source === 'number') return undefined; return isImageUriSource(source) ? source.uri : undefined; } /*** Narrows a React Native image source to one URI-bearing source object. */ function isImageUriSource(source: ImageSourcePropType): source is ImageURISource { return typeof source === 'object' && 'uri' in source; } /*** Narrows a React Native image source to the readonly multi-source form. */ function isImageUriSourceList(source: ImageSourcePropType): source is ImageURISource[] { return Array.isArray(source); }