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

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

  return data;
}

function _chalk() {
  const data = _interopRequireDefault(require("chalk"));

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

  return data;
}

function _lodash() {
  const data = require("lodash");

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

  return data;
}

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

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

  return data;
}

function _fs() {
  const data = require("fs");

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

  return data;
}

function _getnpmregistry() {
  const data = _interopRequireDefault(require("getnpmregistry"));

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

  return data;
}

var _download = require("../../../../download");

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

function getCtx(_x) {
  return _getCtx.apply(this, arguments);
}

function _getCtx() {
  _getCtx = _asyncToGenerator(function* (url, args = {}, api) {
    const debug = api.debug,
          config = api.config;
    debug(`get url ${url}`);
    const ctx = yield (0, _download.getParsedData)(url, _objectSpread({}, config.block || {}, {}, args));

    if (!ctx.isLocal) {
      const blocksTempPath = (0, _download.makeSureMaterialsTempPathExist)(args.dryRun);
      const templateTmpDirPath = (0, _path().join)(blocksTempPath, ctx.id);
      (0, _lodash().merge)(ctx, {
        routePath: args.routePath,
        sourcePath: (0, _path().join)(templateTmpDirPath, ctx.path),
        branch: args.branch || ctx.branch,
        templateTmpDirPath,
        blocksTempPath,
        repoExists: (0, _fs().existsSync)(templateTmpDirPath)
      });
    } else {
      (0, _lodash().merge)(ctx, {
        routePath: args.routePath,
        templateTmpDirPath: (0, _path().dirname)(url)
      });
    }

    return ctx;
  });
  return _getCtx.apply(this, arguments);
}

const parseUrl = /*#__PURE__*/function () {
  var _ref = _asyncToGenerator(function* (ctx, args) {
    const url = args.url;
    ctx.logger.setId(url); // 设置这次 flow 的 log trace id

    ctx.result.blockUrl = url; // 记录当前的 url

    (0, _assert().default)(url, `run ${_chalk().default.cyan.underline('umi help block')} to checkout the usage`);
    const _ctx$api = ctx.api,
          paths = _ctx$api.paths,
          config = _ctx$api.config;
    const blockConfig = config.block || {};
    const useYarn = (0, _fs().existsSync)((0, _path().join)(paths.cwd, 'yarn.lock'));
    const defaultNpmClient = blockConfig.npmClient || (useYarn ? 'yarn' : 'npm');
    const registryUrl = yield (0, _getnpmregistry().default)();
    const blockCtx = yield getCtx(url, _objectSpread({}, args, {
      npmClient: args.npmClient || defaultNpmClient
    }), ctx.api);
    ctx.stages.blockCtx = blockCtx;
    ctx.stages.registry = args.registry || registryUrl;
  });

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

var _default = parseUrl;
exports.default = _default;