var B2C_UrlUtility = (function () {
	return {
		isProdUrl: isProdUrl,
		getEnvironmentUrl: getEnvironmentUrl,
		verifyRedirectUrl: verifyRedirectUrl,
		getProductDetailsSeatingQuiz: getProductDetailsSeatingQuiz

	};

	function verifyRedirectUrl(redirectUrl, fallbackUrl = CCRZ.pagevars.currSiteURL) {
		let isValid = false;
	  	const absoluteUrlExp = new RegExp('^(?:[a-z]+:)?//', 'i');
	  	const trustedDomains = [
								CCRZ.pagevars.currSiteURL
							];
	  
		if(absoluteUrlExp.test(redirectUrl)) {
			_.each(trustedDomains, function(domain) {
				if (new URL(domain).origin.toLowerCase() === new URL(redirectUrl).origin.toLowerCase()) {
					isValid = true;
				}
			});
		} else {
			isValid = true;
	  	}
	  return isValid ? redirectUrl : fallbackUrl;
	}

	function isProdUrl() {
		return !window.location.href.includes('.com/Store');
	}

	function getEnvironmentUrl(url) {
		if (typeof url != 'string') {
			return null;
		}
		if (!window.location.href.includes('.com/Store')) {
			if (url.includes('/Store')) {
				url = url.replace("/Store", "");
				console.log('url **' + url);
			}
		} else if (window.location.href.includes('.com/Store')
			&& !url.includes('.com')
			&& url.startsWith('/')
			&& !url.startsWith('/Store')) {
			url = '/Store' + url;
		}

		if (CCRZ.pagevars.queryParams.cclcl) {
			let userLocale = CCRZ.pagevars.userLocale;
			url += (url.includes('?') ? '&' : '?') + 'cclcl=' + CCRZ.pagevars.userLocale;
		}

		return url;
	}
	
	function getProductDetailsSeatingQuiz(productIds) {
		let productIdList = [];
		productIdList = productIds.split(',');
		const remote = _.extend(CCRZ.RemoteInvocation, { className: 'B2C_TCMSTemplateController' })
		let responseJSON = new Promise(function(resolve, reject) {
			remote.invokeCtx('getProductDetailsSeatingQuiz', productIdList, (res, err) => {
				if (res.success) {
					let prod;
					for (sku in res.data) {
						prod = res.data[sku];
						prod.productPrice = Handlebars.helpers.roundPriceRanged(prod.price, prod.loPrice, prod.hiPrice, 'USD');
					}
					resolve(res);
					JSONresp = res.data;
				} else {
					reject(err);
				}
			},
			{
				nmsp: false
			})
		});
		return responseJSON;
	}
	
})();
