/** File[] を { name, contentBase64 }[] に変換する（upload API 用）。 */
export function filesToBase64(fileList: File[]): Promise<{ name: string; contentBase64: string }[]> {
  return Promise.all(
    fileList.map(
      file =>
        new Promise<{ name: string; contentBase64: string }>((resolve, reject) => {
          const reader = new FileReader();
          reader.onload = () => {
            const result = String(reader.result ?? '');
            const comma = result.indexOf(',');
            resolve({ name: file.name, contentBase64: comma >= 0 ? result.slice(comma + 1) : result });
          };
          reader.onerror = () => reject(reader.error ?? new Error('read failed'));
          reader.readAsDataURL(file);
        }),
    ),
  );
}
