import { getLocaleData } from "@wordpress/i18n";

const getDocsLanguageSegment = () => {
	const data = getLocaleData("zenblocks") || getLocaleData();
	const rawLocale = String((data && data[""] && data[""].lang) || "").toLowerCase().replace("-", "_");

	if (rawLocale.startsWith("ja")) return "ja";
	if (rawLocale.startsWith("de")) return "de";
	if (rawLocale.startsWith("es")) return "es";
	if (rawLocale.startsWith("fr")) return "fr";
	if (rawLocale.startsWith("id")) return "id";
	if (rawLocale.startsWith("it")) return "it";
	if (rawLocale.startsWith("ko")) return "ko";
	if (rawLocale.startsWith("nl")) return "nl";
	if (rawLocale.startsWith("pt")) return "pt-br";
	if (rawLocale.startsWith("zh")) return "zh-tw";
	return "en";
};

/**
 * Generate a localized URL for zenblockswp.com.
 * @param {string} path - e.g. "/downloads/" or "/pricing/"
 * @returns {string} Full URL like "https://zenblockswp.com/ja/downloads/"
 */
export const getLocalizedUrl = (path) => {
	if (!path) return "#";
	if (/^https?:\/\//i.test(path)) return path;

	const lang = getDocsLanguageSegment();
	const normalizedPath = path.startsWith("/") ? path : `/${path}`;
	return `https://zenblockswp.com/${lang}${normalizedPath}`;
};
