import {isProductId} from "../lib/isProductId";
import {InvalidIdError} from "/src-com";
import {cacheFetch} from "../lib/cacheFetch";
import {queryNextData} from "../lib/queryNextData";

export async function executeSold(id: string | number) {
	if (!isProductId(id)) {
		throw new InvalidIdError(id);
	}
	const resp = await cacheFetch(`https://paypayfleamarket.yahoo.co.jp/item/${id}`);
	const text = await resp.text();
	if (resp.ok) {
		return queryNextData(text)?.initialState?.itemsState?.items?.item;
	}
	let data: any = {};
	if (resp.status === 404) {
		data.error = '404 Not Found'
	} else {
		data.error = resp.statusText || text
	}
	return data;
}
