import { authHeaders } from './auth' export interface CardImplementation { status: 'supported' | 'partial' | 'unsupported' manual: boolean } export interface DeckCard { oracleId: string name: string manaCost?: string typeLine: string colorIdentity: string[] oracleText?: string imageUri?: string implementation?: CardImplementation | null } export interface SavedDeck { id: string name: string commander: DeckCard cards: DeckCard[] cardBack?: string | null updatedAt: number } export interface ApiCard { oracle_id: string name: string mana_cost?: string type_line: string color_identity?: string[] oracle_text?: string image_uri?: string implementation?: CardImplementation | null } export function deckCardFromApi(card: ApiCard): DeckCard { return { oracleId: card.oracle_id, name: card.name, manaCost: card.mana_cost, typeLine: card.type_line, colorIdentity: card.color_identity ?? [], oracleText: card.oracle_text, imageUri: card.image_uri, implementation: card.implementation, } } export function newDeckId(): string { return typeof crypto !== 'undefined' && 'randomUUID' in crypto ? crypto.randomUUID() : `deck-${Date.now()}-${Math.random().toString(16).slice(2)}` } export async function loadDecks(apiBase: string): Promise { const { Authorization } = authHeaders() if (!Authorization) return [] try { const data = await $fetch<{ decks: SavedDeck[] }>(`${apiBase}/me/decks`, { headers: { Authorization }, }) return Array.isArray(data.decks) ? data.decks : [] } catch { return [] } } export async function saveDeck(apiBase: string, deck: SavedDeck): Promise { await $fetch(`${apiBase}/me/decks/${encodeURIComponent(deck.id)}`, { method: 'PUT', headers: authHeaders(), body: { name: deck.name, commander: deck.commander, cards: deck.cards, cardBack: deck.cardBack ?? null }, }) } export async function deleteDeck(apiBase: string, id: string): Promise { await $fetch(`${apiBase}/me/decks/${encodeURIComponent(id)}`, { method: 'DELETE', headers: authHeaders(), }) } export function isBasicLand(card: DeckCard): boolean { return /\bbasic\b/i.test(card.typeLine) && /\bland\b/i.test(card.typeLine) } export function canBeCommander(card: DeckCard): boolean { return (/\blegendary\b/i.test(card.typeLine) && /\bcreature\b/i.test(card.typeLine)) || /can be your commander/i.test(card.oracleText ?? '') } export function deckValidation(deck: SavedDeck): string[] { const errors: string[] = [] if (!canBeCommander(deck.commander)) errors.push('The commander must be a legendary creature.') if (deck.cards.length + 1 !== 100) { errors.push(`The deck has ${deck.cards.length + 1} cards; it needs exactly 100.`) } const identity = new Set(deck.commander.colorIdentity) const counts = new Map() for (const card of deck.cards) { counts.set(card.oracleId, (counts.get(card.oracleId) ?? 0) + 1) if (card.oracleId === deck.commander.oracleId) { errors.push('The commander cannot also be in the deck.') } if (card.colorIdentity.some(color => !identity.has(color))) { errors.push(`${card.name} is outside the commander's color identity.`) } } for (const [oracleId, count] of counts) { if (count < 2) continue const card = deck.cards.find(entry => entry.oracleId === oracleId) if (card && !isBasicLand(card)) { errors.push(`${card.name} appears ${count} times; only basic lands may repeat.`) } } return [...new Set(errors)] } export function exportDeckText(deck: SavedDeck): string { const counts = new Map() for (const card of deck.cards) { const entry = counts.get(card.oracleId) if (entry) entry.count++ else counts.set(card.oracleId, { card, count: 1 }) } const deckLines = [...counts.values()].map(entry => `${entry.count} ${entry.card.name}`) return [`Commander`, `1 ${deck.commander.name}`, '', 'Deck', ...deckLines].join('\n') }