import { useContext, createContext, useMemo } from 'react';
import { useMediaQuery } from '../hooks/useMediaQuery.mjs';
import { defaultTheme } from './themes/themes.mjs';
import { RUN_IN_DEV } from '../../../../core/src/utils/dev.mjs';
import { logger } from '../../../../core/src/utils/logger.mjs';

const defaultOptions = {
    hideHeaderText: false
};
function usePresentationWithDefault(maybeConfig, displayWatermark, { products, enableShadowDOM }, productsName) {
    const { theme, options } = maybeConfig ?? {};
    RUN_IN_DEV(()=>{
        const stringProducts = products.filter((p)=>typeof p === 'string');
        if (stringProducts.length > 0) {
            logger.error(`Please add an import for ${productsName} and update config.products to\n` + 'products: [' + products.map((p)=>`${productsName}.${typeof p === 'string' ? p : p.id}`).join(', ') + ']');
            throw new Error("'config.products' should not include strings anymore");
        }
    });
    // Switch theme automatically depending on color scheme
    const isDynamic = isDynamicTheme(theme);
    const darkMode = useMediaQuery(isDynamic ? '(prefers-color-scheme: dark)' : undefined);
    let effectiveTheme;
    if (isDynamic) {
        effectiveTheme = darkMode ? theme[1] : theme[0];
    } else {
        effectiveTheme = theme;
    }
    // Memoize the icon registry so it only need to be constructed once
    const iconRegistry = useMemo(()=>{
        const registry = {};
        for (const product of products){
            Object.assign(registry, product.icons);
        }
        Object.assign(registry, options?.icons);
        return registry;
    }, [
        products,
        options
    ]);
    return {
        theme: {
            ...defaultTheme,
            ...effectiveTheme
        },
        options: {
            enableShadowDOM,
            ...defaultOptions,
            ...options
        },
        displayWatermark,
        iconRegistry
    };
}
function isDynamicTheme(theme) {
    return Array.isArray(theme);
}
const PresentationContext = createContext(undefined);
const usePresentation = ()=>useContext(PresentationContext);
function getButtonId(base, options) {
    if (options.buttonId == null || options.buttonId === false) return undefined;
    if (typeof options.buttonId === 'string') return base + options.buttonId;
    return base;
}

export { PresentationContext, getButtonId, isDynamicTheme, usePresentation, usePresentationWithDefault };
//# sourceMappingURL=PresentationConfig.mjs.map
