"use strict";

require("@babel/polyfill");

var _hmrClient = _interopRequireDefault(require("hmr-client"));

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

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

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: 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); }); }; }

window.document.title = window.reactant.config.title;
(0, _ignoreWarnings.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);
}

const _require = require('react-error-overlay'),
      reportBuildError = _require.reportBuildError,
      startReportingRuntimeErrors = _require.startReportingRuntimeErrors;

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

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

_log.default.trace('connecting . . .');

const client = new _hmrClient.default({
  port: config.ports.dev
});
client.onConnected =
/*#__PURE__*/
_asyncToGenerator(function* () {
  _log.default.trace('connected');
});

client.onHash =
/*#__PURE__*/
function () {
  var _ref2 = _asyncToGenerator(function* (message) {
    _log.default.trace('hash', hash);

    if (hash) windowReload();
    hash = message.data;
  });

  return function (_x) {
    return _ref2.apply(this, arguments);
  };
}();

client.onContentChanged = () => {
  _log.default.trace('content-changed');

  windowReload();
};

client.onClose = () => {
  _log.default.trace('close');

  _log.default.debug('The development server has disconnected.\n' + 'Refresh the page if necessary.');
};

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