export const QUERY_MEMORY = {
  highVolumeGcTime: 0,
  optionGcTime: 30_000,
  summaryGcTime: 60_000
} as const;

// Live views must not spend traffic while the browser tab is hidden, but they
// should reconcile immediately when an operator returns instead of waiting for
// the next interval tick.
export const LIVE_QUERY_POLICY = {
  refetchIntervalInBackground: false,
  refetchOnWindowFocus: true
} as const;

export function retainPreviousPageWithinScope<T>(scope: string, scopeIndex = 1) {
  return (previous: T | undefined, previousQuery?: { queryKey: readonly unknown[] }) => previousQuery?.queryKey[scopeIndex] === scope ? previous : undefined;
}

export function queryScopeKey(query: object) {
  return Object.entries(query)
    .filter(([, value]) => value !== undefined && value !== null && value !== '')
    .sort(([left], [right]) => left.localeCompare(right))
    .map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`)
    .join('&');
}
