"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Flux = exports.Component = exports.Provider = undefined;

var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

exports.dispatchable = dispatchable;
exports.createRenderer = createRenderer;
exports.withFlux = withFlux;

var _react = require("react");

var _react2 = _interopRequireDefault(_react);

var _events = require("events");

var _promisedReducer = require("promised-reducer");

var _promisedReducer2 = _interopRequireDefault(_promisedReducer);

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

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

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } // context


var SharedTypes = {
  dispatch: _react2.default.PropTypes.any,
  rootProps: _react2.default.PropTypes.any
};

var Provider = exports.Provider = function (_React$Component) {
  _inherits(Provider, _React$Component);

  function Provider() {
    _classCallCheck(this, Provider);

    return _possibleConstructorReturn(this, (Provider.__proto__ || Object.getPrototypeOf(Provider)).apply(this, arguments));
  }

  _createClass(Provider, [{
    key: "getChildContext",
    value: function getChildContext() {
      return {
        dispatch: this.props.emitter.emit.bind(this.props.emitter),
        rootProps: this.props
      };
    }
  }, {
    key: "render",
    value: function render() {
      return this.props.children;
    }
  }], [{
    key: "childContextTypes",
    get: function get() {
      return SharedTypes;
    }
  }]);

  return Provider;
}(_react2.default.Component);

var Component = exports.Component = function (_React$Component2) {
  _inherits(Component, _React$Component2);

  function Component() {
    _classCallCheck(this, Component);

    return _possibleConstructorReturn(this, (Component.__proto__ || Object.getPrototypeOf(Component)).apply(this, arguments));
  }

  _createClass(Component, [{
    key: "dispatch",
    value: function dispatch() {
      var _context;

      return (_context = this.context).dispatch.apply(_context, arguments);
    }
  }], [{
    key: "contextTypes",
    get: function get() {
      return SharedTypes;
    }
  }]);

  return Component;
}(_react2.default.Component);

function dispatchable(Wrapped) {
  Wrapped.contextTypes = SharedTypes;
  return Wrapped;
}

function createRenderer(_ref) {
  var emitter = _ref.emitter,
      render = _ref.render;

  return function (el) {
    return render(_react2.default.createElement(
      Provider,
      { emitter: emitter },
      el
    ));
  };
}

var Flux = exports.Flux = function (_PromisedReducer) {
  _inherits(Flux, _PromisedReducer);

  function Flux(_ref2) {
    var renderer = _ref2.renderer,
        initialState = _ref2.initialState,
        middlewares = _ref2.middlewares;

    _classCallCheck(this, Flux);

    var _this3 = _possibleConstructorReturn(this, (Flux.__proto__ || Object.getPrototypeOf(Flux)).call(this, initialState, middlewares));

    _this3._renderer = createRenderer({ emitter: _this3, render: renderer });
    _this3._renderedElement = null;

    _this3.on(":update", function () {
      _this3._renderedElement = _this3._renderer(_this3.render(_this3.state));
    });
    _this3.subscribe();
    return _this3;
  }

  _createClass(Flux, [{
    key: "_setState",
    value: function _setState() {
      if (this._renderedElement) {
        var _renderedElement;

        (_renderedElement = this._renderedElement).setState.apply(_renderedElement, arguments);
      }
    }
  }, {
    key: "subscribe",
    value: function subscribe() {
      // override me
    }
  }]);

  return Flux;
}(_promisedReducer2.default);

function withFlux(subscriber) {
  var initialState = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

  var pr = new _promisedReducer2.default(initialState);
  return function (Wrapped) {
    return function (_React$Component3) {
      _inherits(WithFlux, _React$Component3);

      _createClass(WithFlux, [{
        key: "getChildContext",
        value: function getChildContext() {
          var rootProps = Object.assign({}, this.props, this.state);
          return {
            dispatch: pr.emit.bind(pr),
            rootProps: rootProps
          };
        }
      }], [{
        key: "childContextTypes",
        get: function get() {
          return SharedTypes;
        }
      }]);

      function WithFlux(props) {
        _classCallCheck(this, WithFlux);

        var _this4 = _possibleConstructorReturn(this, (WithFlux.__proto__ || Object.getPrototypeOf(WithFlux)).call(this, props));

        _this4.state = initialState;
        subscriber(pr.update.bind(pr), pr.on.bind(pr));
        pr.on(":update", function () {
          _this4.setState(pr.state);
        });
        return _this4;
      }

      _createClass(WithFlux, [{
        key: "componentWillUnmount",
        value: function componentWillUnmount() {
          pr.removeAllListeners();
        }
      }, {
        key: "render",
        value: function render() {
          return _react2.default.createElement(Wrapped, Object.assign({}, this.props, this.state));
        }
      }]);

      return WithFlux;
    }(_react2.default.Component);
  };
}