import { ApiError, request } from './client';
import type { components } from './generated/v1';

export type ArtworkUpload = components['schemas']['ArtworkUploadResponseModel'];
export type ArtworkUploadTarget = 'logo' | 'watermark';

export function uploadArtwork(file: File, target: ArtworkUploadTarget): Promise<ArtworkUpload> {
  const body = new FormData();
  body.append('file', file);
  body.append('target', target);

  return request<ArtworkUpload>('/api/v1/artwork/uploads', {
    body,
    method: 'POST'
  });
}

export function messageFromArtworkError(error: unknown, fallback = 'Unable to upload artwork'): string {
  if (error instanceof ApiError) {
    return error.detail ?? error.message;
  }

  if (error instanceof Error) {
    return error.message;
  }

  return fallback;
}
