'use strict';

let handleSuccess = (() => {
  var _ref5 = _asyncToGenerator(function* () {
    clearErrors();
    const isHotUpdate = !isFirstCompilation;
    try {
      if (isHotUpdate) {
        yield applyUpdates();
        (0, _reactErrorOverlay.dismissBuildError)();
      }
    } catch (err) {
      (0, _reactErrorOverlay.reportBuildError)(err.stack);
      _core.log.error(err);
      hadError = true;
    }
    isFirstCompilation = false;
  });

  return function handleSuccess() {
    return _ref5.apply(this, arguments);
  };
})();

let handleErrors = (() => {
  var _ref6 = _asyncToGenerator(function* (errors) {
    clearErrors();
    const formatted = (0, _formatWebpackMessages2.default)({ errors, warnings: [] });
    (0, _reactErrorOverlay.reportBuildError)(formatted.errors[0]);
    _lodash2.default.each(formatted.errors, function (error, index) {
      if (index < formatted.errors.length) {
        if (index === 5) {
          _core.log.error('There were more errors in other files.\n' + 'You can find a complete log in the terminal.');
        }
        _core.log.error((0, _stripAnsi2.default)(error));
        return true;
      }
      return false;
    });
    hadServerError = true;
    hadError = true;
    isFirstCompilation = false;
  });

  return function handleErrors(_x2) {
    return _ref6.apply(this, arguments);
  };
})();

let handleWarnings = (() => {
  var _ref7 = _asyncToGenerator(function* (warnings) {
    clearErrors();
    const isHotUpdate = !isFirstCompilation;
    function printWarnings() {
      const formatted = (0, _formatWebpackMessages2.default)({ warnings, errors: [] });
      _lodash2.default.each(formatted.warnings, (warning, index) => {
        if (index < formatted.warnings.length) {
          if (index === 5) {
            _core.log.warn('There were more warnings in other files.\n' + 'You can find a complete log in the terminal.');
          }
          _core.log.warn((0, _stripAnsi2.default)(warning));
        }
      });
    }
    printWarnings();
    try {
      if (isHotUpdate) {
        yield applyUpdates();
        (0, _reactErrorOverlay.dismissBuildError)();
      }
    } catch (err) {
      (0, _reactErrorOverlay.reportBuildError)(err.stack);
      _core.log.error(err);
      hadError = true;
    }
    isFirstCompilation = false;
  });

  return function handleWarnings(_x3) {
    return _ref7.apply(this, arguments);
  };
})();

let applyUpdates = (() => {
  var _ref8 = _asyncToGenerator(function* () {
    if (!module.hot) return windowReload();
    if (!isUpdateAvailable() || module.hot.status() !== 'idle') return false;
    return new Promise(function (resolve, reject) {
      function handleApplyUpdates(err, _updatedModules) {
        if (err && !hadServerError) return reject(err);
        if (hadServerError) hadServerError = false;
        if (isUpdateAvailable()) applyUpdates().then(() => resolve());
        return resolve();
      }
      const result = module.hot.check(true, handleApplyUpdates);
      if (result && result.then) {
        return result.then(function (updatedModules) {
          return handleApplyUpdates(null, updatedModules);
        }).catch(function (err) {
          return handleApplyUpdates(err, null);
        });
      }
      return result;
    });
  });

  return function applyUpdates() {
    return _ref8.apply(this, arguments);
  };
})();

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

var _hmrClient2 = _interopRequireDefault(_hmrClient);

var _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

var _formatWebpackMessages = require('react-dev-utils/formatWebpackMessages');

var _formatWebpackMessages2 = _interopRequireDefault(_formatWebpackMessages);

var _stripAnsi = require('strip-ansi');

var _stripAnsi2 = _interopRequireDefault(_stripAnsi);

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

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

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

if (_core.config.options.debug) window.module = module;

let hadServerError = false;
let hadError = false;
let hash = null;
let isFirstCompilation = true;

(0, _reactErrorOverlay.startReportingRuntimeErrors)({
  onError: () => {
    hadError = true;
    return true;
  },
  filename: `${_core.config.paths.distWebPublic}/scripts/bundle.js`
});
if (module.hot && _lodash2.default.isFunction(module.hot.dispose)) {
  module.hot.dispose(() => (0, _reactErrorOverlay.stopReportingRuntimeErrors)());
}

const client = new _hmrClient2.default({ port: _core.config.ports.dev });
client.onConnected = _asyncToGenerator(function* () {
  _core.log.silly('connected');
});
client.onHash = (() => {
  var _ref2 = _asyncToGenerator(function* (message) {
    if (hash) _core.log.silly('hot reloading');
    _core.log.silly('hash', hash);
    hash = message.data;
  });

  return function (_x) {
    return _ref2.apply(this, arguments);
  };
})();
client.onStillOk = _asyncToGenerator(function* () {
  _core.log.silly('still-ok');
});
client.onOk = _asyncToGenerator(function* () {
  _core.log.silly('ok');
  yield handleSuccess();
});
client.onContentChanged = () => {
  _core.log.silly('content-changed');
  windowReload();
};
client.onWarngins = message => {
  _core.log.silly('warnings');
  handleWarnings(message.data);
};
client.onErrors = message => {
  _core.log.silly('errors');
  handleErrors(message.data);
};
client.onClose = () => {
  _core.log.silly('close');
  _core.log.debug('The development server has disconnected.\n' + 'Refresh the page if necessary.');
};

window.hothot = module.hot;

function isUpdateAvailable() {
  // eslint-disable-next-line camelcase,no-undef
  return hash !== __webpack_hash__;
}

function clearErrors() {
  if (module.hot.status() === 'fail') {
    windowReload();
  } else if (hadError) {
    hadError = false;
    consoleClear();
    (0, _reactErrorOverlay.dismissBuildError)();
  }
}

function windowReload() {
  if (_core.config.options.debug) {
    _core.log.debug('reloading window');
  } else {
    window.location.reload();
  }
}

function consoleClear() {
  if (_core.config.options.debug) {
    _core.log.debug('cleared console');
  } else {
    // eslint-disable-next-line no-console
    console.clear();
  }
}