import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["children"];
import React, { memo, useCallback, useContext, useMemo } from 'react';
import { observable } from 'mobx';
import { useLocalStore } from 'mobx-react-lite';
import isBoolean from 'lodash/isBoolean';
import { FormProvider } from '../../pro/es/form/FormContext';
import ConfigContext from './ConfigContext';
import { isCustomizable } from '../configure/utils';

var ConfigProvider = function ConfigProvider(props) {
  var _useContext = useContext(ConfigContext),
      getParentConfig = _useContext.getConfig;

  var children = props.children,
      localConfig = _objectWithoutProperties(props, _excluded);

  var configStore = useLocalStore(function (config) {
    return {
      config: config
    };
  }, observable.map(localConfig));
  var getLocalConfig = useCallback(function (key) {
    var localValue = configStore.config.get(key);

    if (configStore.config.has(key)) {
      return localValue;
    }

    return getParentConfig(key);
  }, [configStore, getParentConfig]);
  var getLocalPrefixCls = useCallback(function (suffixCls, customizePrefixCls) {
    if (customizePrefixCls) {
      return customizePrefixCls;
    }

    return "".concat(getLocalConfig('prefixCls'), "-").concat(suffixCls);
  }, [getLocalConfig]);
  var getLocalProPrefixCls = useCallback(function (suffixCls, customizePrefixCls) {
    if (customizePrefixCls) {
      return customizePrefixCls;
    }

    return "".concat(getLocalConfig('proPrefixCls'), "-").concat(suffixCls);
  }, [getLocalConfig]);
  var getLocalCustomizable = useCallback(function (component) {
    var customizable = getLocalConfig('customizable');

    if (isBoolean(customizable)) {
      return customizable;
    }

    if (isCustomizable(customizable)) {
      return customizable[component];
    }
  }, [getLocalConfig]);
  var getLocalUsefulTooltip = useCallback(function (target) {
    switch (target) {
      case 'table-cell':
        return getLocalConfig('tableColumnTooltip');

      case 'button':
        return getLocalConfig('buttonTooltip');

      case 'select-option':
        return getLocalConfig('selectOptionTooltip');

      case 'label':
        return getLocalConfig('labelTooltip');

      default:
    }
  }, [getLocalConfig]);
  var getLocalTooltip = useCallback(function (target) {
    var tooltip = getLocalConfig('tooltip');

    if (typeof tooltip === 'function') {
      return tooltip(target);
    }

    return getLocalUsefulTooltip(target) || tooltip;
  }, [getLocalConfig, getLocalUsefulTooltip]);
  var getLocalTooltipTheme = useCallback(function (target) {
    var tooltipTheme = getLocalConfig('tooltipTheme');

    if (typeof tooltipTheme === 'function') {
      return tooltipTheme(target);
    }

    if (target === 'validation') {
      var validationTooltipTheme = getLocalConfig('validationTooltipTheme');

      if (validationTooltipTheme) {
        return validationTooltipTheme;
      }
    }

    return tooltipTheme;
  }, [getLocalConfig]);
  var getLocalTooltipPlacement = useCallback(function (target) {
    var tooltipPlacement = getLocalConfig('tooltipPlacement');

    if (typeof tooltipPlacement === 'function') {
      return tooltipPlacement(target);
    }

    return tooltipPlacement;
  }, [getLocalConfig]);
  var value = useMemo(function () {
    return {
      getConfig: getLocalConfig,
      getPrefixCls: getLocalPrefixCls,
      getProPrefixCls: getLocalProPrefixCls,
      getCustomizable: getLocalCustomizable,
      getTooltip: getLocalTooltip,
      getTooltipTheme: getLocalTooltipTheme,
      getTooltipPlacement: getLocalTooltipPlacement
    };
  }, [getLocalConfig, getLocalPrefixCls, getLocalProPrefixCls, getLocalCustomizable, getLocalTooltip, getLocalTooltipTheme, getLocalTooltipPlacement]);
  return /*#__PURE__*/React.createElement(ConfigContext.Provider, {
    value: value
  }, /*#__PURE__*/React.createElement(FormProvider, null, children));
};

ConfigProvider.displayName = 'ConfigProvider';
export default /*#__PURE__*/memo(ConfigProvider);
//# sourceMappingURL=ConfigProvider.js.map
