"use strict";

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

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

var _autobindDecorator = _interopRequireDefault(require("autobind-decorator"));

var _ignoreWarnings = _interopRequireDefault(require("ignore-warnings"));

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

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

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

var _Reactant = _interopRequireDefault(require("./Reactant"));

var _class;

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

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, 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 asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

let ClientApp = (0, _autobindDecorator.default)(_class = class ClientApp extends _core.ReactantApp {
  constructor(BaseRoot = _Reactant.default, options = {}) {
    const _options$container = options.container,
          container = _options$container === void 0 ? document.getElementById('app') : _options$container;
    super(...arguments);
    this.BaseRoot = BaseRoot;
    this.container = container;

    if (!_core.config.options.debug) {
      (0, _ignoreWarnings.default)(_core.config.ignore.warnings || []);
      (0, _ignoreWarnings.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 = _objectSpread({}, props.context);
      const Root = yield _this.getRoot({});
      if (window.reactant) window.reactant.context = props.context;
      (0, _reactDom.hydrate)(_react.default.createElement(Root, props), _this.container);
      yield (0, _plugin.callLifecycle)('didRender', _this, {});
    })();
  }

  init() {
    var _this2 = this,
        _superprop_callInit = (..._args) => super.init(..._args);

    return _asyncToGenerator(function* () {
      _core.log.debug('local rendering taking over');

      yield _superprop_callInit();
      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;