"use strict";

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

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

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

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));

var _getPrototypeOf3 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));

var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));

var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

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

var _components = require("../../lib/components");

var _Context = _interopRequireDefault(require("./Context"));

/**
 * Factory to create a higher order component with injected props from site
 * @param {string} propName - Prop to inject into component
 * @returns {function} that takes the component to wrap
 */
var siteHocFactory = function siteHocFactory(propName) {
  return function (WrappedComponent) {
    var EnhancedComponent =
    /*#__PURE__*/
    function (_Component) {
      (0, _inherits2.default)(EnhancedComponent, _Component);

      function EnhancedComponent() {
        var _getPrototypeOf2;

        var _this;

        (0, _classCallCheck2.default)(this, EnhancedComponent);

        for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
          args[_key] = arguments[_key];
        }

        _this = (0, _possibleConstructorReturn2.default)(this, (_getPrototypeOf2 = (0, _getPrototypeOf3.default)(EnhancedComponent)).call.apply(_getPrototypeOf2, [this].concat(args)));
        (0, _defineProperty2.default)((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this)), "render", function () {
          return _react.default.createElement(_Context.default.Consumer, null, function (value) {
            if (!value || !value.site) {
              throw new Error('Cannot find <Site> to access with DataLayer');
            }

            return _react.default.createElement(WrappedComponent, (0, _extends2.default)({}, (0, _defineProperty2.default)({}, propName, value[propName]), _this.props));
          });
        });
        return _this;
      }

      return EnhancedComponent;
    }(_react.Component);

    EnhancedComponent.displayName = (0, _components.getDisplayName)(WrappedComponent);
    return EnhancedComponent;
  };
};

exports.siteHocFactory = siteHocFactory;
var _default = siteHocFactory;
exports.default = _default;
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uLy4uL3NyYy9hZGRvbnMvU2l0ZS9zaXRlSG9jRmFjdG9yeS5qcyJdLCJuYW1lcyI6WyJzaXRlSG9jRmFjdG9yeSIsInByb3BOYW1lIiwiV3JhcHBlZENvbXBvbmVudCIsIkVuaGFuY2VkQ29tcG9uZW50IiwidmFsdWUiLCJzaXRlIiwiRXJyb3IiLCJwcm9wcyIsIkNvbXBvbmVudCIsImRpc3BsYXlOYW1lIl0sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7O0FBQUE7O0FBRUE7O0FBQ0E7O0FBRUE7Ozs7O0FBS08sSUFBTUEsY0FBYyxHQUFHLFNBQWpCQSxjQUFpQixDQUFDQyxRQUFEO0FBQUEsU0FBYyxVQUFDQyxnQkFBRCxFQUFzQjtBQUFBLFFBQzFEQyxpQkFEMEQ7QUFBQTtBQUFBO0FBQUE7O0FBQUE7QUFBQTs7QUFBQTs7QUFBQTs7QUFBQTtBQUFBO0FBQUE7O0FBQUE7QUFBQSxtSUFFckQ7QUFBQSxpQkFDUCw2QkFBQyxnQkFBRCxDQUFTLFFBQVQsUUFDSSxVQUFDQyxLQUFELEVBQVc7QUFDWCxnQkFBSSxDQUFDQSxLQUFELElBQVUsQ0FBQ0EsS0FBSyxDQUFDQyxJQUFyQixFQUEyQjtBQUN6QixvQkFBTSxJQUFJQyxLQUFKLENBQVUsNkNBQVYsQ0FBTjtBQUNEOztBQUVELG1CQUNFLDZCQUFDLGdCQUFELCtEQUNVTCxRQURWLEVBQ3FCR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