// admin-spa/src/lib/query-params.ts
export function getQuery(): Record<string, string> {
    try {
      const hash = window.location.hash || "";
      const qIndex = hash.indexOf("?");
      if (qIndex === -1) return {};
      const usp = new URLSearchParams(hash.slice(qIndex + 1));
      const out: Record<string, string> = {};
      usp.forEach((v, k) => (out[k] = v));
      return out;
    } catch {
      return {};
    }
  }
  
  export function setQuery(partial: Record<string, any>) {
    const hash = window.location.hash || "#/";
    const [path, qs = ""] = hash.split("?");
    const usp = new URLSearchParams(qs);
    Object.entries(partial).forEach(([k, v]) => {
      if (v == null || v === "") usp.delete(k);
      else usp.set(k, String(v));
    });
    const next = path + (usp.toString() ? "?" + usp.toString() : "");
    if (next !== hash) {
      history.replaceState(null, "", next);
    }
  }