import type { Theme , PartialTheme , ThemeValue } from "./context" ; export const isThemeValue = ( value ) : value is ThemeValue => { return [ "number" , "string" ] . includes ( typeof value ) ; } ; export const reduceTheme = ( prevTheme , nextTheme ) : Theme => { if ( prevTheme === nextTheme ) return prevTheme ; return Object . entries ( nextTheme ) . reduce ( ( memo , [ k , v ] ) => ( isThemeValue ( v ) ? { ... memo , [ k ] : v } : memo ) , prevTheme ) ; } ; export const asCustomProps = ( theme ) : Record < string , ThemeValue > => { return Object . fromEntries ( Object . entries ( flat ( theme ) ) . map ( ( [ k , v ] ) => [ "--" + k , v ] ) ) ; } ; function flat ( theme , prefix = "" ) { return Object . entries ( theme ) . reduce ( ( memo , [ k , v ] ) => Object . assign ( memo , v === Object ( v ) ? flat ( v as PartialTheme , k + "-" ) : { [ prefix + k ] : v } ) , { } ) ; }