"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard")["default"];

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _react = _interopRequireWildcard(require("react"));

var _mobx = require("mobx");

var _mobxReactLite = require("mobx-react-lite");

var _isBoolean = _interopRequireDefault(require("lodash/isBoolean"));

var _FormContext = require("../../pro/lib/form/FormContext");

var _ConfigContext = _interopRequireDefault(require("./ConfigContext"));

var _utils = require("../configure/utils");

var _excluded = ["children"];

var ConfigProvider = function ConfigProvider(props) {
  var _useContext = (0, _react.useContext)(_ConfigContext["default"]),
      getParentConfig = _useContext.getConfig;

  var children = props.children,
      localConfig = (0, _objectWithoutProperties2["default"])(props, _excluded);
  var configStore = (0, _mobxReactLite.useLocalStore)(function (config) {
    return {
      config: config
    };
  }, _mobx.observable.map(localConfig));
  var getLocalConfig = (0, _react.useCallback)(function (key) {
    var localValue = configStore.config.get(key);

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

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

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

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

    if ((0, _isBoolean["default"])(customizable)) {
      return customizable;
    }

    if ((0, _utils.isCustomizable)(customizable)) {
      return customizable[component];
    }
  }, [getLocalConfig]);
  var getLocalUsefulTooltip = (0, _react.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 = (0, _react.useCallback)(function (target) {
    var tooltip = getLocalConfig('tooltip');

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

    return getLocalUsefulTooltip(target) || tooltip;
  }, [getLocalConfig, getLocalUsefulTooltip]);
  var getLocalTooltipTheme = (0, _react.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 = (0, _react.useCallback)(function (target) {
    var tooltipPlacement = getLocalConfig('tooltipPlacement');

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

    return tooltipPlacement;
  }, [getLocalConfig]);
  var value = (0, _react.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["default"].createElement(_ConfigContext["default"].Provider, {
    value: value
  }, /*#__PURE__*/_react["default"].createElement(_FormContext.FormProvider, null, children));
};

ConfigProvider.displayName = 'ConfigProvider';

var _default = /*#__PURE__*/(0, _react.memo)(ConfigProvider);

exports["default"] = _default;
//# sourceMappingURL=ConfigProvider.js.map
