// api.ts から分割（挙動不変）: 全 API モジュール共通の基盤ヘルパー。

export const BASE = '/api';

// Blob を受け取りブラウザのダウンロードを発火する（zip 一括ダウンロード用）。
export function triggerBlobDownload(blob: Blob, filename: string): void {
  const url = URL.createObjectURL(blob);
  const a = document.createElement('a');
  a.href = url;
  a.download = filename;
  document.body.appendChild(a);
  a.click();
  a.remove();
  URL.revokeObjectURL(url);
}

/**
 * Build a `?key=value&...` query string from defined entries (undefined dropped).
 * Used by the space-scoped variants below so passing `spaceId` adds `?spaceId=…`
 * and omitting it leaves the per-user URL unchanged (backward compatible).
 */
export function buildQuery(params: Record<string, string | undefined>): string {
  const parts = Object.entries(params)
    .filter(([, v]) => v !== undefined && v !== '')
    .map(([k, v]) => `${k}=${encodeURIComponent(v as string)}`);
  return parts.length ? `?${parts.join('&')}` : '';
}
