"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 _cheerio() {
  const data = _interopRequireDefault(require("cheerio"));

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

  return data;
}

var _HTMLGenerator = _interopRequireDefault(require("../../html/HTMLGenerator"));

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

var _default = (service, opts = {}) => {
  const config = service.config,
        paths = service.paths,
        webpackConfig = service.webpackConfig,
        routes = service.routes;
  const chunksMap = opts.chunksMap,
        headScripts = opts.headScripts,
        scripts = opts.scripts;
  return new _HTMLGenerator.default({
    config,
    paths,
    routes,
    publicPath: webpackConfig.output.publicPath,
    chunksMap,

    modifyContext(context, opts = {}) {
      const route = opts.route;
      return service.applyPlugins('modifyHTMLContext', {
        initialValue: context,
        args: {
          route
        }
      });
    },

    modifyRouterBaseStr(str) {
      return str;
    },

    modifyPublicPathStr(memo) {
      return service.applyPlugins('modifyPublicPathStr', {
        initialValue: memo
      });
    },

    modifyChunks(memo, opts = {}) {
      const route = opts.route;
      return service.applyPlugins('modifyHTMLChunks', {
        initialValue: memo,
        args: {
          route
        }
      });
    },

    modifyMetas(memo, opts = {}) {
      const route = opts.route;
      return service.applyPlugins('addHTMLMeta', {
        initialValue: memo,
        args: {
          route
        }
      });
    },

    modifyLinks(memo, opts = {}) {
      const route = opts.route;
      return service.applyPlugins('addHTMLLink', {
        initialValue: memo,
        args: {
          route
        }
      });
    },

    modifyScripts(memo, opts = {}) {
      const route = opts.route;
      return service.applyPlugins('addHTMLScript', {
        initialValue: [...(scripts || []), ...memo],
        args: {
          route
        }
      });
    },

    modifyStyles(memo, opts = {}) {
      const route = opts.route;
      return service.applyPlugins('addHTMLStyle', {
        initialValue: memo,
        args: {
          route
        }
      });
    },

    modifyHeadScripts(memo, opts = {}) {
      const route = opts.route;
      return service.applyPlugins('addHTMLHeadScript', {
        initialValue: [...(headScripts || []), ...memo],
        args: {
          route
        }
      });
    },

    modifyHTML(memo, opts = {}) {
      const route = opts.route,
            getChunkPath = opts.getChunkPath;

      const $ = _cheerio().default.load(memo, {
        decodeEntities: false,
        recognizeSelfClosing: true
      });

      service.applyPlugins('modifyHTMLWithAST', {
        initialValue: $,
        args: {
          route,
          getChunkPath
        }
      });
      return $.html();
    }

  });
};

exports.default = _default;