import { apiClient } from "./client"; import type { AjaxResponse } from "@/types/api.types"; import type { Region, ShippingRate, BulkApplyResult, } from "@/types/shipping.types"; const nonce = (): string => (window.coolviad_shipping_admin_data?.nonce as string) || ""; // ---- Regions ---------------------------------------------------------------- export const getRegions = async (): Promise => { const response = await apiClient.post>("", { action: "coolviad_get_regions", nonce: nonce(), }); return response.data.data || []; }; export const saveRegion = async (region: Partial): Promise => { const response = await apiClient.post>("", { action: "coolviad_save_region", nonce: nonce(), region: JSON.stringify(region), }); if (!response.data.success) { throw new Error(response.data.message || "Failed to save region"); } return response.data.data!; }; export const deleteRegion = async (id: number): Promise => { const response = await apiClient.post("", { action: "coolviad_delete_region", nonce: nonce(), id, }); if (!response.data.success) { throw new Error(response.data.message || "Failed to delete region"); } }; // ---- Bulk apply ------------------------------------------------------------- export const bulkApplyRegionRate = async ( regionCode: string, rate: Partial, ): Promise => { const response = await apiClient.post>("", { action: "coolviad_bulk_apply_region_rate", nonce: nonce(), region_code: regionCode, rate: JSON.stringify(rate), }); if (!response.data.success) { throw new Error(response.data.message || "Bulk apply failed"); } return response.data.data!; };