'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _classCallCheck = require('@babel/runtime/helpers/classCallCheck');
var _createClass = require('@babel/runtime/helpers/createClass');
var _assertThisInitialized = require('@babel/runtime/helpers/assertThisInitialized');
var _inherits = require('@babel/runtime/helpers/inherits');
var _createSuper = require('@babel/runtime/helpers/createSuper');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var PropTypes = require('prop-types');
var React = require('react');
var jsxRuntime = require('react/jsx-runtime');
var ReactNodeView = require('../../dist/ReactNodeView-6d9f9aaa.cjs.dev.js');
require('@babel/runtime/helpers/get');
require('@babel/runtime/helpers/getPrototypeOf');
require('@babel/runtime/helpers/objectSpread2');
require('prosemirror-state');

function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }

var PropTypes__default = /*#__PURE__*/_interopDefault(PropTypes);
var React__default = /*#__PURE__*/_interopDefault(React);

// 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__*/jsxRuntime.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__*/jsxRuntime.jsxs(jsxRuntime.Fragment, {
          children: [consumers, children]
        });
      }
    }]);
    return _class;
  }(React__default["default"].Component), _defineProperty(_class, "childContextTypes", {
    contextAdapter: PropTypes__default["default"].object
  }), _class;
};

exports.ReactNodeView = ReactNodeView.ReactNodeView;
exports.createContextAdapter = createContextAdapter;
