'use strict';

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

var _typeof = require('@babel/runtime/helpers/typeof');

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

var _typeof__default = /*#__PURE__*/_interopDefaultLegacy(_typeof);

/**
 * url参数变对象
 * @param {string} url 输入URL
 * @returns {Object} search对象
 * @example
 * // 完整 url
 * getQueryObj('https://igame.qq.com?name=mike&age=18&feel=cold&from=china');
 * // => { name: 'mike', age: '18', feel: 'cold', from: 'china' }
 * @example
 * // 仅传入 query 字符串（不含 ?）也能解析
 * getQueryObj('name=mike&age=18&feel=cold&from=china');
 * // => { name: 'mike', age: '18', feel: 'cold', from: 'china' }
 */
function getQueryObj(url) {
  var query = {};
  var qs = url.slice(url.indexOf('?') > -1 ? url.indexOf('?') + 1 : 0);
  var kvs;
  var qsList = qs.split('&');
  qsList.forEach(function (item) {
    kvs = item.split('=');
    if (kvs.length === 2) {
      var value = kvs[1];
      query[kvs[0]] = value;
    }
  });
  return query;
}
/**
 * 组装`url`参数，将search参数添加在后面
 * @param {string} url 输入URL
 * @param {Object} queryObj search对象
 * @returns {string} 组装后的url
 * @example
 * // url 本身无 query 参数
 * composeUrlQuery('https://baidu.com', {
 *   name: 'mike',
 *   feel: 'cold',
 *   age: '18',
 *   from: 'test',
 * });
 * // => 'https://baidu.com?name=mike&feel=cold&age=18&from=test'
 * @example
 * // url 已有 query 参数，会与新参数合并
 * composeUrlQuery('https://baidu.com?gender=male', {
 *   name: 'mike',
 *   feel: 'cold',
 *   age: '18',
 *   from: 'test',
 * });
 * // => 'https://baidu.com?gender=male&name=mike&feel=cold&age=18&from=test'
 */
function composeUrlQuery(url, queryObj) {
  var oriQuery = getQueryObj(url);
  var pathname = url.slice(0, url.indexOf('?') > -1 ? url.indexOf('?') : url.length);
  pathname += '?';
  var allQuery = Object.assign(Object.assign({}, oriQuery), queryObj);
  Object.keys(allQuery).forEach(function (i) {
    pathname += "".concat(i, "=").concat(allQuery[i], "&");
  });
  return pathname.slice(0, pathname.length - 1);
}
/**
 * 将对象字符串化
 * @param {object} obj 输入对象
 * @returns {string} 字符串
 * @example
 * // 编码普通对象
 * encodeUrlParam({ a: 1 });
 * // => '%7B%22a%22%3A1%7D'
 * @example
 * // 编码空对象
 * encodeUrlParam({});
 * // => '%7B%7D'
 * @example
 * // 编码嵌套对象（等价于 encodeURIComponent(JSON.stringify(obj))）
 * encodeUrlParam({ name: 'mike', info: { age: 18 } });
 * @example
 * // 编码数组
 * encodeUrlParam([1, 2, 3]);
 * // => encodeURIComponent('[1,2,3]')
 * @example
 * // 编码含中文的对象
 * encodeUrlParam({ name: '张三' });
 * // => encodeURIComponent(JSON.stringify({ name: '张三' }))
 */
function encodeUrlParam(obj) {
  if (_typeof__default["default"](obj) === 'object') {
    return encodeURIComponent(JSON.stringify(obj));
  }
  return encodeURIComponent(obj);
}
/**
 * 将字符串解码，与`encodeUrlParam`相对
 * @param {string} obj 输入字符串
 * @returns {object} 对象
 * @example
 * // 解码普通对象
 * decodeUrlParam('%7B%22a%22%3A1%7D');
 * // => { a: 1 }
 * @example
 * // 解码空对象
 * decodeUrlParam('%7B%7D');
 * // => {}
 * @example
 * // 解码嵌套对象
 * const encoded = encodeURIComponent(JSON.stringify({ name: 'mike', info: { age: 18 } }));
 * decodeUrlParam(encoded);
 * // => { name: 'mike', info: { age: 18 } }
 * @example
 * // 无效字符串返回空对象
 * decodeUrlParam('invalid-string');
 * // => {}
 * @example
 * // encode 和 decode 互逆
 * decodeUrlParam(encodeUrlParam({ name: 'mike', age: 18, list: [1, 2] }));
 * // => { name: 'mike', age: 18, list: [1, 2] }
 */
function decodeUrlParam(str) {
  var res = {};
  try {
    res = JSON.parse(decodeURIComponent(str));
  } catch (err) {
    console.log('[decodeUrlParam] error: ', err);
  }
  return res;
}
/**
 * 获取 Url 参数
 * @param {string} paraName 参数 key
 * @param {string} search url search 部分
 * @returns paraValue
 * @example
 * getUrlPara('gender', '?gender=male&name=mike&feel=cold&age=18&from=test');
 * // => 'male'
 * @example
 * getUrlPara('from', '?gender=male&name=mike&feel=cold&age=18&from=test');
 * // => 'test'
 * @example
 * getUrlPara('age', '?gender=male&name=mike&feel=cold&age=18&from=test');
 * // => '18'
 * @example
 * // 不存在的 key 返回空字符串
 * getUrlPara('other', '?gender=male&name=mike&feel=cold&age=18&from=test');
 * // => ''
 * @example
 * // 未传 search 且环境不存在 location 时，返回空字符串
 * getUrlPara('other');
 * // => ''
 */
function getUrlPara(paraName) {
  var search = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
  if (!search && typeof window !== 'undefined') {
    search = window.location.search;
  }
  if (!search && typeof location !== 'undefined') {
    var _location = location,
      hash = _location.hash;
    search = hash ? hash.substring(hash.indexOf('?')) : '';
  }
  if (!search) search = '';
  var query = search.substring(1);
  var vars = query.split('&');
  // eslint-disable-next-line @typescript-eslint/prefer-for-of
  for (var i = 0; i < vars.length; i++) {
    var pair = vars[i].split('=');
    if (pair[0] == paraName) {
      return pair[1];
    }
  }
  return '';
}

exports.composeUrlQuery = composeUrlQuery;
exports.decodeUrlParam = decodeUrlParam;
exports.encodeUrlParam = encodeUrlParam;
exports.getQueryObj = getQueryObj;
exports.getUrlPara = getUrlPara;
