export const sendGoal = async (goal, params) => {
	if (typeof window.ym === "function") {
		window.ym(97676450, "reachGoal", goal, params);
	} else {
		console.warn("Yandex.Metrika failed");
	}

	try {
		const formData = new FormData();
		formData.append("event_name", goal);
		formData.append("event_category", "goal");
		formData.append("event_action", "reach");
		formData.append("page_url", window.location.href);
		formData.append("page_title", document.title);

		if (document.referrer) {
			formData.append("page_previous", document.referrer);
		}

		if (params) {
			formData.append("event_params", JSON.stringify(params));
		}

		await fetch("https://setka.ru/analytics/send-event", {
			method: "POST",
			body: formData,
			headers: {
				Referer: window.location.href,
			},
		});
	} catch (error) {
		console.warn("Backend analytics failed:", error);
	}
};

export { typo } from "./typo";