/**
 * Asset Format Availability Utility
 *
 * Determines which download formats are available for each brand asset type.
 * Logo types get PNG, JPG, SVG. Favicons also get ICO.
 * Other image types get PNG and JPG. Guidelines types have no image downloads.
 */

/** Asset types that are logos and should support PNG, JPG, SVG downloads */
const LOGO_TYPES: string[] = [
  'logo',
  'secondary-logo',
  'wordmark',
  'logo-icon',
  'logoMarkLight',
  'logoMarkDark',
  'logoHorizontal',
  'logoVertical',
  'logoIconOnly',
];

/** Asset types that are favicons and should support PNG, JPG, SVG, ICO downloads */
const FAVICON_TYPES: string[] = ['favicon'];

/**
 * Asset types whose deliverable is a cut-out mark, so a PNG download of them
 * should have its flat backdrop removed.
 *
 * Everything else — banners, social images, presentations, documents, photos,
 * screenshots — keeps whatever background it was made with. Those are composed
 * images with no artwork/background separation, so removing "the uniform border"
 * from one carves a hole out of a finished design instead of freeing a logo.
 */
const TRANSPARENT_BACKGROUND_TYPES: string[] = [
  'app-icon',
  'watermark',
  'transparent',
  'dark-version',
  'light-version',
];

/** Asset types that generate structured text content (no image downloads) */
const GUIDELINES_TYPES: string[] = [
  'clear-space-guidelines',
  'minimum-size-guidelines',
  'brand-usage-rules',
  'dos-and-donts',
];

/** MIME types for each download format */
const FORMAT_MIME_TYPES: Record<string, string> = {
  png: 'image/png',
  jpg: 'image/jpeg',
  svg: 'image/svg+xml',
  ico: 'image/x-icon',
  webp: 'image/webp',
};

/** File extensions for each download format */
const FORMAT_EXTENSIONS: Record<string, string> = {
  png: 'png',
  jpg: 'jpg',
  svg: 'svg',
  ico: 'ico',
  webp: 'webp',
};

/**
 * Get the list of available download formats for a given asset type.
 * Returns an empty array for guidelines types (content format, no image).
 *
 * @param assetType - The BrandAsset type (e.g., 'logo', 'favicon', 'social-og')
 * @param currentFormat - The stored format of the asset (e.g., 'png', 'svg')
 * @returns Array of available format strings (e.g., ['png', 'jpg', 'svg'])
 */
export function getAvailableFormats(assetType: string, currentFormat?: string): string[] {
  // Guidelines types have no image downloads
  if (GUIDELINES_TYPES.includes(assetType) || currentFormat === 'content') {
    return [];
  }

  // Favicons get PNG, JPG, SVG, ICO
  if (FAVICON_TYPES.includes(assetType)) {
    return ['png', 'jpg', 'svg', 'ico'];
  }

  // Logo types get PNG, JPG, SVG
  if (LOGO_TYPES.includes(assetType)) {
    return ['png', 'jpg', 'svg'];
  }

  // All other image types get PNG, JPG
  return ['png', 'jpg'];
}

/**
 * Check if a given format is available for a given asset type.
 */
export function isFormatAvailable(assetType: string, format: string, currentFormat?: string): boolean {
  return getAvailableFormats(assetType, currentFormat).includes(format);
}

/**
 * Get the MIME type for a given format string.
 */
export function getMimeType(format: string): string {
  return FORMAT_MIME_TYPES[format] || 'application/octet-stream';
}

/**
 * Get the file extension for a given format string.
 */
export function getExtension(format: string): string {
  return FORMAT_EXTENSIONS[format] || format;
}

/**
 * Check if an asset type is a guidelines type (no image download).
 */
export function isGuidelinesType(assetType: string): boolean {
  return GUIDELINES_TYPES.includes(assetType);
}

/**
 * Check if an asset type is a logo type (eligible for PNG, JPG, SVG).
 */
export function isLogoType(assetType: string): boolean {
  return LOGO_TYPES.includes(assetType);
}

/**
 * Check if an asset type is a favicon type (eligible for PNG, JPG, SVG, ICO).
 */
export function isFaviconType(assetType: string): boolean {
  return FAVICON_TYPES.includes(assetType);
}

/**
 * Whether a PNG download of this asset type should come out with its background
 * removed. True for logos, favicons, app icons and watermarks; false for every
 * composed image type.
 */
export function supportsTransparentBackground(assetType: string): boolean {
  return (
    LOGO_TYPES.includes(assetType) ||
    FAVICON_TYPES.includes(assetType) ||
    TRANSPARENT_BACKGROUND_TYPES.includes(assetType)
  );
}