import { clampInt } from '@/lib/url/queryCodec';
import type { UrlCodec } from '@/lib/url/useUrlFilterState';

export interface ShareLinksUrlState extends Record<string, unknown> {
  page: number;
}

export function makeShareLinksCodec(basePath: string): UrlCodec<ShareLinksUrlState> {
  return {
    parse(loc) {
      const sp = new URLSearchParams(loc.search);
      return {
        page: clampInt(sp.get('page'), { min: 0, max: Number.MAX_SAFE_INTEGER, fallback: 0 }),
      };
    },

    build(state) {
      const sp = new URLSearchParams();
      if (state.page > 0) sp.set('page', String(state.page));
      const qs = sp.toString();
      return qs ? `${basePath}?${qs}` : basePath;
    },
  };
}
