import {marketApi} from '@/mammon'; import {getMarketTypes, MarketType} from '../type'; import {MarketTypePrice} from './MarketTypePrice'; export type LocationPrice = { buyUnit: number; sellUnit: number; buy: number; sell: number; }; export type ComparedItem = { typeId: number; name: string; quantity: number; volume: number; prices: Record; }; export type PricesForLocation = (types: MarketType[], locationId: number) => Promise; export const compareAppraisal = async ( raw: string, locationIds: number[], getPrices: PricesForLocation ): Promise => { if (!raw?.trim() || locationIds.length === 0) { return []; } const stacks = await marketApi.parseStacks(raw).then(r => r.data); if (stacks.length === 0) { return []; } const quantities = new Map(stacks.map(s => [s.marketTypeId, s.quantity])); const types = await getMarketTypes(stacks.map(s => s.marketTypeId)); const rows = new Map(types.map(t => [t.id, { typeId: t.id, name: t.name, quantity: quantities.get(t.id) ?? 0, volume: t.volume, prices: {}, }])); const pricesByLocation = await Promise.all( locationIds.map(async locationId => [locationId, await getPrices(types, locationId)] as const) ); pricesByLocation.forEach(([locationId, prices]) => prices.forEach(p => { const row = rows.get(p.type.id); if (row) { row.prices[locationId] = { buyUnit: p.buy, sellUnit: p.sell, buy: p.buy * row.quantity, sell: p.sell * row.quantity, }; } })); return [...rows.values()]; };