/**
 * Centralized configuration for URLs and constants
 * URLs are dynamically set based on environment (localhost = staging, production = live)
 */

(function (window) {
	"use strict";

	// Get frontend URL from localized script (set by PHP based on environment)
	const getFrontendUrl = function () {
		return window.scanshowAdmin && window.scanshowAdmin.frontendUrl
			? window.scanshowAdmin.frontendUrl
			: "https://scan-the-world.com"; // Fallback to production
	};

	// Build external URLs dynamically based on environment
	const buildExternalUrls = function () {
		const frontendUrl = getFrontendUrl();
		return {
			STW_DASHBOARD: frontendUrl + "dashboard",
			STW_SETTINGS: frontendUrl + "settings",
			STW_PRICING: frontendUrl + "pricing",
			STW_CREDITS: frontendUrl + "credits",
			STW_SUPPORT: frontendUrl + "contact",
			STW_TUTORIALS: frontendUrl + "tutorial",
			STW_ABOUT: frontendUrl + "",
			STW_PRIVACY: frontendUrl + "eula/",
			STW_CONTACT: frontendUrl + "contact",
			STW_REGISTER: frontendUrl + "register",
		};
	};

	window.STW_CONFIG = {
		// Dynamic external URLs based on environment
		get EXTERNAL_URLS() {
			return buildExternalUrls();
		},

		//check the ADMIN MENU registered in stw-wordpress.php, use the menu_slug
		ADMIN_URLS: {
			ADD_PRODUCT: "post-new.php?post_type=product",
		},

		// WordPress Site Editor URLs
		EDITOR_URLS: {
			SINGLE_PRODUCT_TEMPLATE: "/wp-admin/site-editor.php",
		},

		THEME: {
			// Get current theme from localized data
			getCurrentTheme: function () {
				return window.scanshowAdmin?.themeSlug || "twentytwentyfive";
			},

			// Get template slug
			getTemplateSlug: function () {
				return window.scanshowAdmin?.templateSlug || "single-product";
			},
		},

		/**
		 * Get full admin URL
		 */
		getAdminUrl: function (path) {
			const baseUrl =
				window.scanshowAdmin?.adminUrl || window.location.origin + "/wp-admin/";
			return baseUrl + path;
		},

		/**
		 * Get site editor URL for single product template
		 * Uses server-generated URL from PHP
		 */
		getSingleProductEditorUrl: function () {
			// Use pre-generated URL from PHP
			if (window.scanshowAdmin?.singleProductEditorUrl) {
				return window.scanshowAdmin.singleProductEditorUrl;
			}

			// Fallback: construct URL dynamically using PHP-provided values
			const theme = this.THEME.getCurrentTheme();
			const templateSlug = this.THEME.getTemplateSlug();

			const templatePath = encodeURIComponent(
				`/wp_template/${theme}//${templateSlug}`,
			);

			return this.getAdminUrl(`site-editor.php?p=${templatePath}&canvas=edit`);
		},

		navigateToAdmin: function (path) {
			window.location.href = this.getAdminUrl(path);
		},

		navigateToRoute: function (route) {
			if (window.STW_Router) {
				window.STW_Router.navigate(route, true);
			}
		},

		openExternal: function (url) {
			window.open(url, "_blank");
		},
	};
})(window);
