import _classCallCheck from '@babel/runtime/helpers/esm/classCallCheck';
import _createClass from '@babel/runtime/helpers/esm/createClass';
import _assertThisInitialized from '@babel/runtime/helpers/esm/assertThisInitialized';
import _inherits from '@babel/runtime/helpers/esm/inherits';
import _createSuper from '@babel/runtime/helpers/esm/createSuper';
import _defineProperty from '@babel/runtime/helpers/esm/defineProperty';
import PropTypes__default from 'prop-types';
import React__default from 'react';
import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
export { R as ReactNodeView } from '../../dist/ReactNodeView-100b47fb.esm.js';
import '@babel/runtime/helpers/get';
import '@babel/runtime/helpers/getPrototypeOf';
import '@babel/runtime/helpers/objectSpread2';
import 'prosemirror-state';

// injects contexts via old context API to children
// and gives access to the original Provider so that
// the child can re-emit it
var createContextAdapter = function createContextAdapter(_createContextAdapter) {
  var _class;
  return _class = /*#__PURE__*/function (_React$Component) {
    _inherits(_class, _React$Component);
    var _super = _createSuper(_class);
    function _class() {
      var _this;
      _classCallCheck(this, _class);
      for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
        args[_key] = arguments[_key];
      }
      _this = _super.call.apply(_super, [this].concat(args));
      _defineProperty(_assertThisInitialized(_this), "contextState", {});
      return _this;
    }
    _createClass(_class, [{
      key: "getChildContext",
      value: function getChildContext() {
        return {
          contextAdapter: this.zipProvidersWithValues()
        };
      }
    }, {
      key: "zipProvidersWithValues",
      value: function zipProvidersWithValues() {
        var _this2 = this;
        return Object.keys(_createContextAdapter).reduce(function (zipped, name) {
          zipped[name] = {
            Provider: _createContextAdapter[name].Provider,
            Consumer: _createContextAdapter[name].Consumer,
            value: _this2.contextState[name]
          };
          return zipped;
        }, {});
      }
    }, {
      key: "render",
      value: function render() {
        var _this3 = this;
        var children = this.props.children;

        // render all the consumers, and react to their value changes independently
        var consumers = Object.keys(_createContextAdapter).map(function (name, idx) {
          var Consumer = _createContextAdapter[name].Consumer;
          return /*#__PURE__*/jsx(Consumer, {
            children: function children(value) {
              // update local copy of value provided from Consumer
              if (_this3.contextState[name] !== value) {
                _this3.contextState[name] = value;
                _this3.forceUpdate();
              }
              return null;
            }
          }, idx);
        });
        return /*#__PURE__*/jsxs(Fragment, {
          children: [consumers, children]
        });
      }
    }]);
    return _class;
  }(React__default.Component), _defineProperty(_class, "childContextTypes", {
    contextAdapter: PropTypes__default.object
  }), _class;
};

export { createContextAdapter };
