export class PixelioCartAbandonRecoveryMoreAjaxRequest {
	constructor() {


	}

	set_uuid(response) {
		this.setMetaContentByName("pixelio-uuid", response.data);
	}
	get_uuid() {
		return new Promise((resolve, reject) => {
			const currentUuid = this.getMetaContentByName('pixelio-uuid');

			if (currentUuid) {
				resolve(currentUuid);
			} else {
				this.makeAjaxRequest(
					"pixelio_cart_abandon_recovery_more_get_uuid",
					new FormData(),
					(response) => {
						this.set_uuid(response);
						resolve(response);
					},
					(error) => reject(error),
					null,
					true
				);
			}
		});
	}
	async makeAjaxRequest(action, data, successCallback = null, failCallback = null, unknowCallback = null,isForUuid = false) {

		data.append('action', action);
		data.append('nonce', pixelio_ajax.pixelio_ajax_nonce);
		if(!isForUuid){
			await this.get_uuid.call(this);
		}
		data.append('uuid', this.getMetaContentByName('pixelio-uuid'));

		jQuery.ajax({
			url: pixelio_ajax.pixelio_ajax_url,
			type: 'POST',
			data: data,
			processData: false,
			contentType: false,
			dataType: 'json',
			success: function(response) {
				if (response.success && successCallback) {
					successCallback(response);
				}else{
					failCallback ? failCallback(response) : null;
				}
			},
			error: function(jqXHR) {
				if (jqXHR.status === 404) {
					unknowCallback ? unknowCallback() : null;
				} else {
					failCallback ? failCallback() : null;
				}
			}
		});
	}

	getMetaContentByName(name) {
		return jQuery(`meta[name="${name}"]`).attr('content') || null;
	}

	setMetaContentByName(name,value) {
		 jQuery(`meta[name="${name}"]`).attr('content',value) ;
	}
}
