import { keepUrlParams } from './keep-url-params.mjs';
import './format-url-params.mjs';
import './resolve-url-params.mjs';
import '@babel/runtime/helpers/slicedToArray';

/**
 * 根据地址长度，进行过滤地址参数，允许指定保留特定参数
 * @param {object} [params={ limit: 1024 }] 参数
 * @param {number} params.url 待过滤地址，默认当前页面地址
 * @param {number} params.limit 参数长度限制
 * @param {array}  params.keepKey 指定保留的参数，比如业务参数、框架参数（登录态、统计上报等）
 * @example
 * // 未超过长度限制，原样返回
 * filterUrlParams({
 *   url: 'https://igame.qq.com/?name=mike#/?from=test',
 *   limit: 600,
 *   keepKey: ['name'],
 * });
 * // => 'https://igame.qq.com/?name=mike#/?from=test'
 * @example
 * // search 参数超长，仅保留 keepKey 指定参数
 * filterUrlParams({
 *   url: 'https://igame.qq.com/?name=mike&__lxsdk_params=...(超长)#/?from=test',
 *   limit: 600,
 *   keepKey: ['name'],
 * });
 * // => 'https://igame.qq.com/#/?name=mike'
 * @example
 * // search 参数超长 + 强制 history 模式返回
 * filterUrlParams({
 *   url: 'https://igame.qq.com/?name=mike&__lxsdk_params=...(超长)',
 *   limit: 600,
 *   keepKey: ['name'],
 *   forceHistoryMode: true,
 * });
 * // => 'https://igame.qq.com/?name=mike'
 * @example
 * // hash 参数超长
 * filterUrlParams({
 *   url: 'https://igame.qq.com/?name=mike#/?name=mike&__lxsdk_params=...(超长)',
 *   limit: 600,
 *   keepKey: ['name'],
 * });
 * // => 'https://igame.qq.com/#/?name=mike'
 * @example
 * // 参数超长，并且支持识别路径
 * filterUrlParams({
 *   url: 'https://igame.qq.com/?name=mike#/detail?name=mike&__lxsdk_params=...(超长)',
 *   limit: 600,
 *   keepKey: ['name'],
 * });
 * // => 'https://igame.qq.com/#/detail?name=mike'
 */
function filterUrlParams() {
  var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {
    url: window.location.href,
    limit: 1000,
    keepKey: []
  };
  var _params$url = params.url,
    url = _params$url === void 0 ? window.location.href : _params$url,
    _params$limit = params.limit,
    limit = _params$limit === void 0 ? 1000 : _params$limit,
    _params$keepKey = params.keepKey,
    keepKeyArr = _params$keepKey === void 0 ? [] : _params$keepKey,
    forceHistoryMode = params.forceHistoryMode;
  // 不超过长度限制，不处理
  var originUrl = url;
  if (originUrl.length <= limit) {
    return originUrl;
  }
  // 只保留特定参数进行拼接地址
  return keepUrlParams(originUrl, keepKeyArr, forceHistoryMode);
}

export { filterUrlParams };
