import {ApiUrlBase, IPage, ISearchItem, ISearchParam} from "/src-com";
import {cacheFetch} from "../lib/cacheFetch";

export async function executeSearch(param: ISearchParam): Promise<IPage<ISearchItem>> {
	const url = 'http://localhost:3090/search';
	const resp = await cacheFetch(url, {
		method: 'POST',
		body: JSON.stringify(param),
		headers: {
			'Content-Type': 'application/json'
		}
	});
	const text = await resp.text();
	try {
		const result = JSON.parse(text);
		result.dataList || (result.dataList = []);
		return {...param, ...result};
	} catch {
	}
	return {...param, dataList: [], error: text || resp.statusText};
}

export async function commitSearchTask(page: IPage<any>): Promise<any> {
	const url = `${ApiUrlBase}/searchGoods/callbackTask`
	return await fetch(url, {
		method: 'POST',
		headers: {
			'Content-Type': 'application/json'
		},
		body: JSON.stringify({...page})
	}).then(r => r.json());
}
