"use strict";

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

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

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

  return data;
}

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

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

  return data;
}

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

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

  return data;
}

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

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

const getContent = path => {
  if ((0, _fs().existsSync)(path)) {
    const content = (0, _fs().readFileSync)(path, 'utf-8');
    return content;
  }

  return '';
};

exports.getContent = getContent;

var _default = (service, clientStat) => {
  const paths = service.paths;
  const absOutputPath = paths.absOutputPath;
  const chunkGroups = clientStat.compilation.chunkGroups;
  const chunkGroupData = (0, _getChunkMap.getChunkGroupData)(chunkGroups); // get umi.js / umi.css

  const _ref = chunkGroupData.find(chunk => chunk.name === 'umi') || {},
        _ref$chunks = _ref.chunks,
        umiChunk = _ref$chunks === void 0 ? [] : _ref$chunks;

  const umiJS = umiChunk.find(chunk => (0, _getChunkMap.isAssetsType)('js', chunk)) || '';
  const umiCSS = umiChunk.find(chunk => (0, _getChunkMap.isAssetsType)('css', chunk)) || '';
  const replaceUmiJS = [/__UMI_SERVER__\.js/g, umiJS];
  const replaceUmiCSS = umiCSS ? [/__UMI_SERVER__\.css/g, umiCSS] : [/,[\0-\+\x2D-\uFFFF]*createElement\("link"[\s\S]*?__UMI_SERVER__\.css[\s\S]*?\)/g, ''];
  const umiServerPath = (0, _path().join)(absOutputPath, 'umi.server.js');
  const umiServer = getContent(umiServerPath);
  const result = umiServer.replace(...replaceUmiJS).replace(...replaceUmiCSS);
  (0, _fs().writeFileSync)(umiServerPath, result, 'utf-8');
};

exports.default = _default;