"use strict";

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

var _jsxRuntime = require("react/jsx-runtime");

var _hoc = _interopRequireDefault(require("@enact/core/hoc"));

var _react = require("react");

var _ProviderDecorator = require("../ProviderDecorator");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

var defaultConfig = {
  // Each handler receives the event payload, the current props, and an API
  // object with the current state and an update method. This structure
  // mirrors the core/kind handlers block allowing authors to use core/handle.
  handlers: null,
  // Each function receives the current state and props and returns a value to
  // be passed to the wrapped component in the given prop.
  // Altneratively accepts a function which receives the current state and props and
  // returns a props object to merge with the current props.
  mapStateToProps: null
};
var ConsumerDecorator = (0, _hoc["default"])(defaultConfig, function (config, Wrapped) {
  var _class, _temp;

  var handlers = config.handlers,
      mapStateToProps = config.mapStateToProps,
      mount = config.mount;

  var mapper = function mapper() {
    return null;
  };

  if (typeof mapStateToProps === 'function') {
    mapper = mapStateToProps;
  } else if (mapStateToProps) {
    var keys = Object.keys(mapStateToProps).filter(function (key) {
      return typeof mapStateToProps[key] === 'function';
    });

    mapper = function mapper(state, props) {
      return keys.reduce(function (updated, prop) {
        updated[prop] = mapStateToProps[prop](state, props);
        return updated;
      }, {});
    };
  }

  return _temp = _class = /*#__PURE__*/function (_Component) {
    _inherits(_class, _Component);

    var _super = _createSuper(_class);

    function _class() {
      var _this;

      _classCallCheck(this, _class);

      _this = _super.call(this);
      _this.handlers = handlers ? Object.keys(handlers).reduce(function (obj, key) {
        obj[key] = function (ev) {
          handlers[key](ev, _this.props, _this.getContext());
        };

        return obj;
      }, {}) : null;
      return _this;
    }

    _createClass(_class, [{
      key: "componentDidMount",
      value: function componentDidMount() {
        if (mount) {
          this.unmountCallback = mount(this.props, this.getContext());
        }
      }
    }, {
      key: "componentWillUnmount",
      value: function componentWillUnmount() {
        if (this.unmountCallback) {
          this.unmountCallback(this.props, this.getContext());
        }
      }
    }, {
      key: "getContext",
      value: function getContext() {
        return {
          state: this.context.state,
          update: this.context.updateAppState
        };
      }
    }, {
      key: "render",
      value: function render() {
        var props = _objectSpread(_objectSpread(_objectSpread({}, this.props), mapper(this.context.state, this.props)), this.handlers);

        return /*#__PURE__*/(0, _jsxRuntime.jsx)(Wrapped, _objectSpread({}, props));
      }
    }]);

    return _class;
  }(_react.Component), _class.displayName = 'ConsumerDecorator', _class.contextType = _ProviderDecorator.AppContext, _temp;
});
exports.ConsumerDecorator = ConsumerDecorator;
var _default = ConsumerDecorator;
exports["default"] = _default;