import { ItemId } from "../definitions/items.js"; import { alchemyPotionDefinition } from "../definitions/alchemy.js"; export function potionFillLevel (stack) { return Math.max(1, Math.min(5, stack?.instance?.fillLevel | 0 || 4)); } /** Bag slots holding a success potion with the given potionKey. */ export function potionStacksForKey (slots, potionKey) { const key = String(potionKey ?? ""); const out = []; if (!key) { return out; } for (let index = 0; index < (slots?.length ?? 0); index++) { const stack = slots[index]; if (!stack || (stack.itemId | 0) !== ItemId.potion) { continue; } if (stack.instance?.kind !== "potion") { continue; } if (String(stack.instance?.potionKey ?? "") !== key) { continue; } if (stack.instance?.unstable) { continue; } out.push({ index, stack, fill: potionFillLevel(stack) }); } return out; } /** Bag slot of this brew with the least fill remaining. */ export function pickEmptiestPotionSlot (slots, potionKey) { const matches = potionStacksForKey(slots, potionKey); if (!matches.length) { return null; } matches.sort((a, b) => a.fill - b.fill || a.index - b.index); return matches[0].index; } export function hasPotionKey (slots, potionKey) { return potionStacksForKey(slots, potionKey).length > 0; } export function potionKeyDisplayName (potionKey) { return alchemyPotionDefinition(potionKey)?.displayName ?? "Brew"; }