import * as plugins from './smartrouter.plugins.js';

export class QueryParams {
  constructor() {}

  public getAllAsObject() {
    const urlSearchParams = new URLSearchParams(window.location.search);
    return Object.fromEntries((urlSearchParams as any).entries());
  }

  public setQueryParam(
    queryKeyArg: string,
    queryContentArg: string,
    pushOrReplaceArg: 'push' | 'replace' = 'replace'
  ) {
    var queryParams = new URLSearchParams(window.location.search);
    queryParams.set(queryKeyArg, queryContentArg);
    pushOrReplaceArg === 'push'
      ? history.pushState(null, null, '?' + queryParams.toString())
      : history.replaceState(null, null, '?' + queryParams.toString());
  }

  public deleteQueryParam(queryKeyArg: string, pushOrReplaceArg: 'push' | 'replace' = 'replace') {
    var queryParams = new URLSearchParams(window.location.search);
    queryParams.delete(queryKeyArg);
    pushOrReplaceArg === 'push'
      ? history.pushState(null, null, '?' + queryParams.toString())
      : history.replaceState(null, null, '?' + queryParams.toString());
  }

  public getQueryParam(queryParamName: string) {
    const queryParams = this.getAllAsObject();
    return queryParams[queryParamName];
  }
}