'use strict';

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

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _class;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _autobindDecorator = require('autobind-decorator');

var _autobindDecorator2 = _interopRequireDefault(_autobindDecorator);

var _ignoreWarnings = require('ignore-warnings');

var _ignoreWarnings2 = _interopRequireDefault(_ignoreWarnings);

var _core = require('@reactant/core');

var _plugin = require('@reactant/core/plugin');

var _reactDom = require('react-dom');

var _Reactant = require('./Reactant');

var _Reactant2 = _interopRequireDefault(_Reactant);

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

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

let ClientApp = (0, _autobindDecorator2.default)(_class = class ClientApp extends _core.ReactantApp {
  constructor(BaseRoot = _Reactant2.default, options = {}) {
    var _options$container = options.container;
    const container = _options$container === undefined ? document.getElementById('app') : _options$container;

    super(...arguments);
    this.BaseRoot = BaseRoot;
    this.container = container;
    if (!_core.config.options.debug) {
      (0, _ignoreWarnings2.default)(_core.config.ignore.warnings || []);
      (0, _ignoreWarnings2.default)('error', _core.config.ignore.errors || []);
    }
  }

  render() {
    var _this = this;

    return _asyncToGenerator(function* () {
      _this.container = document.getElementById('app');
      yield (0, _plugin.callLifecycle)('willRender', _this, {});
      const props = _this.props;

      props.context = _extends({}, props.context);
      const Root = yield _this.getRoot({});
      if (window.reactant) window.reactant.context = props.context;
      (0, _reactDom.hydrate)(_react2.default.createElement(Root, props), _this.container);
      yield (0, _plugin.callLifecycle)('didRender', _this, {});
    })();
  }

  init() {
    var _this2 = this;

    return _asyncToGenerator(function* () {
      _core.log.debug('local rendering taking over');
      yield _core.ReactantApp.prototype.init.call(_this2);
      if (_core.config.offline) require('offline-plugin/runtime').install();
      if (module.hot) {
        module.hot.accept('@reactant/web-isomorphic/ClientRoot', _this2.render);
      }
      yield _this2.render().catch(_core.log.error);
      return _this2;
    })();
  }
}) || _class;

exports.default = ClientApp;