/**
 * Legacy PPOM price preview engine.
 *
 * Older templates render pricing hints directly as data attributes on DOM
 * inputs. This file scans those inputs, builds the hidden `ppom_option_price`
 * payload consumed later in WooCommerce/cart hooks, and renders the visible
 * price summary for legacy mode.
 */
'use strict';

const PPOMWrapper = jQuery( 'body.woocommerce, body.woocommerce-js' );
let ppomPriceListContainer = '';
let ppomPriceListContainerRow = '';
// Quantity update capture/update price change
const wc_product_qty = jQuery( 'form.cart' ).find( 'input[name="quantity"]' );
let ppom_product_base_price = ppom_input_vars.wc_product_price;

jQuery( function ( $ ) {
	// Rebuild the preview immediately so defaults and preselected values show up.
	ppom_update_option_prices();

	// If quantity is changing with some -/+ elements
	$( 'form.cart .quantity' ).on( 'click', function ( e ) {
		e.preventDefault();
		ppom_update_option_prices();
	} );

	wc_product_qty.on( 'change keyup', function ( e ) {
		ppom_update_option_prices();

		$.event.trigger( {
			type: 'ppom_wc_qty_updated',
			qty: ppom_get_order_quantity(),
			time: new Date(),
		} );
	} );

	PPOMWrapper.on(
		'change',
		'select,input:checkbox,input:radio, input:text',
		function ( e ) {
			ppom_update_option_prices();
		}
	);

	PPOMWrapper.on( 'keyup change', 'input:text.ppom-priced', function ( e ) {
		ppom_update_option_prices();
	} );

	// quantities input changes
	if ( $( '.ppom-input-quantities' ).length > 0 ) {
		PPOMWrapper.on(
			'click change keyup',
			'input.ppom-quantity',
			function () {
				ppom_update_option_prices();
			}
		);
	}

	if ( $( '.ppom-input-quantities' ).length > 0 ) {
		PPOMWrapper.on( 'change', 'input.ppom-quantity', function () {
			const dataname = $( this )
				.closest( '.ppom-field-wrapper' )
				.attr( 'data-data_name' );

			const _is_valid = ppom_quantities_min_max( dataname );
			if ( _is_valid ) {
				$( this ).val( 0 );
				ppom_quantities_min_max( dataname );
			}
		} );
		// $("input.ppom-quantity").trigger("change");
	}

	// WC Variation Quantity Change event
	$( '.ppom-wcvq-input' ).on( 'keyup change', function () {
		// console.log('text')
		ppom_update_option_prices();
	} );

	// Woocommmerce vartiation update events
	PPOMWrapper.on(
		'show_variation',
		'.single_variation_wrap',
		function ( event, variation ) {
			// Fired when the user selects all the required dropdowns / attributes
			// and a final variation is selected / shown
			// console.log(variation);
			let variation_price = variation.display_price;

			// WholeSale Price
			if ( variation.wholesale_price !== undefined ) {
				variation_price = variation.wholesale_price;
			}

			$( '.ppom-variable-option.ppom-option-has-percent' ).each(
				function ( i, option ) {
					const option_percent = $( option ).attr( 'data-percent' );
					const option_price = ppom_get_amount_after_percentage(
						variation_price,
						option_percent
					);
					$( option ).attr( 'data-price', option_price );
				}
			);

			ppom_product_base_price = variation_price;
			ppom_update_variation_quatity( variation_price );
			ppom_update_option_prices();
		}
	);

	$( '.single_variation_wrap' ).on(
		'hide_variation',
		function ( event, variation ) {
			// Fired when the user selects all the required dropdowns / attributes
			// and a final variation is selected / shown

			$( '.ppom-variable-option.ppom-option-has-percent' ).each(
				function ( i, option ) {
					$( option ).attr( 'data-price', 0 );
				}
			);

			ppom_product_base_price = 0;
			ppom_update_option_prices();
		}
	);

	// Measue input creating price-checkbox on change
	PPOMWrapper.on( 'click change keyup', '.ppom-measure-input', function () {
		const data_name = $( this ).attr( 'id' );
		const m_qty = $( this ).val();

		// console.log(data_name);
		$( 'input:radio[name="ppom[unit][' + data_name + ']"]:checked' ).attr(
			'data-qty',
			m_qty
		);
		ppom_update_option_prices();
	} );

	// Delete option from price table
	// Since 16.4
	$( document ).on( 'click', '.ppom-delete-option-table', function ( e ) {
		const field_name = $( this ).closest( 'tr' ).attr( 'data-data_name' );
		const option_id = $( this ).closest( 'tr' ).attr( 'data-option_id' );
		if ( field_name ) {
			ppom_delete_option_from_price_table( field_name, option_id );
		}
	} );
} );

// Main legacy rebuild pass: normalize selections, refresh hidden payloads, and
// redraw the price table exactly as the PHP cart hooks expect to receive them.
function ppom_update_option_prices() {
	if ( ppom_input_vars.show_option_price == 'hide' ) {
		jQuery( '#ppom-price-container' ).hide();
	}

	let ppom_option_total = 0;
	let ppom_total_discount = 0;
	let productBasePrice = 0;

	const ppom_all_option_prices = ppom_update_get_prices();
	// Flags
	let ppom_has_variable = false;
	let ppom_has_onetime = false;
	let ppom_has_matrix = false;
	let ppom_has_priced_quantities = false; // variation quantities
	let ppom_show_base_price = true;
	const show_option_price_indivisually =
		ppom_input_vars.show_option_price == 'all_option' ? true : false;
	let show_per_unit_price = false;
	// unlink order quantity controlled by variation quantities
	let unlink_order_qty = false;

	// console.log(ppom_all_option_prices);

	// Set hidden input
	jQuery( '#ppom_option_price' ).val(
		JSON.stringify( ppom_all_option_prices )
	);

	const ppomPriceContainer = jQuery( '#ppom-price-container' );
	// Reset container
	ppomPriceContainer.html( '' );

	/*ppomPriceListContainer = jQuery('<ul/>')
                                .addClass('ppom-option-price-list')
                                .appendTo(ppomPriceContainer);*/
	ppomPriceListContainer = jQuery( '<table/>' )
		.addClass( 'table table-striped' )
		.appendTo( ppomPriceContainer )
		.css( 'width', '100%' );

	/** ====== Matrix Price =========== */
	jQuery( ppom_all_option_prices ).each( function ( i, option ) {
		// console.log(option);

		// Updating flash in first loop
		if ( option.apply == 'variable' ) {
			ppom_has_variable = true;
		}
		if ( option.apply == 'onetime' ) {
			ppom_has_onetime = true;
		}

		// Sum only variable prices
		if ( option.apply !== 'matrix' ) {
			return;
		}

		ppom_has_matrix = true;
		ppom_show_base_price = false;
		if ( ppom_input_vars.show_price_per_unit ) {
			show_per_unit_price = true;
		}

		const price_tag = ppom_get_wc_price( option.price );

		const option_label_with_qty =
			option.label +
			' [ ' +
			jQuery( price_tag ).html() +
			' x ' +
			ppom_get_order_quantity() +
			']';
		// console.log(option);

		// option.apply = 'matrix-fixed';
		if ( option.matrix_fixed ) {
			var matrix_price = parseFloat( option.price ) * 1;
		} else {
			var matrix_price =
				parseFloat( option.price ) * ppom_get_order_quantity();
		}

		ppom_add_price_item_in_table(
			option_label_with_qty,
			matrix_price,
			'ppom-matrix-price'
		);

		// Totals the options
		// ppom_option_total += parseFloat(matrix_price);
		// Set productBasePrice as matrix found
		productBasePrice = matrix_price;
	} );
	/** ====== Matrix Price=========== */

	/** ====== Variation quantities =========== */
	jQuery( ppom_all_option_prices ).each( function ( i, option ) {
		// Sum only variable prices
		if ( option.apply !== 'quantities' ) {
			return;
		}

		// If Matrix price found then do not calculate each option prices
		if ( ppom_has_matrix ) {
			return;
		}

		// console.log(option);

		if ( option.include === 'on' ) {
			ppom_has_priced_quantities = true;
		}

		if ( option.unlink_qty === 'on' ) {
			unlink_order_qty = true;
		}

		// console.log('max price', option);

		// wc_product_qty.val(1);

		// since v18.0 if no price set, no need to use base price
		if ( option.price == '' || option.price == 0 ) {
			return;
		}

		const variation_price = option.price;

		let option_price_with_qty =
			parseFloat( option.quantity ) * parseFloat( variation_price );
		if ( unlink_order_qty ) {
			option_price_with_qty *= ppom_get_order_quantity();
		}
		// Totals the options
		ppom_option_total += option_price_with_qty;

		if ( ! show_option_price_indivisually ) {
			return;
		}

		const price_tag = ppom_get_wc_price( variation_price );
		const option_label_with_qty =
			option.label +
			' ' +
			jQuery( price_tag ).html() +
			' x ' +
			option.quantity;

		ppom_add_price_item_in_table(
			option_label_with_qty,
			option_price_with_qty,
			'ppom-quantities-price'
		);
	} );
	/** ====== Variation quantities =========== */

	/** ====== WC Variation quantities Addon =========== */
	jQuery( ppom_all_option_prices ).each( function ( i, option ) {
		// Sum only variable prices
		if ( option.apply !== 'wc_variation_quantities' ) {
			return;
		}

		// do not include baseprice when wcvq
		ppom_show_base_price = false;

		if ( option.include === 'on' ) {
			ppom_has_priced_quantities = true;
		}

		// since v18.0 if no price set, no need to use base price
		if ( option.price == '' || option.price == 0 ) {
			return;
		}

		const variation_price = option.price;
		const option_price_with_qty =
			parseFloat( option.quantity ) * parseFloat( variation_price );
		// Totals the options
		ppom_option_total += option_price_with_qty;

		// if (!show_option_price_indivisually) return;
		//console.log(option);

		const price_tag = ppom_get_wc_price( variation_price );
		const option_label_with_qty =
			option.label +
			' ' +
			jQuery( price_tag ).html() +
			' x ' +
			option.quantity;

		ppom_add_price_item_in_table(
			option_label_with_qty,
			option_price_with_qty,
			'ppom-quantities-price'
		);
	} );
	/** ====== Variation quantities Addon=========== */

	/** ====== Bulkquantity Addon =========== */
	jQuery( ppom_all_option_prices ).each( function ( i, option ) {
		// Sum only variable prices
		if ( option.apply !== 'bulkquantity' ) {
			return;
		}

		if ( option.include !== 'on' ) {
			ppom_show_base_price = false;
		}

		const quantity = option.quantity;

		// console.log(option);
		const option_label_with_qty =
			option.label +
			' ' +
			ppom_get_formatted_price( option.price ) +
			' x ' +
			quantity;

		const option_price_with_qty =
			parseFloat( quantity ) * parseFloat( option.price );
		ppom_add_price_item_in_table(
			option_label_with_qty,
			option_price_with_qty,
			'ppom-bulkquantity-price'
		);
		ppom_option_total += option_price_with_qty;

		// Base price
		const base_price_tag = ppom_get_wc_price( option.base );
		const base_label =
			ppom_input_vars.product_base_label +
			' ' +
			jQuery( base_price_tag ).html() +
			' x ' +
			quantity;
		const lineBaseTotal = parseFloat( option.base ) * quantity;
		ppom_add_price_item_in_table(
			base_label,
			lineBaseTotal,
			'ppom-bulkquantity-baseprice'
		);
		ppom_option_total += lineBaseTotal;
	} );
	/** ====== Bulkquantity Addon =========== */

	/** ====== Event Calendar Add-on =========== */
	jQuery( ppom_all_option_prices ).each( function ( i, option ) {
		// Sum only variable prices
		if ( option.apply !== 'eventcalendar' ) {
			return;
		}

		// If Matrix price found then do not calculate each option prices
		if ( ppom_has_matrix ) {
			return;
		}

		//  wc_product_qty.val(1);

		// since v18.0 if no price set, no need to use base price
		if ( option.price == '' || option.price == 0 ) {
			return;
		}

		const variation_price = option.price;
		const option_price_with_qty =
			parseFloat( option.quantity ) * parseFloat( variation_price );
		// console.log(option_price_with_qty);
		// Totals the options
		ppom_option_total += option_price_with_qty;

		if ( ! show_option_price_indivisually ) {
			return;
		}

		const price_tag = ppom_get_wc_price( variation_price );
		const option_label_with_qty =
			option.label +
			' ' +
			jQuery( price_tag ).html() +
			' x ' +
			option.quantity;

		ppom_add_price_item_in_table(
			option_label_with_qty,
			option_price_with_qty,
			'ppom-eventcalendar-price'
		);
	} );
	/** ====== Event Calendar Add-on =========== */

	/** ====== Options price variable =========== */
	jQuery( ppom_all_option_prices ).each( function ( i, option ) {
		if ( option.apply !== 'variable' ) {
			return;
		}

		const option_price_with_qty =
			ppom_get_order_quantity() * parseFloat( option.price );
		// Totals the options
		ppom_option_total += option_price_with_qty;

		// Check if to shos options or not
		if ( ! show_option_price_indivisually ) {
			return;
		}
		const price_tag = ppom_get_wc_price( option.price );

		const option_label_with_qty =
			option.label +
			' ' +
			jQuery( price_tag ).html() +
			' x ' +
			ppom_get_order_quantity();

		ppom_add_price_item_in_table(
			option_label_with_qty,
			option_price_with_qty,
			'ppom-variable-price',
			'',
			option
		);
	} );
	/** ====== Options price variable =========== */

	/** ====== Options price onetime/fixed =========== */

	// Heading Fixed Fee
	// Deprecated - Should be removed soon.
	if ( ppom_has_onetime ) {
		ppom_add_price_item_in_table(
			ppom_input_vars.fixed_fee_heading,
			'',
			'ppom-fixed-fee-heading'
		);
	}

	jQuery( ppom_all_option_prices ).each( function ( i, option ) {
		// Sum only variable prices
		if ( option.apply !== 'onetime' ) {
			return;
		}
		const option_label_with_qty = option.label;

		ppom_add_price_item_in_table(
			option_label_with_qty,
			option.price,
			'ppom-fixed-price',
			'',
			option
		);

		// Totals the options
		ppom_option_total += parseFloat( option.price );
	} );
	/** ====== Options price onetime/fixed=========== */

	/** ====== Options total price =========== */
	if ( ppom_has_variable ) {
		ppom_add_price_item_in_table(
			ppom_input_vars.option_total_label,
			ppom_option_total,
			'ppom-option-total-price'
		);
	}
	/** ====== Options total price =========== */

	/** ====== Fixed Price Addon =========== */
	jQuery( ppom_all_option_prices ).each( function ( i, option ) {
		// Sum only variable prices
		if ( option.apply !== 'fixedprice' ) {
			return;
		}
		ppom_product_base_price = option.unitprice;
		ppom_set_order_quantity( option.quantity );
	} );

	/** ====== Hide Product total price is zero ===== */

	if ( ppom_product_base_price == 0 ) {
		ppom_show_base_price = false;
	}
	/** ====== Hide Product total price is zero ===== */

	/** ====== Product base price =========== */

	if ( ppom_show_base_price ) {
		//console.log(ppom_show_base_price);

		const price_tag = ppom_get_wc_price( ppom_product_base_price );

		// if variation quantities has price
		let product_qty = ppom_get_order_quantity();
		if (
			ppom_has_priced_quantities &&
			product_qty > 0 &&
			! unlink_order_qty
		) {
			product_qty = 1;
		}
		// console.log('price_tag', price_tag);
		const product_base_label =
			ppom_input_vars.product_base_label +
			' ' +
			jQuery( price_tag ).html() +
			' x ' +
			product_qty;
		productBasePrice = product_qty * parseFloat( ppom_product_base_price );
		ppom_add_price_item_in_table(
			product_base_label,
			productBasePrice,
			'ppom-product-base-price'
		);
	}
	/** ====== Product base price =========== */

	/** ====== Apply Discounts =========== */
	jQuery( ppom_all_option_prices ).each( function ( i, option ) {
		if ( option.apply !== 'matrix_discount' ) {
			return;
		}

		const is_percent = option.percent !== '' ? true : false;

		if ( ppom_input_vars.show_price_per_unit ) {
			show_per_unit_price = true;
		}

		if ( is_percent ) {
			var option_label_with_qty =
				ppom_input_vars.total_discount_label +
				' (' +
				option.percent +
				')';
		} else {
			const price_tag = ppom_get_wc_price( option.price );
			var option_label_with_qty =
				ppom_input_vars.total_discount_label + ' ' + price_tag.html();
		}

		if ( is_percent ) {
			if ( ppom_pricematrix_discount_type === 'base' ) {
				// if discount apply on base
				var total_item_price = productBasePrice;
			} else {
				var total_item_price =
					parseFloat( productBasePrice ) +
					parseFloat( ppom_option_total );
			}

			const discount_percent = ppom_get_formatted_price(
				parseFloat( option.percent )
			);
			// ppom_total_discount        += ppom_get_formatted_price(parseFloat(discount_percent/100) * parseFloat(total_item_price));
			ppom_total_discount = parseFloat(
				( parseFloat( option.percent ) / 100 ) * total_item_price
			);
		} else {
			ppom_total_discount += option.price;
		}

		ppom_add_price_item_in_table(
			option_label_with_qty,
			ppom_total_discount,
			'ppom-discount-price'
		);

		// Totals the options
		// ppom_option_total += parseFloat(option.price);
	} );
	/** ====== Apply Discounts =========== */

	// console.log('total_discount '+ppom_total_discount);
	// console.log('baseprice '+parseFloat(productBasePrice));
	// console.log('option_total '+parseFloat(ppom_option_total));

	/*productBasePrice    = productBasePrice || 0;
    ppom_option_total   = ppom_option_total || 0;
    ppom_total_discount = ppom_total_discount || 0;*/

	/** ====== Total without fixed/onetime fee =========== */
	let ppom_total_price = ppom_calculate_totals(
		ppom_total_discount,
		productBasePrice,
		ppom_option_total
	);
	const total_price_label = ppom_input_vars.total_without_fixed_label;
	// console.log(ppom_total_price);

	/** ====== Measures ===================*/
	let ppom_measure_quantity = 1;
	let ppom_measure_input_found = false;
	jQuery( ppom_all_option_prices ).each( function ( i, option ) {
		// Sum only variable prices
		if ( option.apply !== 'measure' ) {
			return;
		}
		ppom_show_base_price = false;

		ppom_measure_input_found = true;

		const multiplier = parseFloat( option?.price_multiplier ?? '1' );
		const priceMultiplier = ! Number.isNaN( multiplier ) ? multiplier : 1;

		if ( option.use_units === 'no' ) {
			const option_qty = option.qty == '' ? 0 : parseFloat( option.qty );
			ppom_measure_quantity =
				option_qty * ppom_measure_quantity * priceMultiplier;
		} else {
			ppom_total_price +=
				parseFloat( option.price ) *
				priceMultiplier *
				parseFloat( option.qty ) *
				ppom_get_order_quantity();
		}
	} );

	// If measured input has quantities
	if ( ppom_measure_input_found ) {
		ppom_total_price =
			productBasePrice * parseFloat( ppom_measure_quantity ) +
			ppom_option_total;
	}

	const per_unit_price = parseFloat(
		ppom_total_price / ppom_get_order_quantity()
	);
	let per_unit_label = '';

	// console.log(show_per_unit_price);
	if ( show_per_unit_price && ppom_get_order_quantity() > 0 ) {
		per_unit_label =
			' (' +
			ppom_get_wc_price( per_unit_price ).html() +
			' / ' +
			ppom_input_vars.per_unit_label +
			')';
	}

	/** ====== Total without fixed/onetime fee =========== */
	if ( ppom_input_vars.tax_prefix ) {
		per_unit_label += ' ' + ppom_input_vars.tax_prefix;
	}
	ppom_add_price_item_in_table(
		total_price_label,
		ppom_total_price,
		'ppom-total-without-fixed',
		per_unit_label
	);
}

function ppom_calculate_totals(
	ppom_total_discount,
	productBasePrice,
	ppom_option_total
) {
	const totals =
		parseFloat( productBasePrice ) +
		parseFloat( ppom_option_total ) -
		parseFloat( ppom_total_discount );
	return totals;
}

// Render a single row in the legacy price table and emit the historic hook.
function ppom_add_price_item_in_table(
	label,
	price,
	item_class,
	append_to_price,
	option
) {
	let formatted_price = '';
	let row_id = '';
	let row_data_name = '';
	if ( option !== undefined ) {
		row_id = option.option_id;
		row_data_name = option.data_name;
	}

	if ( price == 0 ) {
		return;
	}

	if ( price !== '' ) {
		if ( item_class === 'ppom-discount-price' ) {
			formatted_price = ppom_get_wc_price( price, true );
		} else {
			formatted_price = ppom_get_wc_price( price );
		}
	}

	if ( append_to_price !== undefined ) {
		formatted_price = formatted_price.html() + append_to_price;
	}

	ppomPriceListContainerRow = jQuery( '<tr/>' )
		.addClass( 'ppom-option-price-list' )
		.attr( 'data-option_id', row_id )
		.attr( 'data-data_name', row_data_name )
		.appendTo( ppomPriceListContainer );

	if ( item_class ) {
		ppomPriceListContainerRow.addClass( item_class );
	}

	// Delete Option
	if ( row_data_name ) {
		label =
			'<span class="fa fa-times ppom-delete-option-table" title="Remove"></span> ' +
			label;
	}

	// Label Item
	var totalWithoutFixedLabel = jQuery( '<th/>' )
		.html( label )
		.addClass( 'ppom-label-item' )
		.appendTo( ppomPriceListContainerRow );

	if ( price === '' ) {
		totalWithoutFixedLabel.attr( 'colspan', '2' );
	} else {
		// Price Item
		var totalWithoutFixedLabel = jQuery( '<th/>' )
			.html( formatted_price )
			.addClass( 'ppom-price-item' )
			.appendTo( ppomPriceListContainerRow );
	}

	jQuery.event.trigger( {
		type: 'ppom_option_price_added',
		price,
		item: item_class,
		time: new Date(),
	} );
}

// Adding Li item in price container
function ppom_add_price_item_in_list( label, price, item_class ) {
	var item_class = item_class || '';

	const ppomListItem = jQuery( '<li/>' )
		.addClass( item_class )
		.appendTo( ppomPriceListContainer );
	// Label Item
	var totalWithoutFixedLabel = jQuery( '<span/>' )
		.html( label )
		.addClass( 'ppom-label-item' )
		.appendTo( ppomListItem );

	// Price Item
	var totalWithoutFixedLabel = jQuery( '<span/>' )
		.html( price )
		.addClass( 'ppom-price-item' )
		.appendTo( ppomListItem );
}

function ppom_get_wc_price( price, is_discount ) {
	const do_discount = is_discount || false;
	const wcPriceWithCurrency = jQuery( '#ppom-price-cloner' )
		.clone()
		.removeAttr( 'id' );
	const is_negative = parseFloat( price ) < 0;

	if ( is_negative ) {
		price = Math.abs( price );
	}

	const ppom_formatted_price = ppom_get_formatted_price( price );

	wcPriceWithCurrency.find( '.ppom-price' ).html( ppom_formatted_price );

	// Adding (-) symbol
	if ( do_discount || is_negative ) {
		wcPriceWithCurrency.prepend( '-' );
	}
	return wcPriceWithCurrency;
}

// Scan every rendered PPOM input and normalize the chosen values into the
// `ppom_option_price` shape that frontend previews and PHP cart logic share.
function ppom_update_get_prices() {
	const options_price_added = [];

	PPOMWrapper.find( 'select,input:checkbox,input:radio,input:text' ).each(
		function ( i, input ) {
			// if fielld is hidden
			if (
				jQuery( this )
					.closest( '.ppom-field-wrapper' )
					.hasClass( 'ppom-c-hide' )
			) {
				return;
			}

			// if fixedprice (addon) then return
			if ( jQuery( this ).hasClass( 'fixedprice' ) ) {
				return;
			}
			// if (jQuery("option:selected", this).attr('data-unitprice') !== undefined) return;
			// if( jQuery("option:selected", this).attr('data-price') === undefined ) return;

			const selected_option_price =
				jQuery( 'option:selected', this ).attr( 'data-price' ) || 0;
			const selected_option_label = jQuery(
				'option:selected',
				this
			).attr( 'data-label' );
			const selected_option_title = jQuery(
				'option:selected',
				this
			).attr( 'data-title' );
			let selected_option_apply =
				jQuery( 'option:selected', this ).attr( 'data-onetime' ) !==
				'on'
					? 'variable'
					: 'onetime';
			const selected_option_taxable = jQuery(
				'option:selected',
				this
			).attr( 'data-taxable' );
			const selected_option_without_tax = jQuery(
				'option:selected',
				this
			).attr( 'data-without_tax' );
			const selected_option_optionid = jQuery(
				'option:selected',
				this
			).attr( 'data-optionid' );
			const selected_option_data_name = jQuery(
				'option:selected',
				this
			).attr( 'data-data_name' );

			const checked_option_price =
				jQuery( this ).attr( 'data-price' ) || 0;
			const checked_option_label = jQuery( this ).attr( 'data-label' );
			let checked_option_title = jQuery( this ).attr( 'data-title' );
			let checked_option_apply =
				jQuery( this ).attr( 'data-onetime' ) !== 'on'
					? 'variable'
					: 'onetime';
			const checked_option_taxable =
				jQuery( this ).attr( 'data-taxable' );
			const checked_option_without_tax =
				jQuery( this ).attr( 'data-without_tax' );
			const checked_option_optionid =
				jQuery( this ).attr( 'data-optionid' );
			const checked_option_data_name =
				jQuery( this ).attr( 'data-data_name' );

			// apply now being added from data-attribute for new prices
			if ( jQuery( this ).attr( 'data-apply' ) !== undefined ) {
				checked_option_apply = jQuery( this ).attr( 'data-apply' );
				selected_option_apply = jQuery( this ).attr( 'data-apply' );
			}

			const does_option_has_price = true;

			if (
				( checked_option_price == undefined ||
					checked_option_price == '' ) &&
				( selected_option_price == undefined ||
					selected_option_price == '' )
			) {
				return;
			}

			const option_price = {};
			if ( jQuery( this ).prop( 'checked' ) ) {
				if ( checked_option_title !== undefined ) {
					option_price.label =
						checked_option_title + ' ' + checked_option_label;
				} else {
					option_price.label = checked_option_label;
				}
				option_price.price = checked_option_price;
				option_price.apply = checked_option_apply;

				option_price.product_title = ppom_input_vars.product_title;
				option_price.taxable = checked_option_taxable;
				option_price.without_tax = checked_option_without_tax;
				option_price.option_id = checked_option_optionid;
				option_price.data_name = checked_option_data_name;

				// More data attributes
				if ( checked_option_apply === 'measure' ) {
					option_price.qty = jQuery( this ).attr( 'data-qty' );
					option_price.use_units =
						jQuery( this ).attr( 'data-use_units' );
					option_price.price_multiplier = jQuery( this ).attr(
						'data-price-multiplier'
					);
				}

				// console.log(option_price);

				options_price_added.push( option_price );
			} else if (
				selected_option_price !== undefined &&
				is_option_calculatable( this ) &&
				jQuery( this ).data( 'type' ) !== 'text'
			) {
				if ( selected_option_title !== undefined ) {
					option_price.label =
						selected_option_title + ' ' + selected_option_label;
				} else {
					option_price.label = selected_option_label;
				}
				option_price.price = selected_option_price;
				option_price.apply = selected_option_apply;

				option_price.product_title = ppom_input_vars.product_title;
				option_price.taxable = selected_option_taxable;
				option_price.without_tax = selected_option_without_tax;
				option_price.option_id = selected_option_optionid;
				option_price.data_name = selected_option_data_name;

				options_price_added.push( option_price );
			} else if (
				jQuery( this ).data( 'type' ) == 'text' &&
				jQuery( this ).val() !== ''
			) {
				// console.log(checked_option_price);
				checked_option_title =
					checked_option_title +
					' ' +
					ppom_get_formatted_price( checked_option_price );

				option_price.label = checked_option_title;
				option_price.price = checked_option_price;
				option_price.apply = checked_option_apply;

				option_price.product_title = ppom_input_vars.product_title;
				option_price.taxable = checked_option_taxable;
				option_price.without_tax = '';

				options_price_added.push( option_price );
				// console.log(jQuery(this));
			}
		}
	);

	// Price matrix
	let ppom_pricematrix = jQuery( '.ppom_pricematrix.active' ).val();

	// if conditionally hidden
	if (
		jQuery( '.ppom_pricematrix.active' )
			.closest( '.ppom-field-wrapper' )
			.hasClass( 'ppom-c-hide' )
	) {
		ppom_pricematrix = undefined;
	}

	const ppom_pricematrix_discount = jQuery( '.ppom_pricematrix.active' ).attr(
		'data-discount'
	);
	const ppom_pricematrix_id = jQuery( '.ppom_pricematrix.active' ).data(
		'dataname'
	);
	const ppom_matrix_array = Array();
	const apply_as_discount = ppom_pricematrix_discount == 'on' ? true : false;

	if ( ppom_pricematrix !== undefined ) {
		jQuery.each( JSON.parse( ppom_pricematrix ), function ( range, meta ) {
			const option_price = {};

			const range_break = range.split( '-' );
			const range_from = parseInt( range_break[ 0 ] );
			const range_to = parseInt( range_break[ 1 ] );
			const product_qty = ppom_get_order_quantity();

			// console.log(range, meta);

			if ( product_qty >= range_from && product_qty <= range_to ) {
				option_price.label = meta.label;
				option_price.price = meta.price;
				option_price.percent = meta.percent;
				option_price.range = range;
				option_price.apply = apply_as_discount
					? 'matrix_discount'
					: 'matrix';
				option_price.data_name = ppom_pricematrix_id;
				option_price.matrix_fixed =
					meta.matrix_fixed == 'on' ? true : false;
				options_price_added.push( option_price );
			}
		} );
	}

	// Variation quantities
	var ppom_quantities_qty = 0;
	jQuery( '.ppom-input-quantities' ).each( function () {
		// console.log(jQuery(this).closest('.ppom-field-wrapper').hasClass('ppom-c-hide'));
		if (
			jQuery( this )
				.closest( '.ppom-field-wrapper' )
				.hasClass( 'ppom-c-hide' )
		) {
			// Resetting quantity to one
			// wc_product_qty.val(1);
			return;
		}

		jQuery( this )
			.find( '.ppom-quantity' )
			.each( function () {
				const option_price = {};

				option_price.price = jQuery( this ).attr( 'data-price' );
				option_price.label = jQuery( this ).attr( 'data-label' );
				option_price.quantity =
					jQuery( this ).val() === '' ? 0 : jQuery( this ).val();
				option_price.include =
					jQuery( this ).attr( 'data-includeprice' );
				option_price.apply = 'quantities';
				option_price.usebase_price =
					jQuery( this ).attr( 'data-usebase_price' );
				option_price.unlink_qty =
					jQuery( this ).attr( 'data-unlink_qty' );

				options_price_added.push( option_price );

				if ( option_price.unlink_qty !== 'on' ) {
					ppom_quantities_qty += parseInt( option_price.quantity );
					ppom_set_order_quantity( ppom_quantities_qty );
				}
			} );
	} );

	// WC Variation Quantity Addon
	let ppom_wcvq = 0;
	jQuery( '.ppom-wcvq-input' ).each( function () {
		const option_price = {};

		option_price.price = jQuery( this ).attr( 'data-price' );

		option_price.label = jQuery( this ).attr( 'data-label' );
		option_price.quantity =
			jQuery( this ).val() === '' ? 0 : jQuery( this ).val();
		option_price.include = jQuery( this ).attr( 'data-includeprice' );
		option_price.apply = 'wc_variation_quantities';
		ppom_wcvq += parseInt( option_price.quantity );

		options_price_added.push( option_price );
		ppom_set_order_quantity( ppom_wcvq );
	} );

	// Bulkquantity
	jQuery( '.bulkquantity' ).each( function () {
		// console.log(jQuery(this).closest('.ppom-field-wrapper').hasClass('ppom-c-hide'));
		if (
			jQuery( this )
				.closest( '.ppom-field-wrapper' )
				.hasClass( 'ppom-c-hide' )
		) {
			// Resetting quantity to one
			// wc_product_qty.val(1);
			return;
		}

		const option_price = {};

		// var ppom_bq_container = jQuery('#ppom-input-bulkquantity');

		option_price.price = jQuery( this )
			.find( '.ppom-bulkquantity-options option:selected' )
			.attr( 'data-price' );
		option_price.base = jQuery( this )
			.find( '.ppom-bulkquantity-options option:selected' )
			.attr( 'data-baseprice' );
		option_price.label = jQuery( this )
			.find( '.ppom-bulkquantity-options option:selected' )
			.attr( 'data-label' );
		option_price.quantity = jQuery( this )
			.find( '.ppom-bulkquantity-qty' )
			.val();
		option_price.include = jQuery( this )
			.find( '.ppom-bulkquantity-options' )
			.attr( 'data-includeprice' );
		option_price.apply = 'bulkquantity';
		//console.log(jQuery(this));
		options_price_added.push( option_price );
		ppom_set_order_quantity( option_price.quantity );
	} );

	// Fixedprice addon
	if ( jQuery( '.ppom-input-fixedprice' ).length > 0 ) {
		const option_price = {};

		const ppom_fp_container = jQuery(
			'.ppom-input-fixedprice.ppom-unlocked'
		);
		const view_type = ppom_fp_container.attr( 'data-input' );
		const selector =
			view_type == 'select'
				? 'select option:selected'
				: 'input[type="radio"]:checked';

		option_price.price =
			ppom_fp_container.find( selector ).attr( 'data-price' ) || 0;
		option_price.unitprice =
			ppom_fp_container.find( selector ).attr( 'data-unitprice' ) || 0;
		option_price.label =
			ppom_fp_container.find( selector ).attr( 'data-label' ) || '';
		option_price.quantity =
			ppom_fp_container.find( selector ).attr( 'data-qty' ) || 0;
		// option_price.include    = jQuery(this).attr('data-includeprice');
		option_price.apply = 'fixedprice';
		options_price_added.push( option_price );
	}

	// Event Calendar Add-on
	var ppom_quantities_qty = 0;
	jQuery( '.ppom-input-eventcalendar' ).each( function () {
		// Checking if quantities is hidden
		if ( jQuery( this ).hasClass( 'ppom-locked' ) ) {
			// Resetting quantity to one
			wc_product_qty.val( 1 );
			return;
		}

		jQuery( this )
			.find( '.ppom-eventcalendar' )
			.each( function () {
				const option_price = {};

				option_price.price = jQuery( this ).attr( 'data-price' );
				option_price.label = jQuery( this ).attr( 'data-label' );
				option_price.quantity =
					jQuery( this ).val() === '' ? 0 : jQuery( this ).val();
				option_price.include = '';
				option_price.apply = 'eventcalendar';
				ppom_quantities_qty += parseInt( option_price.quantity );

				// 		console.log(ppom_quantities_qty);
				options_price_added.push( option_price );
				ppom_set_order_quantity( ppom_quantities_qty );
			} );
	} );

	// console.log(options_price_added);
	return options_price_added;
}

function ppom_get_formatted_price( price ) {
	const no_of_decimal = ppom_input_vars.wc_no_decimal || 2;
	const thousand_sep = ppom_input_vars.wc_thousand_sep || ',';
	var decimal_separator = ppom_input_vars.wc_decimal_sep || '.';

	const formatted_price = accounting.formatNumber(
		price,
		no_of_decimal,
		thousand_sep,
		decimal_separator
	);

	var decimal_separator = ppom_input_vars.wc_decimal_sep;

	return formatted_price;
}

// Return formatted price with decimal and seperator
// function ppom_get_formatted_price(price) {

//     var decimal_separator = ppom_input_vars.wc_decimal_sep;
//     var no_of_decimal = ppom_input_vars.wc_no_decimal;

//     var formatted_price = price > 0 ? Math.abs(parseFloat(price)) : parseFloat(price);
//     formatted_price = formatted_price.toFixed(no_of_decimal);
//     formatted_price = formatted_price.toString().replace('.', decimal_separator);
//     formatted_price = ppom_add_thousand_seperator(formatted_price);
//     // formatted_price = numberWithCommas(formatted_price);

//     return formatted_price;
// }

// function ppom_add_thousand_seperator(n) {

//     var rx = /(\d+)(\d{3})/;
//     return String(n).replace(/^\d+/, function(w) {
//         if (ppom_input_vars.wc_thousand_sep) {
//             while (rx.test(w)) {
//                 w = w.replace(rx, '$1' + ppom_input_vars.wc_thousand_sep + '$2');
//             }
//         }
//         return w;
//     });
// }

// sometime options should not be calculated like in case of bulkquantity
function is_option_calculatable( selector ) {
	let option_calculatable = true;
	if ( jQuery( selector ).attr( 'data-type' ) === 'bulkquantity' ) {
		option_calculatable = false;
	}

	return option_calculatable;
}

// Return quantity
function ppom_get_order_quantity() {
	let quantity =
		ppom_input_vars.is_shortcode === 'yes' ? 1 : wc_product_qty.val();
	quantity = quantity || 1;
	return parseInt( quantity );
}

// Set quantity
function ppom_set_order_quantity( qty ) {
	wc_product_qty.val( qty );
	// for some theme issue
	jQuery( 'form.cart' ).find( 'input[name="quantity"]' ).val( qty );
}

// The summary table can also remove selections, so mirror that back into the
// original form control and let the regular change listeners recompute prices.
function ppom_delete_option_from_price_table( field_name, option_id ) {
	const field_type = ppom_get_field_type_by_id( field_name );

	// get product id
	const product_id = ppom_input_vars.product_id;
	const opt_id = product_id + '-' + field_name + '-' + option_id;

	switch ( field_type ) {
		case 'palettes':
		case 'radio':
		case 'checkbox':
			jQuery( '#' + opt_id ).prop( 'checked', false );
			ppom_update_option_prices();
			break;

		case 'select':
			jQuery( '#' + field_name ).val( '' );
			ppom_update_option_prices();
			break;

		case 'image':
			jQuery( '#' + option_id ).prop( 'checked', false );
			ppom_update_option_prices();
			break;
	}

	jQuery( `.ppom-input[data-data_name="${ field_name }"]` ).trigger(
		'change'
	);
}

// Variation-quantity rows can derive their unit price from the selected base
// WooCommerce variation, so this refresh runs whenever variation prices change.
function ppom_update_variation_quatity( price ) {
	jQuery( 'input.ppom-quantity' ).each( function ( i, q ) {
		if ( jQuery( q ).attr( 'data-usebase_price' ) == 'yes' ) {
			jQuery( q ).attr( 'data-price', price );
		}
	} );
}

// Return ammount after apply percent
function ppom_get_amount_after_percentage( base_amount, percent ) {
	base_amount = parseFloat( base_amount );
	let percent_amount = 0;
	percent = percent.substr( 0, percent.length - 1 );
	percent_amount = ( parseFloat( percent ) / 100 ) * base_amount;

	return percent_amount;
}

// Buil price object of given ppom input
function ppom_build_input_price_meta( field_dataname ) {
	const field_meta = ppom_get_field_meta_by_id( field_dataname );
	const field_type = field_meta.type || '';

	if ( ! field_meta ) {
		return '';
	}

	let ppom_field_price = {};
	const apply = field_meta.onetime === 'on' ? 'onetime' : 'variable';

	switch ( field_type ) {
		case 'text':
			ppom_field_price = {
				product_title: ppom_input_vars.product_title,
				taxable: field_meta.taxable || '',
				without_tax: field_meta.without_tax || '',
				data_name: field_dataname,
				price: field_meta.price || 0,
				apply,
			};
			break;

		default:
		// code
	}

	//console.log(ppom_field_price);

	/*option_price.product_title  = ppom_input_vars.product_title;
    option_price.taxable        = checked_option_taxable;
    option_price.without_tax    = checked_option_without_tax;
    option_price.data_name      = checked_option_data_name;
    option_price.price          = checked_option_price;
    option_price.apply          = checked_option_apply;*/
}

// Quantity-type fields can disable add to cart until their aggregate rules pass.
function ppom_quantities_min_max( dataname ) {
	const ppomInputWrapper = jQuery( '.ppom-input-' + dataname );
	let list = jQuery( '.ppom-quantity', ppomInputWrapper );

	//convert to array
	list = Array.from( list );

	let totalQty = 0;
	let min_qty = '';
	let max_qty = '';
	let _input = '';
	let _return = false;

	list.map( ( elem, index ) => {
		_input = jQuery( elem );
		const QTY = parseInt( _input.val() ) || 0;
		min_qty = parseInt( _input.attr( 'data-min' ) ) || 0;
		max_qty = parseInt( _input.attr( 'data-max' ) ) || 0;

		if ( isNaN( QTY ) ) {
			return false;
		}

		totalQty += QTY;
		// console.log(totalQty);
	} );

	if ( min_qty > totalQty ) {
		jQuery( 'form.cart' )
			.find( 'button[name="add-to-cart"]' )
			.prop( 'disabled', true );
	} else if ( max_qty < totalQty && max_qty != 0 ) {
		alert( 'Maximum ' + max_qty + ' allowed' );
		ppom_update_option_prices();
		_return = true;
	} else {
		jQuery( 'form.cart' )
			.find( 'button[name="add-to-cart"]' )
			.prop( 'disabled', false );
	}

	return _return;
}
