import type { Product } from '../typings/types';

const { gp_conditional_pricing_form_settings_strings: strings } = window;

interface PricingRulesResp {
	success: boolean;
	data: {
		products: Product[];
	};
}

export async function fetchPricingLogic() {
	const resp: PricingRulesResp = await jQuery.get(strings.ajax_url, {
		action: 'gwcp_get_pricing_levels',
		_ajax_nonce: strings.nonce,
		form_id: strings.form_id,
	});

	if (!resp.success) {
		throw new Error('Error fetching pricing rules');
	}

	return resp.data.products;
}
