import { deviceKind } from '@/scripts/device-kind';

export function removeTracking(url: string): string {
	const isMobile = deviceKind !== 'desktop';

	let parsed;
	try {
		parsed = new URL(url);
	} catch {
		// parsing the URL failed, can't clean this
		return url;
	}

	if (['youtu.be', 'youtube.com', 'www.youtube.com'].includes(parsed.host)) {
		parsed.searchParams.delete('si'); // source identifier
	} else if (['heise.de', 'www.heise.de'].includes(parsed.host)) {
		parsed.searchParams.delete('wt_mc');
	} else if (['derstandard.at', 'www.derstandard.at'].includes(parsed.host)) {
		parsed.searchParams.delete('ref');
	} else if (!isMobile && /\.m\.wikipedia\.org$/i.test(parsed.host)) {
		parsed.host = parsed.host.replace('.m.', '.');
	} else {
		parsed.searchParams.delete('utm_source');
		parsed.searchParams.delete('utm_medium');
		parsed.searchParams.delete('utm_campaign');
		parsed.searchParams.delete('utm_term');
		parsed.searchParams.delete('utm_content');
	}

	return parsed.href;
}
