"use strict";

var _interopRequireDefault = require("@babel/runtime-corejs2/helpers/interopRequireDefault");

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

var _assign = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/object/assign"));

var _isEqual = _interopRequireDefault(require("lodash/isEqual"));

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

function includeUrl(href, url) {
  if (url === href) return true;

  if (url.split('?')[0].replace(/(\/$|#.*)/, '') === href.split('?')[0].replace(/(\/$|#.*)/, '')) {
    if (href.split('?')[1]) {
      var currentQueryParams = getQueryParams(href);
      return (0, _isEqual.default)(currentQueryParams, (0, _assign.default)({}, currentQueryParams, getQueryParams(url)));
    }

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