'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var router_pathToRegexp = require('./path-to-regexp.js');
require('@babel/runtime/helpers/typeof');

function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function getQueryBaseStr() {
  var str = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
  if (!str) {
    return {};
  }
  return str.split('&').reduce(function (acc, item) {
    var list = item.split('=');
    acc[list[0]] = list[1];
    return acc;
  }, {});
}
function isMatchPath(meta, path) {
  var _meta$rawPath = meta.rawPath,
    rawPath = _meta$rawPath === void 0 ? [] : _meta$rawPath;
  if (!(rawPath === null || rawPath === void 0 ? void 0 : rawPath.length)) return;
  var _iterator = _createForOfIteratorHelper(rawPath),
    _step;
  try {
    for (_iterator.s(); !(_step = _iterator.n()).done;) {
      var item = _step.value;
      // eslint-disable-next-line prefer-const
      var keys = [];
      var regexp = router_pathToRegexp["default"](item, keys);
      var match = path.match(regexp);
      if (match) {
        return {
          match: match,
          regexp: regexp,
          keys: keys
        };
      }
    }
  } catch (err) {
    _iterator.e(err);
  } finally {
    _iterator.f();
  }
  return;
}
/**
 * 根据路由表，找到 path 对应的 路由名称
 * @param {string} path 路由路径
 * @param {array} routes 路由表
 * @returns {object} 匹配到的路由信息
 *
 * @example
 * ```ts
 * const { name, params, meta, path } = findRouteName(rawPath, ALL_ROUTES) || {};
 *
 * console.log('name', name);
 * ```
 */
function findRouteName(path, routes) {
  var _loop = function _loop() {
      var item = routes[i];
      var name = item.name,
        _item$meta = item.meta,
        meta = _item$meta === void 0 ? {} : _item$meta,
        itemPath = item.path;
      var purePath = path;
      var queryStr = '';
      if (path.includes('?')) {
        var idx = path.indexOf('?');
        purePath = path.slice(0, idx);
        queryStr = path.slice(idx + 1);
      }
      var match = isMatchPath(meta, purePath);
      var queryBasePath = getQueryBaseStr(queryStr);
      if (match) {
        var iMatch = match.match,
          keys = match.keys;
        var params = keys.reduce(function (acc, key, index) {
          var name = key.name;
          acc[name] = iMatch[index + 1];
          return acc;
        }, {});
        return {
          v: {
            name: name,
            params: Object.assign(Object.assign({}, params), queryBasePath),
            path: itemPath,
            meta: meta
          }
        };
      }
    },
    _ret;
  // eslint-disable-next-line @typescript-eslint/prefer-for-of
  for (var i = 0; i < routes.length; i++) {
    _ret = _loop();
    if (_ret) return _ret.v;
  }
}

exports.findRouteName = findRouteName;
exports.getQueryBaseStr = getQueryBaseStr;
exports.isMatchPath = isMatchPath;
