import request from '@/utils/request'
export interface AxiosProgressEvent {
	loaded: number;
	total?: number;
	progress?: number;
	bytes: number;
	rate?: number;
	estimated?: number;
	upload?: boolean;
	download?: boolean;
	event?: any;
}
export interface WardrobeItem {
	buyerId: number
	categories: string[]
	designFor: 'female' | 'male' | 'all'
	page: number
	size: number
}
// 获取我的衣橱assets
export const fetchMyWardrobe = (data: WardrobeItem): Promise<ApiResponse> => {
	return request({
		url: '/buyer/buyer/order/assets/page',
		method: 'post',
		data
	})
}

export interface OrderItem {
	status?: number // 0未支付 1已支付 2已取消 不传查全部
	page: number
	size: number
}

export interface OrdersPageResponse {
	content: any[]
}
// 获取我的衣橱 orders
export const fetchMyOrders = (data: OrderItem): Promise<OrdersPageResponse> => {
	return request({
		url: '/buyer/buyer/order/page',
		method: 'get',
		params: data
	})
}

export interface Download {
	ids: string[]
}
// 下载资源
export const fetchDownloadItemsByGet = (params: Download, onDownloadProgress?: (event: AxiosProgressEvent) => void): Promise<ApiResponse> => {
	return request({
		url: '/buyer/listing/mall/main-product/download',
		method: 'get',
		responseType: 'blob',
		timeout: 600000,
		params,
		paramsSerializer: (p: any) => {
			const usp = new URLSearchParams()
			if (p && p.ids && Array.isArray(p.ids)) {
				p.ids.forEach((id: any) => usp.append('ids', String(id)))
			} else if (p) {
				Object.keys(p).forEach((k) => {
					const v = (p as any)[k]
					if (Array.isArray(v)) {
						v.forEach((x) => usp.append(k, String(x)))
					} else if (v !== undefined && v !== null) {
						usp.append(k, String(v))
					}
				})
			}
			return usp.toString()
		},
		onDownloadProgress
	})
}

// 获取用户信息
export const fetchUserProfile = (): Promise<ApiResponse> => {
	return request({
		url: '/buyer/profile/getProfile',
		method: 'post'
	})
}

// 设置用户信息
export interface UserProfile {
	firstName: string
	lastName: string
	username: string
	roles: string[]
	region: string
	language: string
	email: string
	oldPassword?: string
	newPassword?: string
	verifyCode?: string
}
export const updateUserProfile = (data: UserProfile): Promise<ApiResponse> => {
	return request({
		url: '/buyer/profile/setProfile',
		method: 'post',
		data
	})
}

// 获取设置页验证码
export const fetchVerifyCode = (): Promise<ApiResponse> => {
	return request({
		url: '/buyer/profile/sendEmailChangeCode',
		method: 'post'
	})
}

// 验证设置页验证码
export const verifyEmailCode = (verifyCode: string): Promise<ApiResponse> => {
	return request({
		url: '/buyer/profile/verifyEmailChangeCode',
		method: 'post',
		data: { verifyCode }
	})
}

// 获取用户语言设置
export const getUserLanguage = (): Promise<ApiResponse> => {
	return request({
		url: '/buyer/profile/getLanguage',
		method: 'post'
	})
}

// 设置语言
export const setUserLanguage = (language: string): Promise<ApiResponse> => {
	return request({
		url: '/buyer/profile/setLanguage',
		method: 'post',
		data: { language }
	})
}