import { PixelioCartAbandonRecoveryMoreAjaxRequest } from "./pixelio_cart_abandon_recovery_more_ajax_request";

export class PixelioWishlistButton {
	constructor() {
		const $btn = jQuery('#wishlist-btn');
		const $modal = jQuery('#wishlist-email-modal');
		const $submit = jQuery('#wishlist-email-submit');
		const $input = jQuery('#wishlist-email-input');
		const $error = jQuery('#wishlist-error');

		if (!$btn.length) return;

		this.ajaxRequest = new PixelioCartAbandonRecoveryMoreAjaxRequest();
		this.productId = $btn.data('product-id') || $btn.attr('product-id');

		if (!this.productId) return;

		this._checkStatus($btn);


		$btn.on('click', async (e) => {
			e.preventDefault();

			$error.hide().text('');

			const savedEmail = localStorage.getItem('pixelio_wishlist_email');

			if (savedEmail) {
				await this._send(savedEmail, $btn);
				return;
			}

			$modal.css('display', 'flex');
		});

		$submit.on('click', async (e) => {
			e.preventDefault();

			const email = String($input.val() || '').trim();

			if (!email) {
				$input.addClass('error');
				$error.text(pixelio_ajax.email_required).show();
				return;
			}

			$input.removeClass('error');
			$error.hide().text('');

			await this._send(email, $btn);
		});

		jQuery('#wishlist-modal-close').on('click', function () {
			$modal.hide();
			$error.hide().text('');
		});
	}

	async _checkStatus($btn) {
		const formData = new FormData();
		formData.append('pixelio_wishlist_product_id', this.productId);
		formData.append('pixelio_wishlist_email', localStorage.getItem('pixelio_wishlist_email'));

		await this.ajaxRequest.makeAjaxRequest(
			'wishlist_check_status',
			formData,
			(response) => {
				if (response?.data?.in_wishlist) {
					$btn.text(pixelio_ajax.in_wishlist);
					$btn.data('in-wishlist', true);
				} else {
					$btn.text(pixelio_ajax.add_to_wishlist);
					$btn.data('in-wishlist', false);
				}
			},
			function () {}
		);
	}

	async _send(email, $btn) {
		const formData = new FormData();
		const $error = jQuery('#wishlist-error');

		formData.append('pixelio_wishlist_product_id', this.productId);

		if (email) {
			formData.append('pixelio_wishlist_email', email);
		}
		formData.append(
			'utm_source',
			localStorage.getItem('pixelio_utm_source') || ''
		);

		formData.append(
			'utm_campaign',
			localStorage.getItem('pixelio_utm_campaign') || ''
		);

		formData.append(
			'utm_content',
			localStorage.getItem('pixelio_utm_content') || ''
		);
		const isInWishlist = $btn.data('in-wishlist') === true;
		formData.append('pixelio_wishlist_action_type', isInWishlist ? 'remove' : 'add');

		const $gdprCheckbox = jQuery('.pixelio-cart-abandon-recovery-more-wishlist-rgpd');

		const hasSavedEmail = !!localStorage.getItem('pixelio_wishlist_email');

		if (!isInWishlist && !hasSavedEmail && pixelio_ajax.isRgpdActive && $gdprCheckbox.length) {
			if (!$gdprCheckbox.is(':checked')) {
				$error.text(pixelio_ajax.privacy_required).show();
				return;
			}

			const gdprLabel = $gdprCheckbox
				.closest('label')
				.text()
				.trim()
				.replace(/\s+/g, ' ');

			formData.append('pixelio_wishlist_gdpr_consent', '1');
			formData.append('pixelio_wishlist_gdpr_label', gdprLabel);
		}

		await this.ajaxRequest.makeAjaxRequest(
			'add_to_wishlist',
			formData,
			(response) => {
				const action = response?.data?.action;

				if (email) {
					localStorage.setItem('pixelio_wishlist_email', email);
				}

				if (action === 'removed') {
					$btn.text(pixelio_ajax.add_to_wishlist);
					$btn.data('in-wishlist', false);
					$error.text(pixelio_ajax.removed).show();
					return;
				}

				$btn.text(pixelio_ajax.in_wishlist);
				$btn.data('in-wishlist', true);

				jQuery('#wishlist-email-modal').hide();

				$error
					.text(pixelio_ajax.recovery_email_sent)
					.show();
			},
			(response) => {
				const msg = response?.data?.message || pixelio_ajax.error;
				$error.text(msg).show();
			}
		);
	}
}
