"use strict";

exports.__esModule = true;
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _meetReactComponentOne = require("@alifd/meet-react-component-one");
var _locale = require("../locale");
var _hooks = require("../utils/hooks");
var _render = require("../utils/render");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
var ConfigProvider = /*#__PURE__*/function (_Component) {
  _inheritsLoose(ConfigProvider, _Component);
  function ConfigProvider(props) {
    var _this = _Component.call(this, props) || this;
    _this.state = {
      portals: []
    };
    return _this;
  }
  var _proto = ConfigProvider.prototype;
  _proto.componentDidMount = function componentDidMount() {
    if (!this.props.disablePortal) {
      (0, _render.onProviderMount)(this);
    }
  };
  _proto.componentDidUpdate = function componentDidUpdate(prevProps) {
    if (this.props.disablePortal !== prevProps.disablePortal) {
      if (this.props.disablePortal) {
        (0, _render.onProviderUnMount)();
      } else {
        (0, _render.onProviderMount)(this);
      }
    }
  };
  _proto.componentWillUnmount = function componentWillUnmount() {
    (0, _render.onProviderUnMount)();
  };
  _proto.addPortal = function addPortal(item) {
    var _this2 = this;
    var id = (0, _hooks.guid)('portal-');
    var arr = [].concat(this.state.portals);
    arr.push(Object.assign(Object.assign({}, item), {
      id: id
    }));
    this.setState({
      portals: arr
    });
    return function () {
      var i = _this2.state.portals.find(function (it) {
        return it.id === id;
      });
      if (i) {
        var a = [].concat(_this2.state.portals);
        a.splice(a.indexOf(i), 1);
        _this2.setState({
          portals: a
        });
        if (i.onHide) {
          i.onHide();
        }
      }
    };
  };
  _proto.render = function render() {
    var portals = this.state.portals;
    var _this$props = this.props,
      locale = _this$props.locale,
      children = _this$props.children;
    return /*#__PURE__*/(0, _react.createElement)(_locale.LocaleProvider, {
      locale: locale
    }, /*#__PURE__*/(0, _react.createElement)(_react.Fragment, null, portals.length > 0 && /*#__PURE__*/(0, _react.createElement)(_meetReactComponentOne.Portal, null, portals.map(function (it) {
      return /*#__PURE__*/(0, _react.createElement)(_react.Fragment, {
        key: it.id
      }, it.element);
    })), children));
  };
  return ConfigProvider;
}(_react.Component);
ConfigProvider.Consumer = function (_ref) {
  var children = _ref.children;
  return /*#__PURE__*/(0, _react.createElement)(_locale.Context.Consumer, null, function (_ref2) {
    var locale = _ref2.locale;
    return children ? children({
      locale: locale
    }) : null;
  });
};
var _default = ConfigProvider;
exports.default = _default;