"use strict";

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

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

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

  return data;
}

function _inquirer() {
  const data = _interopRequireDefault(require("inquirer"));

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

  return data;
}

function _ora() {
  const data = _interopRequireDefault(require("ora"));

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

  return data;
}

var _util = require("./util");

var _addBlock = _interopRequireDefault(require("./addBlock"));

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); }); }; }

/**
 * 交互型区块选择
 * - 选择区块名
 * - 输入路径
 * - 选择是否转化 js
 * @param {[
 *  name:string;
 *  value:string;
 *  key:string;
 * ]} blockArray
 * @returns Promise<{args}>
 */
function selectInstallBlockArgs(_x) {
  return _selectInstallBlockArgs.apply(this, arguments);
}
/**
 * 获取区块列表，默认会从  http://blocks.umijs.org/api/blocks 拉
 * 如果配置 defaultGitUrl ，会从 defaultGitUrl 去找
 * @param {*} _
 * @param {*} blockConfig
 */


function _selectInstallBlockArgs() {
  _selectInstallBlockArgs = _asyncToGenerator(function* (blockArray) {
    return new Promise( /*#__PURE__*/function () {
      var _ref = _asyncToGenerator(function* (resolve) {
        let locale = false;

        const _yield$inquirer$promp = yield _inquirer().default.prompt([{
          type: 'list',
          name: 'block',
          message: `⛰  请选择区块（共 ${blockArray.length} 个 )`,
          choices: blockArray
        }, {
          type: 'input',
          name: 'path',
          message: '🏗  请输入输出安装区块的路径'
        }, {
          type: 'confirm',
          name: 'js',
          message: '🤔  将 Typescript 区块转化为 js?',
          default: false
        }, {
          type: 'confirm',
          name: 'uni18n',
          message: '🌎  删除 i18n 代码? ',
          default: false
        }]),
              block = _yield$inquirer$promp.block,
              path = _yield$inquirer$promp.path,
              js = _yield$inquirer$promp.js,
              uni18n = _yield$inquirer$promp.uni18n;

        if (uni18n) {
          const _yield$inquirer$promp2 = yield _inquirer().default.prompt([{
            type: 'input',
            name: 'region',
            message: '🌎  请输入你的选择的语言? ',
            default: 'zh-CN'
          }]),
                region = _yield$inquirer$promp2.region;

          locale = region;
        }

        const blockPath = path || (0, _util.genBlockName)(block);
        resolve({
          url: block,
          path: blockPath,
          js,
          uni18n: locale
        });
      });

      return function (_x3) {
        return _ref.apply(this, arguments);
      };
    }());
  });
  return _selectInstallBlockArgs.apply(this, arguments);
}

function getDefaultBlockList(_x2) {
  return _getDefaultBlockList.apply(this, arguments);
}

function _getDefaultBlockList() {
  _getDefaultBlockList = _asyncToGenerator(function* (_, blockConfig = {}, api) {
    const spinner = (0, _ora().default)();

    const got = require('got');

    let blockArray = [];
    const defaultGitUrl = blockConfig.defaultGitUrl;
    spinner.start('🚣  fetch block list'); // 如果存在 defaultGitUrl 的配置，就从 defaultGitUrl 配置中拿区块列表

    if (defaultGitUrl) {
      // 一个 github 的 api,可以获得文件树
      const files = yield (0, _util.getBlockListFromGit)(defaultGitUrl);
      blockArray = (0, _util.printBlocks)(files, 'link');
    } else {
      const _yield$got = yield got(`http://blocks.umijs.org/api/blocks`),
            body = _yield$got.body;

      const _JSON$parse = JSON.parse(body),
            status = _JSON$parse.status,
            error = _JSON$parse.error,
            data = _JSON$parse.data;

      if (status === 'success') {
        blockArray = (0, _util.printBlocks)(data);
      } else {
        throw new Error(error);
      }
    }

    spinner.succeed();

    if (blockArray.length > 0) {
      const args = yield selectInstallBlockArgs(blockArray);
      return (0, _addBlock.default)(args, {}, api);
    }

    return new Error('No block found');
  });
  return _getDefaultBlockList.apply(this, arguments);
}

var _default = getDefaultBlockList;
exports.default = _default;