'use strict';

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

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var url_formatUrlParams = require('./format-url-params.js');
var url_resolveUrlParams = require('./resolve-url-params.js');
require('@babel/runtime/helpers/slicedToArray');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);

function simpleRemoveUrlParams() {
  var url = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
  var removeKeyArr = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
  if (!url.includes('?') || !removeKeyArr.length) {
    return url;
  }
  var list = url.split('?');
  var query = list[1].split('&').reduce(function (acc, item) {
    var list = item.split('=');
    return Object.assign(Object.assign({}, acc), _defineProperty__default["default"]({}, list[0], list[1]));
  }, {});
  var queryStr = Object.keys(query).filter(function (item) {
    return !removeKeyArr.includes(item);
  }).map(function (item) {
    return "".concat(item, "=").concat(query[item]);
  }).join('&');
  if (!queryStr) {
    return list[0];
  }
  return "".concat(list[0], "?").concat(queryStr);
}
function isSimpleUrl() {
  var url = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
  return url.indexOf('?') === url.lastIndexOf('?');
}
/**
 * @export removeUrlParams
 * @description 移除参数
 * @param {string} url 地址
 * @param {string} removeKeyArr 待移除的参数名集合
 * @returns 重新拼接的地址
 * @example
 * // 地址是 hash 模式参数
 * removeUrlParams('http://www.test.com/#/detail?a=1&b=2&c=3', ['a', 'b']);
 * // => 'http://www.test.com/#/detail?c=3'
 * @example
 * // 地址 history 模式参数并存
 * removeUrlParams('http://www.test.com?a=1&b=2&c=3', ['a', 'b']);
 * // => 'http://www.test.com?c=3'
 * @example
 * // 地址 history 模式参数并存 + 强制 history 模式返回
 * removeUrlParams('http://www.test.com?a=1&b=2&c=3', ['a', 'b'], true);
 * // => 'http://www.test.com?c=3'
 * @example
 * // 全部参数移除完，返回不带 query 的地址
 * removeUrlParams('http://www.test.com?a=1&b=2&c=3', ['a', 'c', 'b'], true);
 * // => 'http://www.test.com'
 * @example
 * // hash 模式下 query 全部移除，会保留末尾的 ?
 * removeUrlParams('http://www.test.com?a=1&b=2&c=3#/detail?d=4', ['a', 'b', 'c', 'd']);
 * // => 'http://www.test.com/#/detail?'
 * @example
 * // hash 模式和 history 模式参数并存
 * removeUrlParams('http://www.test.com?a=1&b=2&c=3#/detail?d=4', ['a', 'd']);
 * // => 'http://www.test.com/#/detail?b=2&c=3'
 * @example
 * // 地址是 hash 模式和 history 模式参数并存，且是多个参数
 * removeUrlParams('http://www.test.com?d=4&f=6#/detail?a=1&b=2&c=3', ['a', 'd']);
 * // => 'http://www.test.com/#/detail?f=6&b=2&c=3'
 */
function removeUrlParams() {
  var url = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
  var removeKeyArr = arguments.length > 1 ? arguments[1] : undefined;
  var forceHistoryMode = arguments.length > 2 ? arguments[2] : undefined;
  if (isSimpleUrl(url)) {
    return simpleRemoveUrlParams(url, removeKeyArr);
  }
  // 获取链接上的所有参数
  var urlParamsObj = url_resolveUrlParams.resolveUrlParams(url);
  // 移除指定的key集合
  var keepParamsObj = {};
  Object.keys(urlParamsObj).forEach(function (key) {
    if (!removeKeyArr.includes(key)) {
      keepParamsObj[key] = urlParamsObj[key];
    }
  });
  // 根据移除后的参数集合，重新拼接地址
  return url_formatUrlParams.formatUrlParams(url, keepParamsObj, forceHistoryMode);
}

exports.removeUrlParams = removeUrlParams;
