(function($){

	"use strict";
	
	$(document).ready(function () {
		byt_woo.init();
	});
	
	var byt_woo = {
	
		init: function () {

			if (window.useWoocommerceForCheckout && window.wc && window.wc.blocksCheckout) {
				const { registerCheckoutFilters } = window.wc.blocksCheckout;

				const itemNameCallback = (defaultValue, extensions, args) => {
					if (args?.cartItem?.item_data?.length > 0) {
						const item_title_json = args?.cartItem?.item_data?.find(item => item.key === '_bookyourtravel_item_title')?.value;
						if (item_title_json) {
							const item_title_array = JSON.parse(item_title_json);
							const item_title = item_title_array.join('<br />');
							return item_title;
						}
					}
			
					return `${defaultValue}!`;
				};

				const itemClassCallback = ( defaultValue, extensions, args ) => {
					if (args?.cartItem?.item_data?.length > 0) {
						const item_title_json = args?.cartItem?.item_data?.find(item => item.key === '_bookyourtravel_item_title')?.value;
						if (item_title_json) {
							return 'bookyourtravel-item';							
						}
					}

					return defaultValue;
				};				
		
				registerCheckoutFilters( 'bookyourtravel', {
					itemName: itemNameCallback,
					cartItemClass: itemClassCallback,					
				});			
			}
        }
    }

})(jQuery);