import { ItemId } from "../definitions/items.js"; import { dyeColorKey } from "../definitions/dyes.js"; export function dyeFillLevel (stack) { return Math.max(1, Math.min(5, stack?.instance?.fillLevel | 0 || 4)); } export function dyeStacksForColor (slots, colorKey) { const out = []; for (let index = 0; index < (slots?.length ?? 0); index++) { const stack = slots[index]; if (!stack || stack.itemId !== ItemId.dye) { continue; } if (dyeColorKey(stack.instance) !== colorKey) { continue; } out.push({ index, stack, fill: dyeFillLevel(stack) }); } return out; } /** Bag slot of the chosen colour with the least fill remaining. */ export function pickEmptiestDyeSlot (slots, colorKey) { const matches = dyeStacksForColor(slots, colorKey); if (!matches.length) { return null; } matches.sort((a, b) => a.fill - b.fill || a.index - b.index); return matches[0].index; } /** Fullest bottle of a colour — shown in the dye picker. */ export function pickFullestDyeDisplay (slots, colorKey) { const matches = dyeStacksForColor(slots, colorKey); if (!matches.length) { return null; } matches.sort((a, b) => b.fill - a.fill || a.index - b.index); return matches[0]; } /** One entry per unique dye colour (fullest bottle for display). */ export function listDyePickerChoices (slots) { const byColor = new Map(); for (let index = 0; index < (slots?.length ?? 0); index++) { const stack = slots[index]; if (!stack || stack.itemId !== ItemId.dye) { continue; } const key = dyeColorKey(stack.instance); if (!key) { continue; } const fill = dyeFillLevel(stack); const prev = byColor.get(key); if (!prev || fill > prev.fill || (fill === prev.fill && index < prev.displayIndex)) { byColor.set(key, { colorKey: key, displayIndex: index, displayStack: stack, fill }); } } return [...byColor.values()].sort((a, b) => a.displayIndex - b.displayIndex); } export function resolveDyeColorKey (slots, options = {}) { if (options.dyeColorKey != null) { return options.dyeColorKey; } if (options.dyeSlotIndex == null) { return null; } const picked = slots?.[options.dyeSlotIndex | 0]; return picked ? dyeColorKey(picked.instance) : null; } export function resolveDyeConsumeSlot (slots, options = {}) { const colorKey = resolveDyeColorKey(slots, options); if (!colorKey) { return null; } return pickEmptiestDyeSlot(slots, colorKey); } export function resolveDyeDisplayStack (slots, options = {}) { const colorKey = resolveDyeColorKey(slots, options); if (!colorKey) { return null; } return pickFullestDyeDisplay(slots, colorKey)?.displayStack ?? null; }