/**
 * Shared mapping: file type options (accept strings) → MIME types.
 * Used by Upload node config (allowed types) and IfElse node (mimeType comparison).
 * Single source of truth – nichts hardcoden.
 */

export interface FileTypeOption {
  acceptValue: string;
  label: string;
  mimeTypes: Array<{ value: string; label: string }>;
}

/** Predefined file type options with their MIME type mappings. */
export const FILE_TYPE_OPTIONS: FileTypeOption[] = [
  { acceptValue: '.pdf', label: 'PDF', mimeTypes: [{ value: 'application/pdf', label: 'PDF' }] },
  {
    acceptValue: 'image/*',
    label: 'Bilder (alle)',
    mimeTypes: [
      { value: 'image/jpeg', label: 'JPEG' },
      { value: 'image/png', label: 'PNG' },
      { value: 'image/gif', label: 'GIF' },
      { value: 'image/webp', label: 'WebP' },
    ],
  },
  {
    acceptValue: '.jpg,.jpeg,.png,.gif,.webp',
    label: 'Bilder (JPG, PNG, …)',
    mimeTypes: [
      { value: 'image/jpeg', label: 'JPEG' },
      { value: 'image/png', label: 'PNG' },
      { value: 'image/gif', label: 'GIF' },
      { value: 'image/webp', label: 'WebP' },
    ],
  },
  {
    acceptValue: '.doc,.docx',
    label: 'Word (DOC, DOCX)',
    mimeTypes: [
      { value: 'application/msword', label: 'DOC' },
      { value: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', label: 'DOCX' },
    ],
  },
  {
    acceptValue: '.xls,.xlsx',
    label: 'Excel (XLS, XLSX)',
    mimeTypes: [
      { value: 'application/vnd.ms-excel', label: 'XLS' },
      { value: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', label: 'XLSX' },
    ],
  },
  { acceptValue: '.txt', label: 'Text (TXT)', mimeTypes: [{ value: 'text/plain', label: 'Text' }] },
  { acceptValue: '.csv', label: 'CSV', mimeTypes: [{ value: 'text/csv', label: 'CSV' }] },
  { acceptValue: '.json', label: 'JSON', mimeTypes: [{ value: 'application/json', label: 'JSON' }] },
  {
    acceptValue: '.xml',
    label: 'XML',
    mimeTypes: [
      { value: 'application/xml', label: 'XML' },
      { value: 'text/xml', label: 'XML (text)' },
    ],
  },
  { acceptValue: '.zip', label: 'ZIP', mimeTypes: [{ value: 'application/zip', label: 'ZIP' }] },
];

/** Parse allowedTypes array or accept string from node params. */
export function parseAllowedTypes(params: Record<string, unknown>): string[] {
  const t = params.allowedTypes;
  if (Array.isArray(t) && t.every((x) => typeof x === 'string')) return t as string[];
  const a = params.accept;
  if (typeof a === 'string' && a.trim()) return a.split(',').map((s) => s.trim()).filter(Boolean);
  return [];
}

/** Get MIME type options for comparison from an Upload node's parameters. */
export function getMimeTypeOptionsFromUploadParams(params: Record<string, unknown>): Array<{ value: string; label: string }> {
  const allowedTypes = parseAllowedTypes(params);
  const seen = new Set<string>();
  const result: Array<{ value: string; label: string }> = [];
  const sources = allowedTypes.length > 0 ? allowedTypes : FILE_TYPE_OPTIONS.map((o) => o.acceptValue);
  for (const acceptVal of sources) {
    const opt = FILE_TYPE_OPTIONS.find((o) => o.acceptValue === acceptVal);
    if (opt) {
      for (const m of opt.mimeTypes) {
        if (!seen.has(m.value)) {
          seen.add(m.value);
          result.push(m);
        }
      }
    }
  }
  return result;
}

/** Get accept values for HTML file input (for UploadNodeConfig). */
export function getAcceptValues(): Array<{ value: string; label: string }> {
  return FILE_TYPE_OPTIONS.map((o) => ({ value: o.acceptValue, label: o.label }));
}
