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

const { gp_conditional_pricing_form_settings_strings: strings } = window;

interface ExportPricingLevelsResp {
	success: boolean;
	data: {
		export_file_url: string;
	};
}

export async function exportPricingLevels(products: Product[]) {
	const resp: ExportPricingLevelsResp = await jQuery.post(strings.ajax_url, {
		action: 'gwcp_export_pricing_levels',
		_ajax_nonce: strings.nonce,
		form_id: strings.form_id,
		products,
	});

	if (!resp.success) {
		throw new Error('Error exporting pricing logic');
	}

	return resp.data;
}
