import { getPx, th as xth, themeGetter } from '@xstyled/styled-components';
import { useMemo } from 'react';
import { useTheme as useStyledTheme } from 'styled-components';
import defaultTheme from '../theme';
import { merge, runIfFn } from '../utils';
/** Merges provided theme extensions and overrides with default VUI theme. */
export function extendTheme(extensions) {
    return merge(defaultTheme, extensions);
}
/** Removes specific theming keys from the given props. */
export function omitThemingProps(props) {
    const { size, variant, ...rest } = props; // eslint-disable-line @typescript-eslint/no-unused-vars
    return rest;
}
/** Re-export of xstyled 'th' with custom typing and screen getter. */
const th = xth;
th.screen = themeGetter({
    name: 'screen',
    key: 'screens',
    compose: getPx,
});
export { th };
/**
 * Combines baseStyle, size and variant styling from a component's theme configuration.
 * @param component - name of any component configured in the theme
 * @param props - props object of the given component
 */
export function useStyleConfig(component, props = {}) {
    const theme = useTheme();
    const { baseStyle, defaultProps, parts = [], sizes, variants } = theme?.components[component] ?? {};
    const mergedProps = {
        ...props,
        colorScheme: props?.colorScheme ?? defaultProps?.colorScheme,
        size: props?.size ?? defaultProps?.size,
        variant: props?.variant ?? defaultProps?.variant,
    };
    const { colorScheme, size, variant } = mergedProps;
    return useMemo(() => {
        const baseStyles = runIfFn(baseStyle, mergedProps) ?? {};
        const sizeStyles = runIfFn(sizes?.[size], mergedProps) ?? {};
        const variantStyles = runIfFn(variants?.[variant], mergedProps) ?? {};
        let styles = {};
        if (parts.length > 0)
            parts.forEach((part) => {
                styles[part] = merge(baseStyles[part] ?? {}, sizeStyles[part] ?? {}, variantStyles[part] ?? {});
            });
        else
            styles = merge(baseStyles, sizeStyles, variantStyles);
        return styles;
    }, [colorScheme, size, variant]);
}
/** Returns theme value at the given path. */
export function useTh(path, defaultValue) {
    const theme = useTheme();
    return th(path, defaultValue)({ theme });
}
/** Returns the theme object with extensions. */
export function useTheme() {
    // eslint-disable-next-line @typescript-eslint/ban-ts-comment
    // @ts-ignore
    return useStyledTheme();
}
//# sourceMappingURL=theme.js.map