window.hbfwc = window.hbfwc || {};

hbfwc.bookingAddon = function($) {
	const post = {
		init: function() {
			const d = this;
			d.$wrapper = $('#hbfwc_booking_addon_details');
			this.initSetup();
			this.bindEvents();
		},

		initSetup: function() {
			const d = this;
			$('.addon-data-wrapper').removeClass('hidden').appendTo( d.$wrapper.find('h2.hndle') );
			const cv = $('.wc_input_price').val();
			if ( cv.indexOf(hbfwc_admin_booking_addon.thousand_sep) ) {
				$('.wc_input_price').val( cv.replaceAll(hbfwc_admin_booking_addon.thousand_sep, hbfwc_admin_booking_addon.decimal_sep));
			}
		},

		bindEvents: function() {
			const d = this;
			d.tipsy();
			d.fixToggle();
			d.elHideShow();
			d.stripeIt();
			d.skipDot();
		},

		tipsy: function() {
			const d = this;
			$( '.woocommerce-help-tip' ).tipTip( {
				attribute: 'data-tip',
				fadeIn: 50,
				fadeOut: 50,
				delay: 200,
				keepAlive: true,
			} );
		},

		elHideShow: function() {
			const [d, els, ops, hds] = [ this, $('[class*="show_if_"]'), $('.show_if_opt_required'), $('.hide_if_sub_type_room') ];
			
			if ( els.length ) {
				let choosen = $('#booking-addon-type').val();

				els.each(function() {
					$(this).addClass('hidden');
					if ( $(this).hasClass('show_if_'+choosen) ) {
						$(this).removeClass('hidden');
						if ( $(this).hasClass('show_if_opt_required') && ! $('#addon-field-required').is(':checked') ) {
							$(this).addClass('hidden');
						}

						if ( $(this).find('.hide_if_sub_type_room').is(':hidden') ) {
							$(this).addClass('hidden');
						}
					}
				});

				$(document.body).on('change', '#booking-addon-type', function(e) {
					choosen = $(this).val();

					els.each(function() {
						$(this).addClass('hidden');
						if ( $(this).hasClass('show_if_'+choosen) ) {
							$(this).removeClass('hidden');
							if ( $(this).hasClass('show_if_opt_required') && ! $('#addon-field-required').is(':checked') ) {
								$(this).addClass('hidden');
							}

							if ( $(this).find('.hide_if_sub_type_room').is(':hidden') ) {
								$(this).addClass('hidden');
							}
						}
					});
					d.stripeIt();
				}).on('change', '#addon-field-required', function(e) {
					const ckd = $(this).is(':checked');
					ops.each( function() {
						if ( $(this).hasClass('show_if_'+choosen) ) {
							if ( ckd ) {
								$(this).removeClass('hidden');
								if ( $(this).find('.hide_if_sub_type_room').is(':hidden') ) {
									$(this).addClass('hidden');
								}
							} else {
								$(this).addClass('hidden');
							}
						}
					});
					d.stripeIt();
				});
			}

			if ( hds.length ) {
				let subValue = $('[name="sub_type"]:checked').val();
				subValue === 'room' ? hds.hide() : hds.show();
				if ( subValue === 'room' ) {
					hds.closest('.hbfwc-field').addClass('hidden');
				}
				$('#booking-addon-type, #addon-field-required').trigger('change');

				$(document.body).on('change', '[name="sub_type"]', function(e) {
					subValue = $('[name="sub_type"]:checked').val();
					subValue === 'room' ? hds.hide() : hds.show();
					if ( subValue === 'room' ) {
						hds.closest('.hbfwc-field').addClass('hidden');
					}
					$('#booking-addon-type, #addon-field-required').trigger('change');
				});
			}
		},

		fixToggle: function() {
			const d = this;
			$(document).ready(() => {
				// Prevent inputs in meta box headings opening/closing contents.
				d.$wrapper.find( '.hndle' ).off( 'click.postboxes' );

				d.$wrapper.on( 'click', '.hndle', function ( event ) {
					// If the user clicks on some form input inside the h2 the box should not be toggled.
					if (
						$( event.target ).filter( 'input, option, label, select' )
							.length
					) {
						return;
					}

					if ( d.$wrapper.hasClass( 'closed' ) ) {
						d.$wrapper.removeClass( 'closed' );
					} else {
						d.$wrapper.addClass( 'closed' );
					}
				} );
			} );
		},

		stripeIt: function() {
			const d = this;
			d.$wrapper.find('p.hbfwc-field')
				.removeClass('even-field')
				.filter(':visible')
				.each(function(i) {
					if (i % 2 === 1) $(this).addClass('even-field');
				});
		},

		skipDot: function () {
			const invalidChars = [ hbfwc_admin_booking_addon.thousand_sep ];
			$( document.body ).on(
				'keydown',
				'.wc_input_price',
				function (e) {
					if (invalidChars.includes( e.key )) {
						e.preventDefault();
					}
				}
			);
		}
	};
	post.init();
	return post;
}(window.jQuery);