"use strict";

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

function _react() {
  const data = _interopRequireDefault(require("react"));

  _react = function _react() {
    return data;
  };

  return data;
}

function _fs() {
  const data = require("fs");

  _fs = function _fs() {
    return data;
  };

  return data;
}

function _path() {
  const data = require("path");

  _path = function _path() {
    return data;
  };

  return data;
}

var _getChunkMap = require("../../utils/getChunkMap");

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

var _default = service => {
  const isDev = process.env.NODE_ENV === 'development';
  return class {
    apply(compiler) {
      compiler.hooks.emit.tap('generate-ssr-client-manifest', compilation => {
        const routes = service.routes,
              config = service.config;
        const chunkGroups = compilation.chunkGroups,
              outputPath = compilation.compiler.options.output.path;
        const manifestFileName = typeof config.ssr === 'object' && config.ssr.manifestFileName ? config.ssr.manifestFileName : 'ssr-client-mainifest.json';
        const dynamicMap = {};
        const chunkGroupData = (0, _getChunkMap.getChunkGroupData)(chunkGroups);
        (0, _getChunkMap.patchDataWithRoutes)(dynamicMap, routes, chunkGroupData);
        const chunkAssetsMaps = (0, _getChunkMap.getChunkAssetsMaps)(dynamicMap);
        const content = JSON.stringify(chunkAssetsMaps, null, 2);

        try {
          compilation.assets[manifestFileName] = {
            source: () => content,
            size: () => content.length
          };
        } catch (e) {
          compilation.errors.push(e);
        }

        if (isDev) {
          (0, _fs().writeFileSync)((0, _path().resolve)(outputPath, manifestFileName), content);
        }
      });
    }

  };
};

exports.default = _default;