export type SortDirection = 'asc' | 'desc';

export interface GetParams {
  with?: string[] | null;
  start_date?: Date | null;
  end_date?: Date | null;
  sort_column?: string | null;
  sort_direction?: SortDirection | null;
  no_pagination?: boolean;
  search?: string | null;
}

export function toQueryString(params?: GetParams): string {
  if (!params) return '';

  const pairs: string[] = [];

  if (params.with != null) {
    const value = Array.isArray(params.with) ? params.with.join(',') : String(params.with);
    if (value) pairs.push(`with=${encodeURIComponent(value)}`);
  }

  if (params.start_date != null) {
    const v = params.start_date instanceof Date ? params.start_date.toISOString() : String(params.start_date);
    pairs.push(`start_date=${encodeURIComponent(v)}`);
  }

  if (params.end_date != null) {
    const v = params.end_date instanceof Date ? params.end_date.toISOString() : String(params.end_date);
    pairs.push(`end_date=${encodeURIComponent(v)}`);
  }

  if (params.sort_column != null) {
    pairs.push(`sort_column=${encodeURIComponent(params.sort_column)}`);
  }

  if (params.sort_direction != null) {
    pairs.push(`sort_direction=${encodeURIComponent(params.sort_direction)}`);
  }

  if (params.no_pagination === true) {
    pairs.push(`no_pagination=true`);
  }

  if (params.search != null) {
    pairs.push(`search=${encodeURIComponent(params.search)}`);
  }

  if (pairs.length === 0) return '';
  return `?${pairs.join('&')}`;
}
