import { apiEndpoints } from "@/lib/api/endpoints"; import { fetchWithAuth } from "@/lib/auth/client"; import type { MarketplaceHomeResponse, MarketplaceListing, MarketplaceRepairShop, MarketplaceRepairShopResponse, MarketplaceResponse, MarketplaceShopProfile, SuccessMessage, } from "@/types/api"; type MarketplaceFormValue = | string | number | boolean | File | null | undefined | Array; type MarketplaceFormPayload = Record; function appendFormValue(formData: FormData, key: string, value: MarketplaceFormValue) { if (value === undefined || value === null || value === "") { return; } if (Array.isArray(value)) { value.forEach((entry) => appendFormValue(formData, key, entry)); return; } if (value instanceof File) { formData.append(key, value); return; } formData.append(key, String(value)); } function toMarketplaceFormData(payload: MarketplaceFormPayload) { const formData = new FormData(); Object.entries(payload).forEach(([key, value]) => appendFormValue(formData, key, value)); return formData; } export async function getMarketplaceHome(params: Record = {}) { return fetchWithAuth(apiEndpoints.marketplace.home(params)); } export async function getMarketplaceShopByAdminId(adminId: string) { return fetchWithAuth(apiEndpoints.marketplace.shopByAdminId(adminId)); } export async function getAdminShopProfile() { return fetchWithAuth(apiEndpoints.marketplace.adminMyShopProfile); } export async function updateAdminShopProfile(payload: MarketplaceFormPayload) { return fetchWithAuth(apiEndpoints.marketplace.adminShopProfile, { method: "PATCH", body: toMarketplaceFormData(payload), }); } export async function createAdminRepairShop(payload: MarketplaceFormPayload) { return fetchWithAuth(apiEndpoints.marketplace.adminCreateRepairShop, { method: "POST", body: toMarketplaceFormData(payload), }); } export async function updateAdminRepairShop(repairShopId: string, payload: MarketplaceFormPayload) { return fetchWithAuth(apiEndpoints.marketplace.adminUpdateRepairShop(repairShopId), { method: "PATCH", body: toMarketplaceFormData(payload), }); } export async function deleteAdminRepairShop(repairShopId: string) { return fetchWithAuth(apiEndpoints.marketplace.adminDeleteRepairShop(repairShopId), { method: "DELETE", }); } export async function listAdminRepairShops( params: Record = {}, ) { return fetchWithAuth(apiEndpoints.marketplace.adminMyRepairShops(params)); } export async function createAdminInstrument(payload: MarketplaceFormPayload) { return fetchWithAuth(apiEndpoints.marketplace.adminCreateInstrument, { method: "POST", body: toMarketplaceFormData(payload), }); } export async function updateAdminInstrument(instrumentId: string, payload: MarketplaceFormPayload) { return fetchWithAuth(apiEndpoints.marketplace.adminUpdateInstrument(instrumentId), { method: "PATCH", body: toMarketplaceFormData(payload), }); } export async function deleteAdminInstrument(instrumentId: string) { return fetchWithAuth(apiEndpoints.marketplace.adminDeleteInstrument(instrumentId), { method: "DELETE", }); } export async function listAdminInstruments( params: Record = {}, ) { return fetchWithAuth(apiEndpoints.marketplace.adminMyInstruments(params)); } export async function createAdminListing(payload: MarketplaceFormPayload) { return fetchWithAuth(apiEndpoints.marketplace.adminCreateListing, { method: "POST", body: toMarketplaceFormData(payload), }); } export async function updateAdminListing(listingId: string, payload: MarketplaceFormPayload) { return fetchWithAuth(apiEndpoints.marketplace.adminUpdateListing(listingId), { method: "PATCH", body: toMarketplaceFormData(payload), }); } export async function deleteAdminListing(listingId: string) { return fetchWithAuth(apiEndpoints.marketplace.adminDeleteListing(listingId), { method: "DELETE", }); } export async function listAdminListings( params: Record = {}, ) { return fetchWithAuth(apiEndpoints.marketplace.adminMyListings(params)); } export async function listModerationListings(params: Record = {}) { return fetchWithAuth(apiEndpoints.marketplace.moderationListings(params)); } export async function updateModerationListingStatus( listingId: string, isActive: boolean, reason?: string, ) { return fetchWithAuth(apiEndpoints.marketplace.moderationUpdateListingStatus(listingId), { method: "PATCH", body: JSON.stringify({ isActive, reason }), }); } export async function deleteModerationListing(listingId: string) { return fetchWithAuth(apiEndpoints.marketplace.moderationDeleteListing(listingId), { method: "DELETE", }); } export async function listModerationRepairShops( params: Record = {}, ) { return fetchWithAuth(apiEndpoints.marketplace.moderationRepairShops(params)); } export async function updateModerationRepairShopStatus( repairShopId: string, isActive: boolean, reason?: string, ) { return fetchWithAuth( apiEndpoints.marketplace.moderationUpdateRepairShopStatus(repairShopId), { method: "PATCH", body: JSON.stringify({ isActive, reason }), }, ); } export async function deleteModerationRepairShop(repairShopId: string) { return fetchWithAuth(apiEndpoints.marketplace.moderationDeleteRepairShop(repairShopId), { method: "DELETE", }); } export async function createMarketplaceListingForSuperAdmin( adminId: string, payload: MarketplaceFormPayload, ) { return fetchWithAuth(apiEndpoints.marketplace.superAdminCreateListing(adminId), { method: "POST", body: toMarketplaceFormData(payload), }); } export async function createMarketplaceInstrumentForSuperAdmin( adminId: string, payload: MarketplaceFormPayload, ) { return fetchWithAuth(apiEndpoints.marketplace.superAdminCreateInstrument(adminId), { method: "POST", body: toMarketplaceFormData(payload), }); } export async function createMarketplaceRepairShopForSuperAdmin( adminId: string, payload: MarketplaceFormPayload, ) { return fetchWithAuth( apiEndpoints.marketplace.superAdminCreateRepairShop(adminId), { method: "POST", body: toMarketplaceFormData(payload), }, ); } export async function updateMarketplaceShopProfileForSuperAdmin( adminId: string, payload: MarketplaceFormPayload, ) { return fetchWithAuth( apiEndpoints.marketplace.superAdminUpdateShopProfile(adminId), { method: "PATCH", body: toMarketplaceFormData(payload), }, ); }