"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 _umiUtils() {
  const data = require("umi-utils");

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

  return data;
}

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

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

  return data;
}

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: 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 generatorFunc = /*#__PURE__*/function () {
  var _ref = _asyncToGenerator(function* (ctx, args) {
    const logger = ctx.logger,
          api = ctx.api;
    const debug = api.debug;
    const dryRun = args.dryRun,
          isPage = args.page,
          js = args.js,
          _args$execution = args.execution,
          execution = _args$execution === void 0 ? 'shell' : _args$execution,
          uni18n = args.uni18n;
    logger.appendLog();
    logger.appendLog('📦  Start generate files');

    const getBlockGenerator = require('../../../../getBlockGenerator');

    const BlockGenerator = getBlockGenerator.default(ctx.api);
    const _ctx$stages$blockCtx = ctx.stages.blockCtx,
          pkg = _ctx$stages$blockCtx.pkg,
          sourcePath = _ctx$stages$blockCtx.sourcePath,
          filePath = _ctx$stages$blockCtx.filePath,
          routePath = _ctx$stages$blockCtx.routePath,
          templateTmpDirPath = _ctx$stages$blockCtx.templateTmpDirPath;
    let isPageBlock = pkg.blockConfig && pkg.blockConfig.specVersion === '0.1';

    if (isPage !== undefined) {
      // when user use `umi block add --page`
      isPageBlock = isPage;
    }

    debug(`isPageBlock: ${isPageBlock}`);
    const latestPkgPath = (0, _umiUtils().winPath)((0, _path().join)(__dirname, '../../../../../../../../package.json'));
    const generator = new BlockGenerator(args._ ? args._.slice(2) : [], {
      sourcePath,
      path: filePath,
      routePath,
      blockName: args.name || getBlockGenerator.getNameFromPkg(pkg),
      isPageBlock,
      dryRun,
      execution,
      env: {
        cwd: api.cwd
      },
      resolved: latestPkgPath
    });

    try {
      yield generator.run();
    } catch (e) {
      logger.appendLog(`Faild generate files: ${e.message}\n`);
      throw new Error(e);
    } // write dependencies


    if (pkg.blockConfig && pkg.blockConfig.dependencies) {
      const subBlocks = pkg.blockConfig.dependencies;

      try {
        yield Promise.all(subBlocks.map(block => {
          const subBlockPath = (0, _path().join)(templateTmpDirPath, block);
          debug(`subBlockPath: ${subBlockPath}`);
          return new BlockGenerator(args._ ? args._.slice(2) : [], {
            sourcePath: subBlockPath,
            path: isPageBlock ? generator.path : (0, _path().join)(generator.path, generator.blockFolderName),
            blockName: getBlockGenerator.getNameFromPkg( // eslint-disable-next-line
            require((0, _path().join)(subBlockPath, 'package.json'))),
            isPageBlock: false,
            dryRun,
            env: {
              cwd: api.cwd
            },
            routes: api.config.routes,
            resolved: latestPkgPath
          }).run();
        }));
      } catch (e) {
        logger.appendLog(`Faild generate files: ${e.message}\n`);
        throw new Error(e);
      }
    }

    logger.appendLog('🎉  Success generate files\n'); // 调用 sylvanas 转化 ts

    if (js) {
      // 区块需要拼接一下 blockName
      const relayPath = generator.isPageBlock ? generator.blockFolderPath : `${generator.blockFolderPath}/${generator.blockName}`;
      logger.appendLog('🎭  Start TypeScript to JavaScript');

      require('../../../../tsTojs').default(relayPath);

      logger.appendLog('🎉  Success TypeScript to JavaScript\n');
    }

    if (uni18n) {
      logger.appendLog('🌏  Start remove i18n code');

      require('../../../../remove-locale').default(generator.blockFolderPath, uni18n);

      logger.appendLog('🎉  Success remove i18n code\n');
    }

    ctx.stages.generator = generator;
  });

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

var _default = generatorFunc;
exports.default = _default;