"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getNoExitPath = exports.getNoExitRoute = exports.getNoExitVar = void 0;

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

  _react = function _react() {
    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 defaultNameArray = ['Two', 'Three', 'Four', 'Five', 'Six', 'Seven'];

const getNewName = (name, index = 0) => {
  return `${name}${defaultNameArray[index]}`;
};
/**
 * 获取一个未定义的变量名
 * @param param2
 */


const getNoExitVar = /*#__PURE__*/function () {
  var _ref = _asyncToGenerator(function* ({
    api,
    name,
    path,
    defaultName,
    index = 0,
    need = true
  }) {
    if (!need) {
      return name;
    }

    try {
      const _ref2 = yield api.callRemote({
        type: 'org.umi.block.checkBindingInFile',
        payload: {
          path,
          name
        }
      }),
            exists = _ref2.exists;

      if (index < 6 && exists) {
        return getNoExitVar({
          name: getNewName(defaultName || name, index),
          path,
          api,
          defaultName: defaultName || name,
          index: index + 1
        });
      }

      return name;
    } catch (error) {// no thing
    }

    return name;
  });

  return function getNoExitVar(_x) {
    return _ref.apply(this, arguments);
  };
}();
/**
 * 获取未定义的路由
 * @param param0
 */


exports.getNoExitVar = getNoExitVar;

const getNoExitRoute = /*#__PURE__*/function () {
  var _ref3 = _asyncToGenerator(function* ({
    api,
    path,
    defaultPath,
    index = 0,
    need = true
  }) {
    if (!need) {
      return path;
    }

    try {
      const _ref4 = yield api.callRemote({
        type: 'org.umi.block.checkExistRoute',
        payload: {
          path: path.toLowerCase()
        }
      }),
            exists = _ref4.exists;

      if (index < 6 && exists) {
        const fileArray = (defaultPath || path).split('/');
        const name = fileArray.pop();
        const filePath = fileArray.join('/') || '/';
        return getNoExitRoute({
          path: `${filePath}/${getNewName(name, index)}`.replace(/\/\//g, '/'),
          api,
          defaultPath: defaultPath || path,
          index: index + 1
        });
      }

      return path;
    } catch (error) {// no thing
    }

    return path;
  });

  return function getNoExitRoute(_x2) {
    return _ref3.apply(this, arguments);
  };
}();
/**
 * 获取未定义的文件列表
 * @param param0
 */


exports.getNoExitRoute = getNoExitRoute;

const getNoExitPath = /*#__PURE__*/function () {
  var _ref5 = _asyncToGenerator(function* ({
    api,
    path,
    defaultPath,
    index = 0,
    need = true
  }) {
    if (!need) {
      return path;
    }

    try {
      const _ref6 = yield api.callRemote({
        type: 'org.umi.block.checkExistFilePath',
        payload: {
          path
        }
      }),
            exists = _ref6.exists;

      if (index < 6 && exists) {
        const fileArray = (defaultPath || path).split('/');
        const name = fileArray.pop();
        const filePath = fileArray.join('/') || '/';
        return getNoExitPath({
          path: `${filePath}/${getNewName(name, index)}`.replace(/\/\//g, '/'),
          api,
          defaultPath: defaultPath || path,
          index: index + 1
        });
      }

      return path;
    } catch (error) {// no thing
    }

    return path;
  });

  return function getNoExitPath(_x3) {
    return _ref5.apply(this, arguments);
  };
}();

exports.getNoExitPath = getNoExitPath;