"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));

exports.onCreateWebpackConfig = function (_ref, pluginOptions) {
  var actions = _ref.actions,
      plugins = _ref.plugins;
  var _pluginOptions$redire = pluginOptions.redirectComponent,
      redirectComponent = _pluginOptions$redire === void 0 ? null : _pluginOptions$redire;
  actions.setWebpackConfig({
    plugins: [plugins.define({
      GATSBY_INTL_REDIRECT_COMPONENT_PATH: JSON.stringify(redirectComponent)
    })]
  });
};

exports.onCreatePage =
/*#__PURE__*/
function () {
  var _ref3 = (0, _asyncToGenerator2["default"])(
  /*#__PURE__*/
  _regenerator["default"].mark(function _callee(_ref2, pluginOptions) {
    var page, actions, createPage, deletePage, _pluginOptions$path, path, _pluginOptions$langua, languages, _pluginOptions$defaul, defaultLanguage, _pluginOptions$redire2, redirect, generatePage, newPage;

    return _regenerator["default"].wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            page = _ref2.page, actions = _ref2.actions;
            createPage = actions.createPage, deletePage = actions.deletePage;
            _pluginOptions$path = pluginOptions.path, path = _pluginOptions$path === void 0 ? "." : _pluginOptions$path, _pluginOptions$langua = pluginOptions.languages, languages = _pluginOptions$langua === void 0 ? ["en"] : _pluginOptions$langua, _pluginOptions$defaul = pluginOptions.defaultLanguage, defaultLanguage = _pluginOptions$defaul === void 0 ? "en" : _pluginOptions$defaul, _pluginOptions$redire2 = pluginOptions.redirect, redirect = _pluginOptions$redire2 === void 0 ? false : _pluginOptions$redire2;

            generatePage = function generatePage(routed, language) {
              return (0, _extends2["default"])({}, page, {
                path: routed ? "/" + language + page.path : page.path,
                context: (0, _extends2["default"])({}, page.context, {
                  intl: {
                    language: language,
                    languages: languages,
                    messages: require(path + "/" + language + ".json"),
                    routed: routed,
                    originalPath: page.path,
                    redirect: redirect
                  }
                })
              });
            };

            newPage = generatePage(false, defaultLanguage);
            deletePage(page);
            createPage(newPage);
            languages.forEach(function (language) {
              var localePage = generatePage(true, language);

              if (localePage.path.includes("/404/")) {
                localePage.matchPath = "/" + language + "/*";
              }

              createPage(localePage);
            });

          case 8:
          case "end":
            return _context.stop();
        }
      }
    }, _callee);
  }));

  return function (_x, _x2) {
    return _ref3.apply(this, arguments);
  };
}();