import { getApiEndpoint } from '../config/api.uts'
import { request, HttpError, resolveApiUrl } from './http.uts'

// Public portal data is intentionally separate from authenticated talent services.
export const getAppHome = (onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => {
	return request<any>({ path: getApiEndpoint('appHome'), method: 'GET', skipAuth: true }, onSuccess, onError)
}

export const getNews = (page: number, size: number, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => {
	return request<any>({ path: getApiEndpoint('appNews'), method: 'GET', skipAuth: true, data: { page: page, size: size } }, onSuccess, onError)
}

export const getNewsDetail = (newsId: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => {
	return request<any>({ path: getApiEndpoint('appNewsDetail') + encodeURIComponent(newsId), method: 'GET', skipAuth: true }, onSuccess, onError)
}

export const getPolicies = (page: number, size: number, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => {
	return request<any>({ path: getApiEndpoint('appPolicies'), method: 'GET', skipAuth: true, data: { page: page, size: size } }, onSuccess, onError)
}

export const getPolicyDetail = (policyId: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => {
	return request<any>({ path: getApiEndpoint('appPolicyDetail') + encodeURIComponent(policyId), method: 'GET', skipAuth: true }, onSuccess, onError)
}

export const getMerchants = (page: number, size: number, keyword: string, talentId: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => {
	const data: any = { page: page, size: size, keyword: keyword }
	if (talentId != '') data.talentId = Number(talentId)
	return request<any>({ path: getApiEndpoint('appMerchants'), method: 'GET', skipAuth: true, data: data }, onSuccess, onError)
}

export const getFoodTopic = (onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => {
	return request<any>({ path: getApiEndpoint('appFoodTopic'), method: 'GET', skipAuth: true }, onSuccess, onError)
}

export const getFoodTopicMerchants = (page: number, size: number, talentId: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => {
	const data: any = { page: page, size: size }
	if (talentId != '') data.talentId = Number(talentId)
	return request<any>({ path: getApiEndpoint('appFoodTopicMerchants'), method: 'GET', skipAuth: true, data: data }, onSuccess, onError)
}

export const getTravelTopic = (onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => request<any>({ path: getApiEndpoint('appTravelTopic'), method: 'GET', skipAuth: true }, onSuccess, onError)
export const getTravelTopicMerchants = (page: number, size: number, talentId: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => {
	const data: any = { page: page, size: size }
	if (talentId != '') data.talentId = Number(talentId)
	return request<any>({ path: getApiEndpoint('appTravelTopicMerchants'), method: 'GET', skipAuth: true, data: data }, onSuccess, onError)
}
export const getStayTopic = (onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => request<any>({ path: getApiEndpoint('appStayTopic'), method: 'GET', skipAuth: true }, onSuccess, onError)
export const getStayTopicMerchants = (type: string, page: number, size: number, talentId: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => {
	const data: any = { type: type, page: page, size: size }
	if (talentId != '') data.talentId = Number(talentId)
	return request<any>({ path: getApiEndpoint('appStayTopicMerchants'), method: 'GET', data: data }, onSuccess, onError)
}
export const getEntertainmentTopic = (onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => request<any>({ path: getApiEndpoint('appEntertainmentTopic'), method: 'GET', skipAuth: true }, onSuccess, onError)
export const getEntertainmentTopicMerchants = (page: number, size: number, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => request<any>({ path: getApiEndpoint('appEntertainmentTopicMerchants'), method: 'GET', skipAuth: true, data: { page: page, size: size } }, onSuccess, onError)
export const getTransportTopic = (onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => request<any>({ path: getApiEndpoint('appTransportTopic'), method: 'GET', skipAuth: true }, onSuccess, onError)
export const getTransportTopicMerchants = (type: string, talentId: string, page: number, size: number, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => {
	const data: any = { type: type, page: page, size: size }
	if (talentId != '') data.talentId = Number(talentId)
	return request<any>({ path: getApiEndpoint('appTransportTopicMerchants'), method: 'GET', data: data }, onSuccess, onError)
}
export const getShoppingTopic = (onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => request<any>({ path: getApiEndpoint('appShoppingTopic'), method: 'GET', skipAuth: true }, onSuccess, onError)
export const getShoppingTopicMerchants = (categoryId: string, talentId: string, page: number, size: number, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => {
	const data: any = { categoryId: categoryId, page: page, size: size }
	if (talentId != '') data.talentId = Number(talentId)
	return request<any>({ path: getApiEndpoint('appShoppingTopicMerchants'), method: 'GET', skipAuth: true, data: data }, onSuccess, onError)
}
export const getPremiumTopic = (onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => request<any>({ path: getApiEndpoint('appPremiumTopic'), method: 'GET', skipAuth: true }, onSuccess, onError)
export const getPremiumTopicServices = (type: string, page: number, size: number, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => request<any>({ path: getApiEndpoint('appPremiumTopicServices'), method: 'GET', skipAuth: true, data: { type: type, page: page, size: size } }, onSuccess, onError)
export const getSpouseEmploymentBenefit = (serviceId: string, talentId: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => {
	const data: any = {}
	if (talentId != '') data.talentId = Number(talentId)
	return request<any>({ path: getApiEndpoint('appSpouseEmploymentBenefit') + encodeURIComponent(serviceId), method: 'GET', skipAuth: true, data: data }, onSuccess, onError)
}
export const submitSpouseEmploymentApplication = (serviceId: string, payload: any, idempotencyKey: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => request<any>({ path: getApiEndpoint('appSpouseEmploymentBenefit') + encodeURIComponent(serviceId) + '/applications', method: 'POST', skipAuth: true, data: payload, idempotencyKey: idempotencyKey }, onSuccess, onError)
export const getAdminApprovalBenefit = (serviceId: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => request<any>({ path: getApiEndpoint('appAdminApprovalBenefit') + encodeURIComponent(serviceId), method: 'GET', skipAuth: true }, onSuccess, onError)
export const getFinanceMerchantDetail = (merchantId: string, talentId: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => {
	const data: any = {}
	if (talentId != '') data.talentId = Number(talentId)
	return request<any>({ path: getApiEndpoint('appFinanceMerchantDetail') + encodeURIComponent(merchantId), method: 'GET', skipAuth: true, data: data }, onSuccess, onError)
}
export const getChildrenEntrustmentBenefit = (serviceId: string, talentId: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => {
	const data: any = {}
	if (talentId != '') data.talentId = Number(talentId)
	return request<any>({ path: getApiEndpoint('appChildrenEntrustmentBenefit') + encodeURIComponent(serviceId), method: 'GET', skipAuth: true, data: data }, onSuccess, onError)
}
export const getChildrenEntrustmentSchools = (serviceId: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => request<any>({ path: getApiEndpoint('appChildrenEntrustmentBenefit') + encodeURIComponent(serviceId) + '/schools', method: 'GET', skipAuth: true }, onSuccess, onError)
export const getChildrenEntrustmentSchool = (serviceId: string, schoolId: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => request<any>({ path: getApiEndpoint('appChildrenEntrustmentBenefit') + encodeURIComponent(serviceId) + '/school-detail', method: 'GET', skipAuth: true, data: { schoolId: schoolId } }, onSuccess, onError)
export const getChildren = (talentId: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => request<any>({ path: getApiEndpoint('appChildren'), method: 'GET', skipAuth: true, data: { talentId: talentId } }, onSuccess, onError)
export const getChild = (childId: string, talentId: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => request<any>({ path: getApiEndpoint('appChildren') + '/' + encodeURIComponent(childId), method: 'GET', skipAuth: true, data: { talentId: talentId } }, onSuccess, onError)
export const createChild = (payload: any, idempotencyKey: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => request<any>({ path: getApiEndpoint('appChildren'), method: 'POST', skipAuth: true, data: payload, idempotencyKey: idempotencyKey }, onSuccess, onError)
export const resubmitChild = (childId: string, payload: any, idempotencyKey: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => request<any>({ path: getApiEndpoint('appChildren') + '/' + encodeURIComponent(childId), method: 'PUT', skipAuth: true, data: payload, idempotencyKey: idempotencyKey }, onSuccess, onError)
export const getMedicalPackages = (merchantId: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => request<any>({ path: getApiEndpoint('appMedicalMerchantPackages') + encodeURIComponent(merchantId) + '/packages', method: 'GET', skipAuth: true }, onSuccess, onError)
export const getMedicalPackage = (packageId: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => request<any>({ path: getApiEndpoint('appMedicalPackages') + encodeURIComponent(packageId), method: 'GET', skipAuth: true }, onSuccess, onError)
export const getMedicalPackageItems = (packageId: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => request<any>({ path: getApiEndpoint('appMedicalPackages') + encodeURIComponent(packageId) + '/items', method: 'GET', skipAuth: true }, onSuccess, onError)
export const submitServiceAppointment = (payload: any, idempotencyKey: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => request<any>({ path: getApiEndpoint('appServiceAppointmentApplications'), method: 'POST', skipAuth: true, data: payload, idempotencyKey: idempotencyKey }, onSuccess, onError)
export const getFuelMerchantDetail = (merchantId: string, talentCardId: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => {
	const data: any = {}
	if (talentCardId != '') data.talentCardId = Number(talentCardId)
	return request<any>({ path: getApiEndpoint('appFuelMerchantDetail') + encodeURIComponent(merchantId), method: 'GET', skipAuth: true, data: data }, onSuccess, onError)
}
export const submitFuelMobileRegistration = (merchantId: string, talentId: string, benefitId: string, mobile: string, idempotencyKey: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => request<any>({ path: getApiEndpoint('appFuelMerchantDetail') + encodeURIComponent(merchantId) + '/registrations', method: 'POST', skipAuth: true, data: { talentId: Number(talentId), benefitId: Number(benefitId), mobile: mobile }, idempotencyKey: idempotencyKey }, onSuccess, onError)
export const getParkingMerchantDetail = (merchantId: string, talentCardId: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => {
	const data: any = {}
	if (talentCardId != '') data.talentCardId = Number(talentCardId)
	return request<any>({ path: getApiEndpoint('appParkingMerchantDetail') + encodeURIComponent(merchantId), method: 'GET', skipAuth: true, data: data }, onSuccess, onError)
}
export const getCarWashMerchantDetail = (merchantId: string, talentCardId: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => {
	const data: any = {} 
	if (talentCardId != '') data.talentCardId = Number(talentCardId)
	return request<any>({ path: getApiEndpoint('appCarWashMerchantDetail') + encodeURIComponent(merchantId), method: 'GET', skipAuth: true, data: data }, onSuccess, onError)
}
export const submitCarWashCredential = (merchantId: string, talentId: string, cardId: string, phone: string, idempotencyKey: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => {
	const data: any = { talentId: Number(talentId), cardId: Number(cardId), proofFiles: [], description: '手机号：' + phone }
	return request<any>({ path: getApiEndpoint('appCarWashMerchantDetail') + encodeURIComponent(merchantId) + '/credentials', method: 'POST', skipAuth: true, data: data, idempotencyKey: idempotencyKey }, onSuccess, onError)
}
export const getShoppingMerchantDetail = (merchantId: string, talentId: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => {
	const data: any = {}
	if (talentId != '') data.talentId = Number(talentId)
	return request<any>({ path: getApiEndpoint('appShoppingMerchantDetail') + encodeURIComponent(merchantId), method: 'GET', skipAuth: true, data: data }, onSuccess, onError)
}
export const searchParkingLots = (merchantId: string, keyword: string, page: number, size: number, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => request<any>({ path: getApiEndpoint('appParkingMerchantDetail') + encodeURIComponent(merchantId) + '/parking-lots', method: 'GET', skipAuth: true, data: { keyword: keyword, page: page, size: size } }, onSuccess, onError)
export const submitParkingPlateRegistration = (merchantId: string, talentId: string, benefitId: string, parkingLotId: string, plateNumber: string, idempotencyKey: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => {
	const data: any = { talentId: Number(talentId), benefitId: Number(benefitId), plateNumber: plateNumber }
	if (parkingLotId != '') data.parkingLotId = Number(parkingLotId)
	return request<any>({ path: getApiEndpoint('appParkingMerchantDetail') + encodeURIComponent(merchantId) + '/registrations', method: 'POST', skipAuth: true, data: data, idempotencyKey: idempotencyKey }, onSuccess, onError)
}
export const getCinemaMerchant = (merchantId: string, talentId: string, cardId: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => {
	const data: any = {}
	if (talentId != '') data.talentId = Number(talentId)
	if (cardId != '') data.cardId = Number(cardId)
	return request<any>({ path: getApiEndpoint('appCinemaMerchants') + encodeURIComponent(merchantId), method: 'GET', skipAuth: true, data: data }, onSuccess, onError)
}
export const getCinemaCoupons = (merchantId: string, talentId: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => request<any>({ path: getApiEndpoint('appCinemaMerchants') + encodeURIComponent(merchantId) + '/coupons', method: 'GET', skipAuth: true, data: { talentId: talentId } }, onSuccess, onError)
export const claimCinemaCoupon = (merchantId: string, couponId: string, talentId: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => request<any>({ path: getApiEndpoint('appCinemaMerchants') + encodeURIComponent(merchantId) + '/coupons/' + encodeURIComponent(couponId) + '/claims', method: 'POST', skipAuth: true, data: { talentId: Number(talentId) }, idempotencyKey: 'cinema-claim-' + merchantId + '-' + couponId + '-' + talentId }, onSuccess, onError)

export const issueBenefitVerificationCode = (benefitType: string, talentId: string, cardId: string, merchantId: string, idempotencyKey: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => {
	const data: any = { benefitType: benefitType, merchantId: Number(merchantId) }
	if (talentId != '') data.talentId = Number(talentId)
	if (cardId != '') data.cardId = Number(cardId)
	return request<any>({ path: getApiEndpoint('appBenefitVerificationCodes'), method: 'POST', data: data, idempotencyKey: idempotencyKey }, onSuccess, onError)
}
export const getBenefitQuota = (benefitType: string, talentId: string, cardId: string, merchantId: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => {
	const data: any = { benefitType: benefitType, merchantId: Number(merchantId) }
	if (talentId != '') data.talentId = Number(talentId)
	if (cardId != '') data.cardId = Number(cardId)
	return request<any>({ path: getApiEndpoint('appBenefitVerificationCodes') + '/quota', method: 'GET', data: data }, onSuccess, onError)
}
export const getCinemaCouponClaim = (claimId: string, talentId: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => request<any>({ path: getApiEndpoint('appCinemaCouponClaims') + encodeURIComponent(claimId), method: 'GET', skipAuth: true, data: { talentId: talentId } }, onSuccess, onError)
export const getTicketPackageClaim = (claimId: string, talentId: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => request<any>({ path: getApiEndpoint('appTicketPackageClaims') + encodeURIComponent(claimId), method: 'GET', skipAuth: true, data: { talentId: Number(talentId) } }, onSuccess, onError)
export const getCinemaCodeImageUrl = (claimId: string): string => resolveApiUrl(getApiEndpoint('appBenefitVerificationCodes') + '/' + encodeURIComponent(claimId) + '/image')
/** 观影权益（票包兑换码）的二维码：内容是后台批量生成、绑影城的那串兑换码。 */
export const getTicketPackageCodeImageUrl = (claimId: string): string => resolveApiUrl(getApiEndpoint('appBenefitVerificationCodes') + '/ticket-package-claims/' + encodeURIComponent(claimId) + '/image')

export const searchMerchants = (keyword: string, categoryId: string, mode: string, talentId: string, page: number, size: number, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => {
	const data: any = { keyword: keyword, categoryId: categoryId, mode: mode, page: page, size: size, sortField: 'sort', sortOrder: 'asc' }
	if (talentId != '') data.talentId = Number(talentId)
	return request<any>({
		path: getApiEndpoint('appMerchants'),
		method: 'GET',
		skipAuth: true,
		data: data
	}, onSuccess, onError)
}

export const getMerchantDetail = (merchantId: string, talentId: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => {
	const data: any = {}
	if (talentId != '') data.talentId = Number(talentId)
	return request<any>({ path: getApiEndpoint('appMerchantDetail') + encodeURIComponent(merchantId), method: 'GET', skipAuth: true, data: data }, onSuccess, onError)
}

export const getProducts = (merchantId: string, talentId: string, page: number, size: number, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => {
	const data: any = { merchantId: merchantId, page: page, size: size }
	if (talentId != '') data.talentId = Number(talentId)
	return request<any>({ path: getApiEndpoint('appProducts'), method: 'GET', skipAuth: true, data: data }, onSuccess, onError)
}

export const getProductDetail = (productId: string, talentId: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => {
	const data: any = {}
	if (talentId != '') data.talentId = Number(talentId)
	return request<any>({ path: getApiEndpoint('appProductDetail') + encodeURIComponent(productId), method: 'GET', data: data }, onSuccess, onError)
}

export const getOrderCoupons = (merchantId: string, productId: string, orderId: string, talentId: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => {
	return request<any>({
		path: getApiEndpoint('appOrderCoupons'),
		method: 'GET',
		data: { merchantId: merchantId, productId: productId, orderId: orderId, talentId: talentId }
	}, onSuccess, onError)
}

export const getOrderCouponQuote = (merchantId: string, productId: string, orderId: string, couponId: string, talentId: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => {
	const data: any = { merchantId: merchantId, productId: productId, orderId: orderId, talentId: talentId }
	if (couponId != '') data.couponId = couponId
	return request<any>({ path: getApiEndpoint('appOrderCouponQuote'), method: 'GET', data: data }, onSuccess, onError)
}

export const createAppOrder = (merchantId: string, productId: string, couponId: string, idempotencyKey: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => {
	const data: any = { merchantId: Number(merchantId), productId: Number(productId) }
	if (couponId != '') data.couponId = Number(couponId)
	return request<any>({ path: getApiEndpoint('appOrders'), method: 'POST', data: data, idempotencyKey: idempotencyKey }, onSuccess, onError)
}

export const getTalentCardProfile = (talentId: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => {
	const data: any = {}
	if (talentId != '') data.talentId = talentId
	return request<any>({ path: getApiEndpoint('appTalentCardProfile'), method: 'GET', skipAuth: true, data: data }, onSuccess, onError)
}
export const getFamilyCardAccess = (talentId: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => {
	const data: any = {}
	if (talentId != '') data.talentId = talentId
	return request<any>({ path: getApiEndpoint('appFamilyCardAccess'), method: 'GET', skipAuth: true, data: data }, onSuccess, onError)
}
export const getAgreement = (type: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => request<any>({ path: getApiEndpoint('appAgreements') + encodeURIComponent(type), method: 'GET', skipAuth: true }, onSuccess, onError)
export const getTicketPackages = (talentId: string, cardId: string, merchantId: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => {
	const data: any = {}
	if (talentId != '') data.talentId = Number(talentId)
	if (cardId != '') data.cardId = Number(cardId)
	if (merchantId != '') data.merchantId = Number(merchantId)
	return request<any>({ path: getApiEndpoint('appTicketPackages'), method: 'GET', skipAuth: true, data: data }, onSuccess, onError)
}
export const claimTicketPackage = (packageId: string, talentId: string, cardId: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void, merchantId: string = ''): (() => void) => {
	const data: any = { talentId: Number(talentId) }
	if (cardId != '') data.cardId = Number(cardId)
	if (merchantId != '') data.merchantId = Number(merchantId)
	return request<any>({ path: getApiEndpoint('appTicketPackages') + '/' + encodeURIComponent(packageId) + '/claims', method: 'POST', skipAuth: true, data: data, idempotencyKey: 'ticket-package-claim-' + packageId + '-' + talentId + '-' + Date.now() }, onSuccess, onError)
}
export const getDisplayMerchantPolicies = (talentId: string, cardId: string, merchantType: string, keyword: string, page: number, size: number, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => {
	const data: any = { page: page, size: size }
	if (talentId != '') data.talentId = Number(talentId)
	if (cardId != '') data.cardId = Number(cardId)
	if (merchantType != '') data.merchantType = merchantType
	if (keyword != '') data.keyword = keyword
	return request<any>({ path: getApiEndpoint('appDisplayMerchantPolicies'), method: 'GET', skipAuth: true, data: data }, onSuccess, onError)
}
export const getDisplayMerchantPolicyDetail = (merchantId: string, talentId: string, cardId: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => {
	const data: any = {}
	if (talentId != '') data.talentId = Number(talentId)
	if (cardId != '') data.cardId = Number(cardId)
	return request<any>({ path: getApiEndpoint('appDisplayMerchantPolicies') + '/' + encodeURIComponent(merchantId), method: 'GET', skipAuth: true, data: data }, onSuccess, onError)
}
export const recordDisplayMerchantPolicyEvent = (merchantId: string, talentId: string, cardId: string, eventType: string, remark: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => {
	const data: any = { eventType: eventType }
	if (talentId != '') data.talentId = Number(talentId)
	if (cardId != '') data.cardId = Number(cardId)
	if (remark != '') data.remark = remark
	return request<any>({ path: getApiEndpoint('appDisplayMerchantPolicies') + '/' + encodeURIComponent(merchantId) + '/events', method: 'POST', skipAuth: true, data: data }, onSuccess, onError)
}

export const issueTalentDynamicCode = (talentId: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => {
	return request<any>({
		path: getApiEndpoint('appTalentDynamicCode') + encodeURIComponent(talentId) + '/dynamic-codes',
		method: 'POST',
		skipAuth: true
	}, onSuccess, onError)
}
export const issueTalentDynamicCodeForScene = (talentId: string, scene: string, merchantId: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => {
	const data: any = { scene: scene }
	if (/^\d+$/.test(merchantId)) data.merchantId = Number(merchantId)
	return request<any>({ path: getApiEndpoint('appTalentDynamicCode') + encodeURIComponent(talentId) + '/dynamic-codes', method: 'POST', data: data }, onSuccess, onError)
}

export const getCardUpgrade = (talentId: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => {
	return request<any>({ path: getApiEndpoint('appCardUpgrades'), method: 'GET', skipAuth: true, data: { talentId: talentId } }, onSuccess, onError)
}

export const submitCardUpgrade = (payload: any, idempotencyKey: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => {
	return request<any>({ path: getApiEndpoint('appCardUpgrades'), method: 'POST', skipAuth: true, data: payload, idempotencyKey: idempotencyKey }, onSuccess, onError)
}

export const getUsageRecords = (talentId: string, status: string, page: number, size: number, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => {
	const data: any = { talentId: talentId, page: page, size: size }
	if (status.length > 0) data.status = status
	return request<any>({ path: getApiEndpoint('appUsageRecords'), method: 'GET', skipAuth: true, data: data }, onSuccess, onError)
}

export const getPendingOrder = (orderNo: string, talentId: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => {
	return request<any>({ path: getApiEndpoint('appPendingOrders') + encodeURIComponent(orderNo), method: 'GET', skipAuth: true, data: { talentId: talentId } }, onSuccess, onError)
}

export const getPendingOrderCoupons = (orderNo: string, talentId: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => {
	return request<any>({ path: getApiEndpoint('appPendingOrders') + encodeURIComponent(orderNo) + '/coupons', method: 'GET', skipAuth: true, data: { talentId: talentId } }, onSuccess, onError)
}

export const selectPendingOrderCoupon = (orderNo: string, talentId: string, couponId: string, priceVersion: number, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => {
	return request<any>({ path: getApiEndpoint('appPendingOrders') + encodeURIComponent(orderNo) + '/coupon', method: 'POST', skipAuth: true, data: { talentId: Number(talentId), couponId: Number(couponId), priceVersion: priceVersion } }, onSuccess, onError)
}

export const cancelPendingOrder = (orderNo: string, talentId: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => {
	return request<any>({ path: getApiEndpoint('appPendingOrders') + encodeURIComponent(orderNo) + '/cancel', method: 'POST', data: { talentId: Number(talentId) } }, onSuccess, onError)
}

export const createPendingOrderPayment = (orderNo: string, talentId: string, priceVersion: number, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => {
	return request<any>({
		path: getApiEndpoint('appPendingOrders') + encodeURIComponent(orderNo) + '/payment',
		method: 'POST',
		data: { talentId: Number(talentId), priceVersion: priceVersion },
		idempotencyKey: 'pending-payment-' + orderNo + '-' + priceVersion
	}, onSuccess, onError)
}

export const getPendingOrderPaymentStatus = (orderNo: string, talentId: string, paymentNo: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => {
	const data: any = { talentId: talentId }
	if (paymentNo != '') data.paymentNo = paymentNo
	return request<any>({ path: getApiEndpoint('appPendingOrders') + encodeURIComponent(orderNo) + '/payment', method: 'GET', data: data }, onSuccess, onError)
}

// 「放弃支付」：用户在收银台里点了关闭时告诉服务端。服务端只据此触发关单、不据此判定收款，
// 所以调用方不要把它的返回当成支付结论（页面照常按待支付渲染，真正的结论由支付状态查询给出）。
export const abandonPendingOrderPayment = (orderNo: string, talentId: string, paymentNo: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => {
	const data: any = { talentId: Number(talentId) }
	if (paymentNo != '') data.paymentNo = paymentNo
	return request<any>({ path: getApiEndpoint('appPendingOrders') + encodeURIComponent(orderNo) + '/payment/abandon', method: 'POST', data: data }, onSuccess, onError)
}

export const getPendingUseOrder = (orderNo: string, talentId: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => request<any>({ path: getApiEndpoint('appPendingUseOrders') + encodeURIComponent(orderNo), method: 'GET', skipAuth: true, data: { talentId: talentId } }, onSuccess, onError)
export const getCompletedOrder = (orderNo: string, talentId: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => request<any>({ path: getApiEndpoint('appCompletedOrders') + encodeURIComponent(orderNo), method: 'GET', skipAuth: true, data: { talentId: talentId } }, onSuccess, onError)
export const getCancelledOrder = (orderNo: string, talentId: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => request<any>({ path: getApiEndpoint('appCancelledOrders') + encodeURIComponent(orderNo), method: 'GET', skipAuth: true, data: { talentId: talentId } }, onSuccess, onError)
export const getProcessingApplication = (applicationNo: string, talentId: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => request<any>({ path: getApiEndpoint('appProcessingApplications') + encodeURIComponent(applicationNo), method: 'GET', skipAuth: true, data: { talentId: talentId } }, onSuccess, onError)
export const getFamilyCards = (talentCardId: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => request<any>({ path: getApiEndpoint('appFamilyCards'), method: 'GET', skipAuth: true, data: { talentCardId: Number(talentCardId) } }, onSuccess, onError)
export const getFreeBenefit = (talentId: string, merchantId: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => request<any>({ path: getApiEndpoint('appFreeBenefits'), method: 'GET', skipAuth: true, data: { talentId: talentId, merchantId: merchantId } }, onSuccess, onError)
export const issueFreeBenefitCode = (talentId: string, merchantId: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => request<any>({ path: getApiEndpoint('appFreeBenefits') + '/dynamic-code', method: 'POST', skipAuth: true, data: { talentId: Number(talentId), merchantId: Number(merchantId) } }, onSuccess, onError)
export const createFamilyCardApplication = (talentCardId: string, dependentName: string, dependentIdCardNo: string, dependentMobile: string, relation: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => request<any>({ path: getApiEndpoint('appFamilyCards'), method: 'POST', skipAuth: true, data: { talentCardId: Number(talentCardId), dependentName: dependentName, dependentIdCardNo: dependentIdCardNo, dependentMobile: dependentMobile, relation: relation }, idempotencyKey: 'family-card-apply-' + talentCardId + '-' + Date.now() }, onSuccess, onError)
export const getFamilyCardApplyDetail = (applicationId: string, talentCardId: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => request<any>({ path: getApiEndpoint('appFamilyCards') + '/' + encodeURIComponent(applicationId), method: 'GET', skipAuth: true, data: { talentCardId: Number(talentCardId) } }, onSuccess, onError)
export const resubmitFamilyCardApplication = (applicationId: string, talentCardId: string, dependentName: string, dependentIdCardNo: string, dependentMobile: string, relation: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => request<any>({ path: getApiEndpoint('appFamilyCards') + '/' + encodeURIComponent(applicationId) + '/resubmit', method: 'POST', skipAuth: true, data: { talentCardId: Number(talentCardId), dependentName: dependentName, dependentIdCardNo: dependentIdCardNo, dependentMobile: dependentMobile, relation: relation }, idempotencyKey: 'family-card-resubmit-' + applicationId + '-' + Date.now() }, onSuccess, onError)
export const unbindFamilyCard = (applicationId: string, talentCardId: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => request<any>({ path: getApiEndpoint('appFamilyCards') + '/' + encodeURIComponent(applicationId) + '/unbind', method: 'POST', skipAuth: true, data: { talentCardId: Number(talentCardId) }, idempotencyKey: 'family-card-unbind-' + applicationId }, onSuccess, onError)
export const unbindFamilyCards = (applicationIds: string[], talentCardId: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => {
	let index = 0
	const runNext = (): void => {
		if (index >= applicationIds.length) { onSuccess(null); return }
		const id = applicationIds[index]
		index = index + 1
		unbindFamilyCard(id, talentCardId, () => runNext(), onError)
	}
	runNext()
	return () => {}
}
export const issueOrderVerificationCode = (orderNo: string, talentId: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => request<any>({ path: getApiEndpoint('appPendingUseOrders') + encodeURIComponent(orderNo) + '/verification-code', method: 'POST', skipAuth: true, data: { talentId: Number(talentId) } }, onSuccess, onError)
export const refundPendingUseOrder = (orderNo: string, talentId: string, reason: string, onSuccess: (data: any) => void, onError: (error: HttpError) => void): (() => void) => request<any>({ path: getApiEndpoint('appPendingUseOrders') + encodeURIComponent(orderNo) + '/refund', method: 'POST', skipAuth: true, data: { talentId: Number(talentId), reason: reason }, idempotencyKey: 'refund-' + orderNo }, onSuccess, onError)
