import type { TmsAttachmentFile } from './types';

import { getAuthMediaUrl } from '#/utils/jnpf';

const IMAGE_EXT = new Set(['jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp', 'svg']);
const VIDEO_EXT = new Set(['mp4', 'webm', 'ogg', 'mov', 'm4v']);
const AUDIO_EXT = new Set(['mp3', 'wav', 'aac', 'm4a', 'flac']);
const OFFICE_EXT = new Set([
  'doc',
  'docx',
  'docm',
  'dot',
  'dotx',
  'xls',
  'xlsx',
  'xlsm',
  'xlt',
  'xltx',
  'ppt',
  'pptx',
  'pptm',
  'pps',
  'ppsx',
  'pot',
  'potx',
  'csv',
  'rtf',
  'odt',
  'ods',
  'odp',
  'ott',
  'ots',
  'otp',
]);

export function resolvePreviewType(ext?: string): string {
  if (!ext) return 'other';
  const e = ext.toLowerCase().replace(/^\./, '');
  if (IMAGE_EXT.has(e)) return 'image';
  if (VIDEO_EXT.has(e)) return 'video';
  if (AUDIO_EXT.has(e)) return 'audio';
  if (e === 'pdf') return 'pdf';
  if (OFFICE_EXT.has(e)) return 'office';
  return 'other';
}

export function guessExt(file?: Pick<TmsAttachmentFile, 'fileExt' | 'name' | 'url'>): string {
  if (file?.fileExt) return file.fileExt.replace(/^\./, '').toLowerCase();
  const fromName = file?.name?.match(/\.([^.\\/]+)$/);
  if (fromName?.[1]) return fromName[1].toLowerCase();
  const fromUrl = file?.url?.match(/\.([^.\\/?#]+)(?:[?#]|$)/);
  if (fromUrl?.[1]) return fromUrl[1].toLowerCase();
  return '';
}

/** 补齐 previewType / fileExt，便于仅有 name+url 的附件也能页内预览 */
export function normalizeAttachment(file: TmsAttachmentFile): TmsAttachmentFile {
  const fileExt = guessExt(file);
  return {
    ...file,
    fileExt: file.fileExt || fileExt || undefined,
    previewType: file.previewType || resolvePreviewType(fileExt),
  };
}

/** 认证媒体地址；t=t 让文件服务直接输出内容，避免 img/video 302 */
export function toAuthMediaUrl(url?: string): string {
  if (!url) return '';
  const apiIndex = url.indexOf('/api/');
  const path = /^https?:\/\//i.test(url) && apiIndex >= 0 ? url.slice(apiIndex) : url;
  return getAuthMediaUrl(path, false);
}
