"use strict";

exports.__esModule = true;
exports.toRelativePath = toRelativePath;
exports.includeUrl = includeUrl;
exports.getQueryParams = getQueryParams;
exports.parse = parse;

require("core-js/modules/es6.regexp.split");

require("core-js/modules/es6.regexp.replace");

require("core-js/modules/es6.regexp.constructor");

function toRelativePath(url, host) {
  if (host) {
    var reg = new RegExp(host.replace(/^https?:\/\//, ''));

    if (reg.test(url)) {
      var path = url.replace(/^https?:\/\//, '').replace(reg, '');
      if (path === '') return '/';
      return path;
    }
  }

  return url;
}
/**
 * 检查路由的包含关系， 不会检查查询参数
 *
 * @export
 * @param {*} href
 * @param {*} url
 * @param {boolean} [fuzzy=false] 模糊查找
 * @returns
 */


function includeUrl(href, url, fuzzy) {
  if (fuzzy === void 0) {
    fuzzy = false;
  }

  if (url === href) return true;

  if (fuzzy) {
    if (href.split('?')[0].replace(/(\/$|#.*)/, '').indexOf(url.split('?')[0].replace(/(\/$|#.*)/, '')) > -1) {
      return true;
    }
  } else {
    if (url.split('?')[0].replace(/(\/$|#.*)/, '') === href.split('?')[0].replace(/(\/$|#.*)/, '')) {
      return true;
    }
  }

  return false;
}

function getQueryParams(url) {
  var result = {};
  var param = /([^?=&]+)=([^&]+)/ig;
  var match;

  while ((match = param.exec(url)) != null) {
    result[match[1]] = match[2];
  }

  return result;
}

function parse(url) {
  var _url$toLowerCase$repl = url.toLowerCase().replace(/^:?\/\/|https?:\/\/?(.+?)/, '$1').split('/'),
      hostname = _url$toLowerCase$repl[0],
      pathname = _url$toLowerCase$repl.slice(1);

  var params = getQueryParams(url);
  return {
    hostname,
    pathname: '/' + pathname.join('/').replace(/(\?.*?)$/, ''),
    params
  };
}