"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.isPromise = isPromise;
exports.getBaseName = getBaseName;
exports.findPathByLeafId = findPathByLeafId;
exports.transformURL = transformURL;
exports.getRouteAuthority = exports.getAuthorityFromRouter = exports.getPageQuery = exports.isUrl = void 0;

var _pathToRegexp = _interopRequireDefault(require("path-to-regexp"));

var _querystring = require("querystring");

var _packageLock = _interopRequireDefault(require("../../package-lock.json"));

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

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

/* eslint no-useless-escape:0 import/prefer-default-export:0 */
var reg = /(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/;

var isUrl = function isUrl(path) {
  return reg.test(path);
};

exports.isUrl = isUrl;

var getPageQuery = function getPageQuery() {
  return (0, _querystring.parse)(window.location.href.split('?')[1]);
};

exports.getPageQuery = getPageQuery;

var getAuthorityFromRouter = function getAuthorityFromRouter(router, pathname) {
  var authority = router.find(function (_ref) {
    var routes = _ref.routes,
        _ref$path = _ref.path,
        path = _ref$path === void 0 ? '/' : _ref$path,
        _ref$target = _ref.target,
        target = _ref$target === void 0 ? '_self' : _ref$target;
    return path && target !== '_blank' && (0, _pathToRegexp["default"])(path).exec(pathname) || routes && getAuthorityFromRouter(routes, pathname);
  });
  if (authority) return authority;
  return undefined;
};

exports.getAuthorityFromRouter = getAuthorityFromRouter;

var getRouteAuthority = function getRouteAuthority(path, routeData) {
  var authorities;
  routeData.forEach(function (route) {
    if ((0, _pathToRegexp["default"])("".concat(route.path, "/(.*)")).test("".concat(path, "/"))) {
      if (route.authority) {
        authorities = route.authority;
      }

      if (route.path === path) {
        authorities = route.authority || authorities;
      }

      if (route.routes) {
        authorities = getRouteAuthority(path, route.routes) || authorities;
      }
    }
  });
  return authorities;
};

exports.getRouteAuthority = getRouteAuthority;

function isPromise(obj) {
  return !!obj && ( //有实际含义的变量才执行方法，变量null，undefined和''空串都为false
  _typeof(obj) === 'object' || typeof obj === 'function') && // 初始promise 或 promise.then返回的
  typeof obj.then === 'function';
}

function getBaseName() {
  return _packageLock["default"].name.toLocaleLowerCase();
}

function findPathByLeafId(leafId, nodes, path, key) {
  if (path === undefined) {
    path = {};
  }

  var tmpPath = path;

  for (var i = 0; i < nodes.length; i++) {
    if (nodes[i] && nodes[i][key] && leafId == nodes[i][key]) {
      tmpPath = nodes[i];
      return tmpPath;
    }

    if (nodes[i] && nodes[i].routes) {
      var findResult = findPathByLeafId(leafId, nodes[i].routes, tmpPath, key, location);

      if (findResult) {
        return findResult;
      }
    }
  }

  return tmpPath;
}

function transformURL(path) {
  var el = document.createElement("input");
  el.value = path;
  String.fromCharCode(92);
  return el.value.replace(/\\/g, "/");
}