import * as React from 'react';
import ThemeContext from '../useTheme/ThemeContext.mjs';
import nested from './nested.mjs';
import { jsx } from 'react/jsx-runtime';
import useTheme from '../useTheme/useTheme.mjs';

function mergeOuterLocalTheme(outerTheme, localTheme) {
  if (typeof localTheme === 'function') {
    const mergedTheme = localTheme(outerTheme);
    return mergedTheme;
  }
  return {
    ...outerTheme,
    ...localTheme
  };
}

/**
 * This component takes a `theme` prop.
 * It makes the `theme` available down the React tree thanks to React context.
 * This component should preferably be used at **the root of your component tree**.
 */
function ThemeProvider$2(props) {
  const {
    children,
    theme: localTheme
  } = props;
  const outerTheme = useTheme();
  const theme = React.useMemo(() => {
    const output = outerTheme === null ? {
      ...localTheme
    } : mergeOuterLocalTheme(outerTheme, localTheme);
    if (output != null) {
      output[nested] = outerTheme !== null;
    }
    return output;
  }, [localTheme, outerTheme]);
  return /*#__PURE__*/jsx(ThemeContext.Provider, {
    value: theme,
    children: children
  });
}

export { ThemeProvider$2 as default };
//# sourceMappingURL=ThemeProvider.mjs.map
