'use strict';

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

var _classCallCheck = require('@babel/runtime/helpers/classCallCheck');
var _createClass = require('@babel/runtime/helpers/createClass');
var _callSuper = require('@babel/runtime/helpers/callSuper');
var _inherits = require('@babel/runtime/helpers/inherits');
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-b430da87.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) {
    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 = _callSuper(this, _Class, [].concat(args));
      _defineProperty(_this, "contextState", {});
      return _this;
    }
    _inherits(_Class, _React$Component);
    return _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]
        });
      }
    }]);
  }(React__default["default"].Component), _defineProperty(_Class, "childContextTypes", {
    contextAdapter: PropTypes__default["default"].object
  }), _Class;
};

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