// Data-import DOMAIN — supported formats + the accepted-format check (D3 direct / C5 cloud).
// Pure. Source: docs/workflows/data-import-spec.md §2.1. Cross-spec conflict #1: the Direct
// Upload page documents MHA/MHD; the Cloud page omits them — so the accepted set is PER-PIPELINE,
// not one global enum. We model that explicitly here.

import type { DataType, Pipeline } from './dataTypes';

/** Double extensions must be detected before the last-dot fallback (`a.nii.gz` -> `.nii.gz`). */
const DOUBLE_EXTENSIONS = ['.nii.gz'];

/** Lower-cased file extension, double-extension aware. `Brain.NII.GZ` -> `.nii.gz`, `x.DCM` -> `.dcm`. */
export function extensionOf(filename: string): string {
  const lower = filename.toLowerCase();
  for (const dbl of DOUBLE_EXTENSIONS) {
    if (lower.endsWith(dbl)) return dbl;
  }
  const dot = lower.lastIndexOf('.');
  return dot >= 0 ? lower.slice(dot) : '';
}

/** Per-data-type accepted extensions (the §2.1 sets, mapped onto the 5 picker types). */
export const ACCEPTED_EXTENSIONS_BY_TYPE: Record<DataType, readonly string[]> = {
  dicom: ['.dcm', '.ima', '.dicom', '.dicm'],
  nifti: ['.nii', '.nii.gz'],
  image2d: ['.png', '.jpeg', '.jpg', '.bmp'],
  video: ['.mp4', '.mov', '.avi'],
  videoFrames: ['.png', '.jpeg', '.jpg', '.bmp'],
};

/**
 * MHD companion extensions (D6). An `.mhd` header references a raw data file: `.raw`/`.img`
 * (uncompressed) or `.zraw` (compressed). Validated by mapping/mhdCompanions.ts.
 */
export const MHD_COMPANION_EXTENSIONS = ['.raw', '.img', '.zraw'] as const;

/**
 * The full §2.1 supported imaging set, PER PIPELINE. Direct Upload additionally documents the
 * single-file volume formats NRRD/MHA/MHD; the Cloud page omits MHA/MHD (conflict #1, open Q9.1).
 * Used as the generic "is this even an imaging file we accept on this pipeline" gate.
 */
const COMMON_IMAGE_EXTENSIONS = [
  '.dcm', '.ima', '.dicom', '.dicm', // DICOM
  '.nii', '.nii.gz', // NIfTI
  '.png', '.jpeg', '.jpg', '.bmp', // RGB
  '.mp4', '.mov', '.avi', // Video
  '.nrrd', // NRRD
];
const DIRECT_ONLY_EXTENSIONS = ['.mha', '.mhd']; // companions (.raw/.img/.zraw) gate via D6, not here

export const SUPPORTED_EXTENSIONS_DIRECT: readonly string[] = [
  ...COMMON_IMAGE_EXTENSIONS,
  ...DIRECT_ONLY_EXTENSIONS,
];
export const SUPPORTED_EXTENSIONS_CLOUD: readonly string[] = COMMON_IMAGE_EXTENSIONS;

export function supportedExtensions(pipeline: Pipeline): readonly string[] {
  return pipeline === 'direct' ? SUPPORTED_EXTENSIONS_DIRECT : SUPPORTED_EXTENSIONS_CLOUD;
}

/** D3/C5 — is `filename` an accepted format for this pipeline? (generic imaging gate) */
export function isSupportedFormat(pipeline: Pipeline, filename: string): boolean {
  return supportedExtensions(pipeline).includes(extensionOf(filename));
}

/**
 * Stricter check used during Direct Upload staging: does `filename` match the *chosen* data
 * type's accepted set? (A `.mp4` staged under a DICOM job is wrong even though `.mp4` is supported.)
 * `.mhd`/`.mha`/`.raw`/`.img`/`.zraw` are allowed alongside any volume type for D6 handling.
 */
export function isAcceptedForType(dataType: DataType, filename: string): boolean {
  const ext = extensionOf(filename);
  if (ACCEPTED_EXTENSIONS_BY_TYPE[dataType].includes(ext)) return true;
  // MHD/MHA volume + their raw companions ride alongside the volume types (DICOM/NIfTI).
  if (isVolumeCompanionExtension(ext) && (dataType === 'nifti' || dataType === 'dicom')) return true;
  return false;
}

function isVolumeCompanionExtension(ext: string): boolean {
  return ext === '.mhd' || ext === '.mha' || (MHD_COMPANION_EXTENSIONS as readonly string[]).includes(ext);
}
