// Mapping for product families based on URL patterns
const productFamilyMapping = {
    "antivirus-mac": "Trend Micro Antivirus Mac",
    "antivirus-plus": "Trend Micro Antivirus+ Security",
    "internet-": "Trend Micro Internet Security",
    "maximum-security-for-mac": "Trend Micro Maximum Security For Mac",
    "maximum-security-plus": "Trend Micro Maximum Security Plus",
    "maximum-": "Trend Micro Maximum Security",
    "premium-security-it-helpdesk": "Trend Micro Premium Security IT Helpdesk",
    "premium-security-silver": "Trend Micro Premium Security Silver",
    "premium-security-suite": "Trend Micro Premium Security Suite",
    "premium-": "Trend Micro Premium Security",
    "device-security-basic": "Trend Micro Device Security Basic",
    "device-security-advanced": "Trend Micro Device Security Advanced",
    "device-security-pro": "Trend Micro Device Security Pro",
    "device-security-ultimate": "Trend Micro Device Security Ultimate",
    "security-suite-pro-plus": "Trend Micro Security Suite Pro Plus",
    "security-suite-pro": "Trend Micro Security Suite Pro",
    "security-suite-advanced": "Trend Micro Security Suite Advanced",
    "security-suite-ultimate": "Trend Micro Security Suite Ultimate",
    "mobile-": "Trend Micro Mobile Security",
    "id-s": "Trend Micro ID Security",
    "id-protection": "Trend Micro ID Protection",
    "wifi-": "Trend Micro WI-FI Protection",
    "trend-micro-vpn": "Trend Micro VPN",
    "trend-micro-check": "Trend Micro ScamCheck",
    "personal-protection-suite": "Trend Micro Personal Protection Suite",
    "cleaner-": "Trend Micro Cleaner Pro",
    "password-": "Trend Micro Password Manager",
    "homenetworksecurity": "Trend Micro Home Network Security",
    "it-helpdesk": "Trend Micro IT Helpdesk",
    "-installation": "Trend Micro Premium Installation",
    "-removal": "Virus and Spyware Removal",
    "-health-check": "Trend Micro PC Security Health Check",
    "product-comparison": "Comparison Page",
    "campaign": "Campaign Page"
};

// Get the product family from the URL
function getProductFamily() {
    const loc = window.location.href.toLowerCase();
	
	// Check if the URL is pointing to the homepage
    if (loc.endsWith('/') || loc.endsWith('default.asp') || loc.endsWith('default.asp')) {
        return "Homepage"; // Return a special value for the homepage
    }
	
    for (const pattern in productFamilyMapping) {
        if (loc.includes(pattern)) {
            return productFamilyMapping[pattern];
        }
    }
    return "Others";
}

function getPrice(imgObj, prodID, prodName, prodList, prodRRP, prodPrice) {
	prodID.push("productID");
	prodName.push("productName");
	prodList.push("productSKU");
	prodRRP.push("rrp");
	prodPrice.push("price");
}

function onProductClick(ProductID,ProductName,ProductCategory,ListName,ProductPrice,Position,EventAction) {
	FWEvent('SI', 'select_item', ProductID, ProductName, ProductCategory, ProductPrice, Position)
}

function addToCart(ListName, ProductID, ProductName, ProductCategory, ProductPrice, ProductPosition) {
	switch (ListName) {
	  case "BASIC":
	  case "RECOMMENDED":
	  case "COMPREHENSIVE":
		ListName = ["1"];
		break;
	}
	
	FWEvent('ADD', 'cart_add', ProductID, ProductName, ProductCategory, ProductPrice, ListName)
 }

function toLowerCase(value) { 
	if (Array.isArray(value)) { 
		return value.map(v => String(v).toLowerCase()); 
	} 
	if (value !== undefined && value !== null) { 
		return String(value).toLowerCase(); 
	}
	return value; 
}

var product_sku;
function FWEvent(src, ActionName, param1, param2, param3, param4, param5, param6, param7, param8) {
	if (typeof param1 !== "undefined" || param1 != null) {
		switch (param1) {
			case "MAC":
				product_sku = "Trend Micro Antivirus Mac";
				break;
			case "TAV":
				product_sku = "Trend Micro Antivirus+ Security";
				break;
			case "TIS":
				product_sku = "Trend Micro Internet Security";
				break;
			case "MCX":
				product_sku = "Trend Micro Maximum Security For Mac";
				break;
			case "MXP":
				product_sku = "Trend Micro Maximum Security Plus";
				break;
			case "MAX":
				product_sku = "Trend Micro Maximum Security";
				break;
			case "PRG":
				product_sku = "Trend Micro Premium Security IT Helpdesk";
				break;
			case "PRS":
				product_sku = "Trend Micro Premium Security Silver";
				break;
			case "PSS":
				product_sku = "Trend Micro Premium Security Suite";
				break;
			case "PRM":
				product_sku = "Trend Micro Premium Security";
				break;
			case "DSB":
				product_sku = "Trend Micro Device Security Basic";
				break;
			case "DSA":
				product_sku = "Trend Micro Device Security Advanced";
				break;
			case "DSP":
				product_sku = "Trend Micro Device Security Pro";
				break;
			case "DSU":
				product_sku = "Trend Micro Device Security Ultimate";
				break;
			case "SPP":
				product_sku = "Trend Micro Security Suite Pro Plus";
				break;
			case "SSP":
				product_sku = "Trend Micro Security Suite Pro";
				break;
			case "SSA":
				product_sku = "Trend Micro Security Suite Advanced";
				break;
			case "SSU":
				product_sku = "Trend Micro Security Suite Ultimate";
				break;
			case "TMS":
				product_sku = "Trend Micro Mobile Security";
				break;
			case "IDS":
				product_sku = "Trend Micro ID Security";
				break;
			case "IDP":
				product_sku = "Trend Micro ID Protection";
				break;
			case "IDA":
				product_sku = "Trend Micro ID Protection Advanced";
				break;
			case "WFP":
				product_sku = "Trend Micro WI-FI Protection";
				break;
			case "VPN":
				product_sku = "Trend Micro VPN";
				break;
			case "TMC":
				product_sku = "Trend Micro ScamCheck";
				break;
			case "PPS":
				product_sku = "Trend Micro Personal Protection Suite";
				break;
			case "CLP":
				product_sku = "Trend Micro Cleaner Pro";
				break;
			case "PWM":
				product_sku = "Trend Micro Password Manager";
				break;
			case "HNS":
				product_sku = "Trend Micro Home Network Security";
				break;
			case "ITH":
				product_sku = "Trend Micro IT Helpdesk";
				break;
			case "PRI":
				product_sku = "Trend Micro Premium Installation";
				break;
			case "VRS":
				product_sku = "Virus and Spyware Removal";
				break;
			case "SHC":
				product_sku = "Trend Micro PC Security Health Check";
				break;
		}
	}
	
	if (param3 != "")
		product_name = product_sku + ", " + (param2 && param2 !== "NA" ? param2 + " Device " : "") + param3 + " mth";
	
	const product_family = getProductFamily();
	switch(src) {
	  case "FC":
			switch(ActionName) {
			  case "Open":
				utag.link({
					enh_action: "click",
					event_category: "FC Interactions",
					event_action: "Start Session",
					event_label: [FC] + "-" + ActionName,
					tealium_event: 'freshchat_open'
				});
				break;
			  case "Close":
				utag.link({
					enh_action: "click",
					event_category: "FC Interactions",
					event_action: "End Session",
					event_label: [FC] + "-" + ActionName,
					tealium_event: 'freshchat_close'
				});
				break;
			}
	  case "AIF":
			utag.link({
				tealium_event: ActionName,
				popup_content: param1
			});
			break;
	  case "CS":
			utag.link({
				tealium_event: 'country_switch',
				link_text: ActionName,
				link_url: param1
			});
			break;
	  case "TEL":
			utag.link({
				tealium_event: 'click_tel',
				link_text: ActionName,
				link_url: param1
			});
			break;
	  case "LC":
			utag.link({
				tealium_event: ActionName,
				product_family: product_family
			});
			break;
	  case "ACC":
			utag.link({
				tealium_event: ActionName,
				product_family: product_family,
				accordion_title: param1
			});
			break;
	  case "OL":
			utag.link({
				tealium_event: ActionName,
				product_family: product_family,
				overlay_content: param1
			});
			break;
	  case "UP":
			utag.link({
				tealium_event: param1,
				product_family: product_family,
				overlay_content: ActionName
			});
			break;
	  case "PO":
			utag.link({
				tealium_event: ActionName,
				product_family: product_family,
				product_name: product_name,
				product_device_count: param2,
				product_subscription_months: param3
			});
			break;
	  case "PA":
			utag.link({
				tealium_event: ActionName,
				product_family: product_family,
				anchor_content: param1
			});
			break;
	  case "VIL":
			if (Array.isArray(param7) && !param7.some(value => value.includes('Home Page')))
				param3 = param7;
				
			param1 = toLowerCase(param1);
			param6 = toLowerCase(param6);
			
			utag.link({
				tealium_event: ActionName,
				product_id: param1,
				product_name: param2,
				product_sku: param1,
				product_category: param6,
				product_price: param5,
				product_list_id: param3,
				product_list_name: param3,
				product_position: param8
			});
			break;
	  case "SI":
			param6 = (typeof param2 === "undefined" || param2.includes('Renewal')) ? ['Renew'] : (param2.includes('Upgrade') ? ['Upgrade'] : ['New']);
			
			param1 = toLowerCase(param1);
			param6 = toLowerCase(param6);
			
			utag.link({
				tealium_event: ActionName,
				product_id: [param1],
				product_name: [param2],
				product_sku: [param1],
				product_category: param6,
				product_price: [param4],
				product_list_id: [product_family],
				product_list_name: [product_family],
				product_position: ["1"]
			});
			break;
	  case "VI":
			param6 = typeof param6 === 'undefined' || (Array.isArray(param6) && !param6.some(value => value.includes('Renew'))) ? ['New'] : param6;

			param1 = toLowerCase(param1);
			param6 = toLowerCase(param6);
			
			utag.link({
				tealium_event: ActionName,
				order_currency_code: '"' + varStoreCurrency + '"',
				event_value: '"' + param5 + '"',
				product_id: [param1],
				product_name: [param2],
				product_sku: [param1],
				product_category: param6,
				product_price: [param5],
				product_list_id: [product_family],
				product_list_name: [product_family],
				product_position: ["1"],
				product_retail_price: [param4]
			});
			break;
	  case "ADD":
			if (typeof param5 === "undefined" || param5 === "")
				param5 = ["1"];
			param6 = (typeof param2 === "undefined" || param2.includes('Renewal')) ? ['Renew'] : (param2.includes('Upgrade') ? ['Upgrade'] : ['New']);
				
			param1 = toLowerCase(param1);
			param6 = toLowerCase(param6);
			
			/*utag.link({
				tealium_event: ActionName,
				order_currency_code: '"' + varStoreCurrency + '"',
				event_value: '"' + param4 + '"',
				product_id: [param1],
				product_name: [param2],
				product_sku: [param1],
				product_category: param6,
				product_price: [param4],
				product_list_id: [product_family],
				product_list_name: [product_family],
				product_position: param5
			});*/
			break;
	  case "VP":
			utag.link({
				tealium_event: ActionName,
				creative_name: param1,
				creative_slot: param2,
				promotion_id: param3,
				promotion_name: param4
			});
			break;
	  case "SP":
			utag.link({
				tealium_event: ActionName,
				creative_name: param1,
				creative_slot: param2,
				promotion_id: param3,
				promotion_name: param4
			});
			break;
	}
}