"use strict";

var _startsWith2 = require("lodash/startsWith");

var _startsWith3 = _interopRequireDefault(_startsWith2);

var _filter2 = require("lodash/filter");

var _filter3 = _interopRequireDefault(_filter2);

var _get2 = require("lodash/get");

var _get3 = _interopRequireDefault(_get2);

var _react = require("react");

var _react2 = _interopRequireDefault(_react);

var _html = require("./components/html");

var _html2 = _interopRequireDefault(_html);

var _server = require("react-dom/server");

var _server2 = _interopRequireDefault(_server);

var _workboxSw = require("./libs/workbox-sw");

var _workboxSw2 = _interopRequireDefault(_workboxSw);

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

// As service worker has special scope for self lets store it to variable
// eslint-disable-next-line
var serviceWorker = self;

// ASSETS variable is inserted by dev.server.js & server.js on fly.

// let WEB_ASSETS = serviceWorker.ASSETS || [];
var _GLOBALS = {};

self.addEventListener("install", function (event) {
  event.waitUntil(fetch("/_globals").then(function (res) {
    return res.json();
  }).then(function (data) {
    _GLOBALS = data;
    return Promise.resolve();
  }));
});

var getOfflineHtml = function getOfflineHtml() {
  var allCss = (0, _get3.default)(_GLOBALS, "allCss", []);
  var allJs = (0, _get3.default)(_GLOBALS, "allJs", []);

  /**
   * Get css generated by current route and module
   */
  var currentRouteCss = (0, _filter3.default)(allCss, function (css) {
    var fileName = css.split("/").pop();
    return !(0, _startsWith3.default)(fileName, "mod-");
  });

  /**
   * Get all javascript but the modules
   */
  var currentRouteJs = (0, _filter3.default)(allJs, function (js) {
    var fileName = js.split("/").pop();
    return !(0, _startsWith3.default)(fileName, "mod-") && !(0, _startsWith3.default)(fileName, "service-worker.js");
  });

  /**
   * Trying offline solution.
   */
  var html = _server2.default.renderToString(_react2.default.createElement(_html2.default, {
    stylesheets: currentRouteCss,
    scripts: currentRouteJs
  }));
  return "<!DOCTYPE html>" + html;
};

var workboxSW = new _workboxSw2.default({ skipWaiting: true, clientsClaim: true });

// workboxSW.precache(_.map(WEB_ASSETS, asset => { return {url: asset};}));

var networkFirstHandler = workboxSW.strategies.networkFirst();

workboxSW.router.registerRoute(new RegExp("^" + self.location.origin + "/.(css|bmp|tif|ttf|docx|woff2|js|pict|tiff|eot|xlsx|jpg|csv|eps|woff|xls|jpeg|doc|ejs|otf|pptx|gif|pdf|swf|ps|ico|pls|midi|svgz|class|png|ppt|mid|webp|jar)$", "g"), workboxSW.strategies.cacheFirst());

workboxSW.router.registerRoute(self.location.origin + "/_globals", workboxSW.strategies.cacheFirst());

workboxSW.router.registerRoute(new RegExp("^" + self.location.origin + "/.*"), function (_ref) {
  var event = _ref.event;

  return networkFirstHandler.handle({ event: event }).then(function (response) {
    if (!response) {
      var request = event.request;
      if (request.url.indexOf(serviceWorker.location.origin) !== -1 && request.method.toUpperCase() === "GET" && request.headers.get("accept").indexOf("html") !== -1) {
        return new Response(getOfflineHtml(), { headers: { "Content-Type": "text/html" } });
      }
    }
    return response;
  });
});
;

var _temp = function () {
  if (typeof __REACT_HOT_LOADER__ === 'undefined') {
    return;
  }

  __REACT_HOT_LOADER__.register(serviceWorker, "serviceWorker", "lib/service-worker.js");

  __REACT_HOT_LOADER__.register(_GLOBALS, "_GLOBALS", "lib/service-worker.js");

  __REACT_HOT_LOADER__.register(getOfflineHtml, "getOfflineHtml", "lib/service-worker.js");

  __REACT_HOT_LOADER__.register(workboxSW, "workboxSW", "lib/service-worker.js");

  __REACT_HOT_LOADER__.register(networkFirstHandler, "networkFirstHandler", "lib/service-worker.js");
}();

;