import type { ThemeTokens } from '@helpwave/hightide-design/theme-tokens' import type { SemanticTokenResolvers } from '@helpwave/hightide-design/semantic-token-resolvers' import type { ComponentTokenResolvers, PressableState, PressableStateValue } from '@helpwave/hightide-design/component-token-resolvers' export type InteractionState = { isDisabled?: boolean, isHovered?: boolean, isFocused?: boolean, isFocusVisible?: boolean, isPressed?: boolean, isReadonly?: boolean, isInvalid?: boolean, } export const toPressableInteractionState = ( state: InteractionState = {} ): PressableState => { const active = new Set() if (state.isDisabled) { active.add('disabled') } if (state.isFocused) { active.add('focused') } if (state.isFocusVisible) { active.add('focusVisible') } if (state.isHovered) { active.add('hovered') } if (state.isPressed) { active.add('pressed') } return active } export type StyleOverwrite = TStyle | ((state: TState, prev: TStyle) => TStyle) export type StyleResolverFunction = ( props: TState, overwrite?: StyleOverwrite, ) => TStyle export type SimpleStyleResolver = StyleResolverFunction, TStyle> export type ComponentThemeResolver = (params: { themeTokens: ThemeTokens, semanticTokens: SemanticTokenResolvers, componentTokens: ComponentTokenResolvers, }) => TTheme export const createStyleResolver = ( resolve: (props: TState) => TStyle ): StyleResolverFunction => { return (props, overwrite) => { const base = resolve(props) if (overwrite === undefined) { return base } if (typeof overwrite === 'function') { return (overwrite as (state: TState, prev: TStyle) => TStyle)(props, base) } return [base, overwrite] as TStyle } } export const createSimpleStyleResolver = ( resolve: () => TStyle ): SimpleStyleResolver => { return createStyleResolver, TStyle>(resolve) } export const createValueResolver = ( resolve: (props: TState) => TValue ): StyleResolverFunction => { return (props, overwrite) => { const base = resolve(props) if (overwrite === undefined) { return base } if (typeof overwrite === 'function') { return (overwrite as (state: TState, prev: TValue) => TValue)(props, base) } return overwrite } } export const createSimpleValueResolver = ( resolve: () => TValue ): SimpleStyleResolver => { return createValueResolver, TValue>(resolve) }