package main

import "core:strings"

is_url :: proc(url: string) -> bool {
	if strings.contains(url, ":") || strings.contains(url, "/") || strings.contains(url, ".") {
		return true
	}
	return false
}

get_ext :: proc(url: string) -> string {
	clean := url
	if question := strings.index_byte(clean, '?'); question != -1 {
		clean = clean[:question]
	}
	if hash := strings.index_byte(clean, '#'); hash != -1 {
		clean = clean[:hash]
	}
	if dot := strings.last_index_byte(clean, '.'); dot != -1 {
		return strings.to_lower(clean[dot:], context.temp_allocator)
	}
	return ""
}

detect_mime_type :: proc(url: string) -> string {
	ext := get_ext(url)
	switch ext {
	case ".jpg", ".jpeg", ".jpe", ".jfif", ".jif", ".pjpeg", ".pjp":
		return "image/jpeg"
	case ".png":
		return "image/png"
	case ".apng":
		return "image/apng"
	case ".gif":
		return "image/gif"
	case ".webp":
		return "image/webp"
	case ".avif":
		return "image/avif"
	case ".heic":
		return "image/heic"
	case ".heif", ".hif":
		return "image/heif"
	case ".bmp", ".dib":
		return "image/bmp"
	case ".tif", ".tiff":
		return "image/tiff"
	case ".ico", ".cur":
		return "image/x-icon"
	case ".svg", ".svgz":
		return "image/svg+xml"
	case ".raw":
		return "image/x-raw"
	case ".cr2":
		return "image/x-canon-cr2"
	case ".cr3":
		return "image/x-canon-cr3"
	case ".nef":
		return "image/x-nikon-nef"
	case ".nrw":
		return "image/x-nikon-nrw"
	case ".orf":
		return "image/x-olympus-orf"
	case ".sr2", ".srf":
		return "image/x-sony-sr2"
	case ".arw":
		return "image/x-sony-arw"
	case ".rw2":
		return "image/x-panasonic-rw2"
	case ".pef":
		return "image/x-pentax-pef"
	case ".rwl":
		return "image/x-pentax-rwl"
	case ".raf":
		return "image/x-fuji-raf"
	case ".dng":
		return "image/x-adobe-dng"
	case ".3fr":
		return "image/x-3fr"
	case ".ari":
		return "image/x-ari"
	case ".bay":
		return "image/x-bay"
	case ".cap":
		return "image/x-cap"
	case ".dcr":
		return "image/x-kodak-dcr"
	case ".erf":
		return "image/x-epson-erf"
	case ".fff":
		return "image/x-hasselblad-fff"
	case ".iiq":
		return "image/x-iiq"
	case ".k25":
		return "image/x-kodak-k25"
	case ".kdc":
		return "image/x-kodak-kdc"
	case ".mdc":
		return "image/x-minolta-mdc"
	case ".mos":
		return "image/x-mamiya-mos"
	case ".mrw":
		return "image/x-minolta-mrw"
	case ".obm":
		return "image/x-obm"
	case ".ptx":
		return "image/x-pentax-ptx"
	case ".pxn":
		return "image/x-panasonic-pxn"
	case ".r3d":
		return "image/x-red-r3d"
	case ".rwz":
		return "image/x-rwz"
	case ".srw":
		return "image/x-samsung-srw"
	case ".x3f":
		return "image/x-sigma-x3f"
	case ".gpr":
		return "image/x-gpr"
	case ".pgm":
		return "image/x-portable-graymap"
	case ".ppm":
		return "image/x-portable-pixmap"
	case ".pbm":
		return "image/x-portable-bitmap"
	case ".pnm":
		return "image/x-portable-anymap"
	case ".pam":
		return "image/x-portable-arbitrarymap"
	case ".pfm":
		return "image/x-portable-floatmap"
	case ".tga", ".icb", ".vda", ".vst":
		return "image/x-targa"
	case ".pcx":
		return "image/x-pcx"
	case ".xbm":
		return "image/x-xbitmap"
	case ".xpm":
		return "image/x-xpixmap"
	case ".xwd":
		return "image/x-xwindowdump"
	case ".sgi", ".rgb", ".rgba", ".bw":
		return "image/x-sgi"
	case ".jp2", ".j2k", ".j2c":
		return "image/jp2"
	case ".jpf", ".jpx", ".jpm", ".mj2":
		return "image/jpx"
	case ".wbmp":
		return "image/vnd.wap.wbmp"
	case ".emf":
		return "image/x-emf"
	case ".wmf":
		return "image/x-wmf"
	case ".eps", ".epsi", ".epsf":
		return "application/postscript"
	case ".psd":
		return "image/vnd.adobe.photoshop"
	case ".psb":
		return "image/vnd.adobe.photoshop"
	case ".ai":
		return "application/postscript"
	case ".cdr":
		return "image/x-coreldraw"
	case ".xcf":
		return "image/x-xcf"
	case ".gbr":
		return "image/x-gimp-gbr"
	case ".gih":
		return "image/x-gimp-gih"
	case ".pat":
		return "image/x-gimp-pat"
	case ".art":
		return "image/x-art"
	case ".jxl":
		return "image/jxl"
	case ".jxr", ".wdp", ".hdp":
		return "image/vnd.ms-photo"
	case ".exr":
		return "image/x-exr"
	case ".hdr":
		return "image/vnd.radiance"
	case ".dpx":
		return "image/x-dpx"
	case ".cin":
		return "image/x-cineon"
	case ".fits", ".fit", ".fts":
		return "image/fits"
	case ".icns":
		return "image/x-icns"
	case ".flif":
		return "image/x-flif"
	case ".dds":
		return "image/vnd.ms-dds"
	case ".ktx":
		return "image/ktx"
	case ".ktx2":
		return "image/ktx2"
	case ".astc":
		return "image/x-astc"
	case ".kodak":
		return "image/x-kodak"
	case ".fpx":
		return "image/vnd.fpx"
	case ".pcd":
		return "image/x-photo-cd"
	case ".cmx":
		return "image/x-cmx"
	case ".drw":
		return "image/x-drw"
	case ".cgm":
		return "image/cgm"
	case ".plt", ".hpgl":
		return "image/x-hpgl"
	case ".iff", ".ilbm", ".lbm":
		return "image/x-iff"
	case ".mac":
		return "image/x-macpaint"
	case ".msp":
		return "image/x-mspaint"
	case ".pct", ".pic", ".pict":
		return "image/x-pict"
	case ".pntg":
		return "image/x-macpaint"
	case ".qtif", ".qti":
		return "image/x-quicktime"
	case ".ras", ".sun":
		return "image/x-sun-raster"
	case ".tpic":
		return "image/x-tpic"
	case ".wpg":
		return "image/x-wpg"
	case ".g3":
		return "image/g3fax"
	case ".g4":
		return "image/g4fax"
	case ".dcm", ".dicom":
		return "application/dicom"
	case ".ora":
		return "image/openraster"
	case ".kra":
		return "application/x-krita"
	case ".clip":
		return "image/x-clip"

	case ".mp3", ".mp2", ".mp1", ".mpa", ".mpga":
		return "audio/mpeg"
	case ".ogg", ".oga", ".opus":
		return "audio/ogg"
	case ".spx":
		return "audio/x-speex"
	case ".wav", ".wave":
		return "audio/wav"
	case ".flac":
		return "audio/flac"
	case ".aac", ".adts":
		return "audio/aac"
	case ".wma":
		return "audio/x-ms-wma"
	case ".m4a", ".m4b", ".m4p", ".m4r":
		return "audio/mp4"
	case ".aif", ".aiff", ".aifc":
		return "audio/aiff"
	case ".au", ".snd":
		return "audio/basic"
	case ".mid", ".midi", ".rmi", ".kar":
		return "audio/midi"
	case ".ra", ".ram":
		return "audio/x-pn-realaudio"
	case ".mka":
		return "audio/x-matroska"
	case ".ape":
		return "audio/x-ape"
	case ".wv":
		return "audio/x-wavpack"
	case ".ac3":
		return "audio/ac3"
	case ".ec3", ".eac3":
		return "audio/eac3"
	case ".dts":
		return "audio/vnd.dts"
	case ".dtshd":
		return "audio/vnd.dts.hd"
	case ".amr", ".awb":
		return "audio/amr"
	case ".caf":
		return "audio/x-caf"
	case ".gsm":
		return "audio/x-gsm"
	case ".mpc":
		return "audio/x-musepack"
	case ".shn":
		return "audio/x-shn"
	case ".tta":
		return "audio/x-tta"
	case ".voc":
		return "audio/x-voc"
	case ".vox":
		return "audio/x-vox"
	case ".8svx":
		return "audio/x-8svx"
	case ".mod":
		return "audio/x-mod"
	case ".s3m":
		return "audio/x-s3m"
	case ".xm":
		return "audio/x-xm"
	case ".it":
		return "audio/x-it"
	case ".stm":
		return "audio/x-stm"
	case ".med":
		return "audio/x-med"
	case ".mtm":
		return "audio/x-mtm"
	case ".umx":
		return "audio/x-umx"
	case ".669":
		return "audio/x-669"
	case ".far":
		return "audio/x-far"
	case ".amf":
		return "audio/x-amf"
	case ".dsm":
		return "audio/x-dsm"
	case ".gdm":
		return "audio/x-gdm"
	case ".imf":
		return "audio/x-imf"
	case ".okt":
		return "audio/x-okt"
	case ".sfx":
		return "audio/x-sfx"
	case ".ult":
		return "audio/x-ult"
	case ".uni":
		return "audio/x-uni"
	case ".psm":
		return "audio/x-psm"
	case ".j2b":
		return "audio/x-j2b"
	case ".dbm":
		return "audio/x-dbm"
	case ".digi":
		return "audio/x-digi"
	case ".dtm":
		return "audio/x-dtm"
	case ".plm":
		return "audio/x-plm"
	case ".nsf", ".nsfe":
		return "audio/x-nsf"
	case ".spc":
		return "audio/x-spc"
	case ".gym":
		return "audio/x-gym"
	case ".vgm", ".vgz":
		return "audio/x-vgm"
	case ".gbs":
		return "audio/x-gbs"
	case ".hes":
		return "audio/x-hes"
	case ".ay":
		return "audio/x-ay"
	case ".sid":
		return "audio/x-sid"
	case ".oma":
		return "audio/x-oma"
	case ".aa3", ".at3", ".at9":
		return "audio/x-atrac"
	case ".qcp":
		return "audio/qcelp"
	case ".tak":
		return "audio/x-tak"
	case ".ofr", ".ofs":
		return "audio/x-optimfrog"
	case ".sox":
		return "audio/x-sox"
	case ".weba":
		return "audio/webm"
	case ".dss":
		return "audio/x-dss"
	case ".alac":
		return "audio/alac"
	case ".vqf", ".vql":
		return "audio/x-vqf"
	case ".mxmf":
		return "audio/x-mxmf"
	case ".ota":
		return "audio/x-ota"
	case ".imy":
		return "audio/i-melody"
	case ".rtttl":
		return "audio/x-rtttl"
	case ".rtx":
		return "audio/x-rtx"
	case ".xmf":
		return "audio/x-xmf"
	case ".w64":
		return "audio/x-w64"
	case ".rf64":
		return "audio/x-rf64"
	case ".bwf":
		return "audio/x-bwf"
	case ".cda":
		return "audio/x-cda"
	case ".aup":
		return "audio/x-aup"

	case ".mp4", ".m4v", ".mp4v":
		return "video/mp4"
	case ".webm":
		return "video/webm"
	case ".mov", ".qt":
		return "video/quicktime"
	case ".avi":
		return "video/x-msvideo"
	case ".mkv":
		return "video/x-matroska"
	case ".flv":
		return "video/x-flv"
	case ".f4v", ".f4p":
		return "video/x-f4v"
	case ".f4a", ".f4b":
		return "audio/mp4"
	case ".wmv":
		return "video/x-ms-wmv"
	case ".mpg", ".mpeg", ".mpe", ".m1v", ".m2v", ".mpv", ".mp2v", ".mpv2":
		return "video/mpeg"
	case ".ogv", ".ogx":
		return "video/ogg"
	case ".3gp", ".3gpp":
		return "video/3gpp"
	case ".3g2", ".3gpp2":
		return "video/3gpp2"
	case ".asf", ".asx":
		return "video/x-ms-asf"
	case ".vob":
		return "video/x-vob"
	case ".ts", ".mts", ".m2ts", ".tsv", ".tsa":
		return "video/mp2t"
	case ".divx":
		return "video/x-divx"
	case ".xvid":
		return "video/x-xvid"
	case ".rm", ".rmvb":
		return "video/x-pn-realvideo"
	case ".swf":
		return "application/x-shockwave-flash"
	case ".dv", ".dif":
		return "video/x-dv"
	case ".gifv":
		return "video/x-gifv"
	case ".roq":
		return "video/x-roq"
	case ".nsv":
		return "video/x-nsv"
	case ".svi":
		return "video/x-svi"
	case ".annodex":
		return "application/x-annodex"
	case ".axv":
		return "video/x-annodex"
	case ".amv":
		return "video/x-amv"
	case ".bik", ".bk2":
		return "video/x-bik"
	case ".smk":
		return "video/x-smacker"
	case ".ivf":
		return "video/x-ivf"
	case ".mxf":
		return "application/mxf"
	case ".nut":
		return "video/x-nut"
	case ".ogm":
		return "video/x-ogm"
	case ".fli", ".flc":
		return "video/x-fli"
	case ".y4m":
		return "video/x-yuv4mpegpipe"
	case ".264", ".h264":
		return "video/H264"
	case ".265", ".h265", ".hevc":
		return "video/H265"
	case ".av1":
		return "video/AV1"
	case ".vc1":
		return "video/x-vc1"
	case ".vp8":
		return "video/VP8"
	case ".vp9":
		return "video/VP9"
	case ".wtv":
		return "video/x-wtv"
	case ".yuv":
		return "video/x-yuv"
	case ".str":
		return "video/x-str"
	case ".pva":
		return "video/x-pva"
	case ".trp":
		return "video/x-trp"
	case ".rec":
		return "video/x-rec"
	case ".vid":
		return "video/x-vid"
	case ".rv":
		return "video/vnd.rn-realvideo"
	case ".cpk":
		return "video/x-cpk"
	case ".film":
		return "video/x-film"

	case "js", "javascript":
		return "text/javascript"
	case:
		return "text/html"
	}
}

parse_type_hint :: proc(type_hint: string) -> string {
	switch type_hint {
	case "i", "image", "img":
		return "image"
	case "a", "audio":
		return "audio"
	case "v", "video":
		return "video"
	case "f", "iframe", "frame":
		return "iframe"
	case "s", "script", "js", "javascript":
		return "script"
	case:
		return ""
	}
}

detect_embed_type :: proc(url: string) -> string {
	mime := detect_mime_type(url)
	switch {
	case strings.has_prefix(mime, "image/"):
		return "image"
	case strings.has_prefix(mime, "audio/"):
		return "audio"
	case strings.has_prefix(mime, "video/"):
		return "video"
	case mime == "text/javascript" || mime == "application/javascript":
		return "script"
	case:
		return "iframe"
	}
}
