// admin api
function Admin() {
  function getQueryParams(...excludes) {
      const params = {};
      const search = location.search.substring(1);
      const _excludes = [...excludes];
      if (!search) return params;

      search.split('&').forEach(pair => {
          const [k, v] = pair.split('=');
          if (_excludes.indexOf(k) !== -1) return true;
          params[decodeURIComponent(k)] = decodeURIComponent(v || '');
      });
      return params;
  }
  function removeQueryParams(...keys) {
      const q = getQueryParams();

      keys.forEach(k => {
          delete q[k];
      });

      updateQueryParams(q, true);

      // 通知当前页面状态刷新（由调用方决定具体逻辑）
      window.dispatchEvent(new PopStateEvent('popstate'));
  }
  function updateQueryParams(params, replace = false) {
      const query = Object.entries(params)
          .filter(([_, v]) => v !== null && v !== undefined && v !== '')
          .map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`)
          .join('&');

      const url = location.pathname + (query ? '?' + query : '');
      replace
          ? history.replaceState(null, '', url)
          : history.pushState(null, '', url);
  }
  function buildWhereFromQuery(...excludes) {
    const q = getQueryParams();
    const _excludes = ['page', 'limit', ...excludes];

    const where = {};
    Object.keys(q).forEach(k => {
      if (q[k] !== '' && q[k] != null && _excludes.indexOf(k) === -1) {
        where[k] = q[k];
      }
    });
    return where;
  }
  function buildPaginateFromQuery() {
    const q = getQueryParams();
    return {
      curr: Number(q.page) || 1,
      limit: Number(q.limit) || 10
    }
  }
  return {
    getQueryParams,
    removeQueryParams,
    updateQueryParams,
    buildWhereFromQuery,
    buildPaginateFromQuery
  }
}
var Admin = window.Admin = Admin();