'use strict';

require('babel-polyfill');

var _hmrClient = require('hmr-client');

var _hmrClient2 = _interopRequireDefault(_hmrClient);

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

var _ignoreWarnings2 = _interopRequireDefault(_ignoreWarnings);

var _log = require('@reactant/core/log');

var _log2 = _interopRequireDefault(_log);

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"); }); }; }

window.document.title = window.reactant.config.title;
(0, _ignoreWarnings2.default)('react-error-overlay is not meant for use in production');

let hash = null;
const config = window.reactant.config;


if (config.options.verbose || config.options.debug || config.level === 'trace') {
  (0, _log.setLevel)('trace');
} else if (config.env === 'development') {
  (0, _log.setLevel)('debug');
} else {
  (0, _log.setLevel)(config.level);
}

var _require = require('react-error-overlay');

const reportBuildError = _require.reportBuildError,
      startReportingRuntimeErrors = _require.startReportingRuntimeErrors;


_log2.default.error(window.reactant.errStack);

startReportingRuntimeErrors({});
reportBuildError(window.reactant.errStack);

_log2.default.trace('connecting . . .');
const client = new _hmrClient2.default({ port: config.ports.dev });
client.onConnected = _asyncToGenerator(function* () {
  _log2.default.trace('connected');
});
client.onHash = (() => {
  var _ref2 = _asyncToGenerator(function* (message) {
    _log2.default.trace('hash', hash);
    if (hash) windowReload();
    hash = message.data;
  });

  return function (_x) {
    return _ref2.apply(this, arguments);
  };
})();
client.onContentChanged = () => {
  _log2.default.trace('content-changed');
  windowReload();
};
client.onClose = () => {
  _log2.default.trace('close');
  _log2.default.debug('The development server has disconnected.\n' + 'Refresh the page if necessary.');
};

function windowReload() {
  if (config.options.debug) {
    _log2.default.trace('reloading window . . .');
  } else {
    setTimeout(() => {
      window.location.reload();
    }, 1000);
  }
}