import _objectSpread from"@babel/runtime/helpers/objectSpread2";import _objectWithoutProperties from"@babel/runtime/helpers/objectWithoutProperties";import _slicedToArray from"@babel/runtime/helpers/slicedToArray";var _excluded=["children","schemeTarget"];import{createScopedElement}from"../../lib/jsxRuntime";import*as React from"react";import{canUseDOM,useDOM}from"../../lib/dom";import{ConfigProviderContext,defaultConfigProviderProps,Scheme}from"./ConfigProviderContext";import{VKCOM}from"../../lib/platform";import{useIsomorphicLayoutEffect}from"../../lib/useIsomorphicLayoutEffect";import{useObjectMemo}from"../../hooks/useObjectMemo";import{noop}from"../../lib/utils";import{warnOnce}from"../../lib/warnOnce";var warn=warnOnce("ConfigProvider");function useSchemeDetector(r,e){function t(){if(o&&canUseDOM)return r.getAttribute("scheme")}var o="inherit"===e,c=React.useState(t()),n=_slicedToArray(c,2),c=n[0],i=n[1];return React.useEffect(function(){if(!o)return noop;i(t());var e=new MutationObserver(function(){return i(t())});return e.observe(r,{attributes:!0,attributeFilter:["scheme"]}),function(){return e.disconnect()}},[o]),"inherit"===e?c:e}var deriveAppearance=function(e){return e===Scheme.SPACE_GRAY||e===Scheme.VKCOM_DARK?"dark":"light"};function normalizeScheme(e,r){if("inherit"===e)return e;if(e===Scheme.VKCOM)return"development"===process.env.NODE_ENV&&warn('Схема "'.concat(Scheme.VKCOM,'" устарела и будет удалена 5.0.0. Вместо неё используйте "').concat(Scheme.VKCOM_LIGHT,'"')),Scheme.VKCOM_LIGHT;if(r===VKCOM&&(e===Scheme.BRIGHT_LIGHT||e===Scheme.SPACE_GRAY))return"development"===process.env.NODE_ENV&&warn('Платформа "vkcom" и схема "'.concat(e,'" несовместимы. С этой платформой можно использовать схемы "').concat(Scheme.VKCOM_LIGHT,'" или "').concat(Scheme.VKCOM_DARK,'"')),Scheme.VKCOM_LIGHT;switch(e){case Scheme.DEPRECATED_CLIENT_LIGHT:return Scheme.BRIGHT_LIGHT;case Scheme.DEPRECATED_CLIENT_DARK:return Scheme.SPACE_GRAY;default:return e}}var ConfigProvider=function(e){var r=e.children,t=e.schemeTarget,o=_objectWithoutProperties(e,_excluded),c=normalizeScheme(o.scheme,o.platform),e=useDOM().document,n=t||(null==e?void 0:e.body);useIsomorphicLayoutEffect(function(){return"inherit"===c?noop:("development"===process.env.NODE_ENV&&n.hasAttribute("scheme")&&warn('<body scheme> was set before VKUI mount - did you forget scheme="inherit"?'),n.setAttribute("scheme",c),function(){return n.removeAttribute("scheme")})},[c]);e=useSchemeDetector(n,c),o=useObjectMemo(_objectSpread({appearance:deriveAppearance(e)},o));return createScopedElement(ConfigProviderContext.Provider,{value:o},r)};ConfigProvider.defaultProps=_objectSpread({},defaultConfigProviderProps);export default ConfigProvider;