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

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

  return data;
}

var _writeNewRoute = _interopRequireDefault(require("../../../../../../../utils/writeNewRoute"));

var _appendBlockToContainer = _interopRequireDefault(require("../../../../appendBlockToContainer"));

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

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

const writeRoutes = /*#__PURE__*/function () {
  var _ref = _asyncToGenerator(function* (ctx, args) {
    const generator = ctx.stages.generator;
    const api = ctx.api,
          logger = ctx.logger;
    const skipModifyRoutes = args.skipModifyRoutes,
          isLayout = args.layout,
          dryRun = args.dryRun,
          index = args.index;

    if (generator.needCreateNewRoute && api.config.routes && !skipModifyRoutes) {
      logger.appendLog(`🛠 Start write route from ${generator.routePath} to ${api.service.userConfig.file}`); // 当前 _modifyBlockNewRouteConfig 只支持配置式路由
      // 未来可以做下自动写入注释配置，支持约定式路由

      const newRouteConfig = api.applyPlugins('_modifyBlockNewRouteConfig', {
        initialValue: _objectSpread({
          name: args.name,
          path: generator.routePath.toLowerCase(),
          component: `.${generator.path}`
        }, isLayout ? {
          routes: []
        } : {})
      });

      try {
        if (!dryRun) {
          (0, _writeNewRoute.default)(newRouteConfig, api.service.userConfig.file, api.paths.absSrcPath);
        }
      } catch (e) {
        logger.appendLog(`Failed to write route: ${e.message}\n`);
        throw new Error(e);
      }

      logger.appendLog('🎉  Success write route\n');
    }

    if (!generator.isPageBlock) {
      logger.appendLog(`🍽  Start write block component ${generator.blockFolderName} import to ${generator.entryPath}`);

      try {
        (0, _appendBlockToContainer.default)({
          entryPath: generator.entryPath,
          blockFolderName: generator.blockFolderName,
          dryRun,
          index
        });
      } catch (e) {
        logger.appendLog(`Failed write block component: ${e.message}\n`);
        throw new Error(e);
      }

      logger.appendLog('🎉  Success write block component \n');
    }

    const _process$env = process.env,
          PORT = _process$env.PORT,
          BASE_PORT = _process$env.BASE_PORT; // Final: show success message

    const viewUrl = `http://localhost:${BASE_PORT || PORT || '8000'}${generator.path.toLowerCase()}`;
    logger.appendLog(`✨  Probable url ${_chalk().default.cyan(viewUrl)} for view the block.`);
  });

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

var _default = writeRoutes;
exports.default = _default;