// Lightweight mime-types shim — avoids bundling the full 295KB mime-db
// Only includes types actually used in payment-kit API routes

const types: Record<string, string> = {
  '.html': 'text/html',
  '.htm': 'text/html',
  '.json': 'application/json',
  '.js': 'application/javascript',
  '.mjs': 'application/javascript',
  '.css': 'text/css',
  '.txt': 'text/plain',
  '.xml': 'application/xml',
  '.png': 'image/png',
  '.jpg': 'image/jpeg',
  '.jpeg': 'image/jpeg',
  '.gif': 'image/gif',
  '.svg': 'image/svg+xml',
  '.webp': 'image/webp',
  '.ico': 'image/x-icon',
  '.woff': 'font/woff',
  '.woff2': 'font/woff2',
  '.pdf': 'application/pdf',
  '.zip': 'application/zip',
  '.csv': 'text/csv',
  '.mp4': 'video/mp4',
  '.webm': 'video/webm',
  '.mp3': 'audio/mpeg',
  '.wav': 'audio/wav',
  '.bin': 'application/octet-stream',
};

const extensions: Record<string, string> = {};
for (const [ext, mime] of Object.entries(types)) {
  extensions[mime] = ext;
}

export function lookup(path: string): string | false {
  const ext = '.' + path.split('.').pop()?.toLowerCase();
  return types[ext] || false;
}

export function contentType(typeOrPath: string): string | false {
  const mime = typeOrPath.includes('/') ? typeOrPath : lookup(typeOrPath);
  if (!mime) return false;
  if (mime.startsWith('text/')) return `${mime}; charset=utf-8`;
  return mime;
}

export function extension(mime: string): string | false {
  return extensions[mime]?.slice(1) || false;
}

export const charset = (_mime: string) => 'UTF-8';
export const charsets = { lookup: charset };

export default { lookup, contentType, extension, charset, charsets, types };
