import { css, html, nothing } from 'lit'; import { customElement, property } from 'lit/decorators.js'; import { MOON_PHASE_EMOJI } from '../tokens/index.js'; import type { GetCurrentMoonPhaseResponse, GetMoonCalendarResponse, GetUpcomingMoonPhasesResponse, } from '../types/index.js'; import { RoxyDataElement } from '../utils/base-element.js'; import { baseStyles } from '../utils/base-styles.js'; import { formatNumber } from '../utils/format.js'; type MoonPhaseData = | GetCurrentMoonPhaseResponse | GetUpcomingMoonPhasesResponse | GetMoonCalendarResponse; type MoonListEntry = | GetUpcomingMoonPhasesResponse['phases'][number] | GetMoonCalendarResponse['calendar'][number]; /** * Moon phase card. Renders /astrology/moon-phase/{current,upcoming,calendar/...}. */ @customElement('roxy-moon-phase') export class RoxyMoonPhase extends RoxyDataElement { static styles = [ baseStyles, css` .card { background: var(--roxy-surface, #fff); color: var(--roxy-fg, #0a0a0a); border: 1px solid var(--roxy-border, #e4e4e7); border-radius: var(--roxy-radius-md, 8px); padding: var(--roxy-space-lg, 1.5rem); box-shadow: var(--roxy-shadow-sm); display: grid; gap: var(--roxy-space-md, 1rem); } .hero { display: flex; align-items: center; gap: var(--roxy-space-md, 1rem); } .emoji { font-size: 3rem; line-height: 1; } .label { margin: 0; font-size: var(--roxy-text-lg, 1.125rem); font-weight: var(--roxy-weight-bold, 600); text-transform: capitalize; } .date { color: var(--roxy-muted, #71717a); font-size: var(--roxy-text-sm, 0.875rem); } .stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(8rem, 1fr)); gap: var(--roxy-space-md, 1rem); font-size: var(--roxy-text-sm, 0.875rem); color: var(--roxy-secondary, #475569); } .stats div span:first-child { display: block; color: var(--roxy-muted, #71717a); font-size: var(--roxy-text-xs, 0.75rem); text-transform: uppercase; letter-spacing: 0.06em; } .stats strong { color: var(--roxy-fg, #0a0a0a); font-variant-numeric: tabular-nums; } .meaning { color: var(--roxy-fg, #0a0a0a); } .keywords { display: flex; flex-wrap: wrap; gap: var(--roxy-space-xs, 0.25rem); margin-top: var(--roxy-space-sm, 0.5rem); } .keywords span { background: color-mix(in srgb, var(--roxy-accent, #f59e0b) 14%, transparent); padding: 2px 8px; border-radius: var(--roxy-radius-full, 9999px); font-size: var(--roxy-text-xs, 0.75rem); } .list { display: grid; gap: var(--roxy-space-sm, 0.5rem); } .list-item { display: grid; grid-template-columns: 2.5rem 1fr auto; gap: var(--roxy-space-sm, 0.5rem); align-items: center; border-bottom: 1px solid var(--roxy-border, #e4e4e7); padding: var(--roxy-space-sm, 0.5rem) 0; font-size: var(--roxy-text-sm, 0.875rem); } .list-item:last-child { border-bottom: none; } `, ]; @property({ type: String, reflect: true }) mode: 'current' | 'upcoming' | 'calendar' = 'current'; protected renderEmpty() { return html`
No moon phase data
`; } protected renderData(d: MoonPhaseData) { const list: MoonListEntry[] = 'phases' in d ? d.phases : 'calendar' in d ? d.calendar : []; if (this.mode !== 'current' && list.length > 0) { const month = 'month' in d ? d.month : undefined; const year = 'year' in d ? d.year : undefined; return html`

${month ?? 'Moon phases'} ${year ?? ''}

${list.map((phase) => this.renderListItem(phase))}
`; } if (!('phase' in d)) return nothing; return this.renderSingle(d); } private renderSingle(d: GetCurrentMoonPhaseResponse) { // The API ships the exact phase emoji in meaning.symbol; prefer it and fall // back to the name-derived glyph for the list endpoints that omit meaning. const emoji = d.meaning?.symbol || phaseEmoji(d.phase); return html`

${d.phase ?? 'Moon'}

${d.date ? html`
${d.date}
` : nothing}
${ typeof d.illumination === 'number' ? html`
Illumination ${formatIllumination(d.illumination)}
` : nothing } ${ typeof d.age === 'number' ? html`
Age ${formatNumber(d.age, 1)} days
` : nothing } ${ d.sign ? html`
Sign ${d.sign}
` : nothing } ${ typeof d.distance === 'number' ? html`
Distance ${(d.distance / 1000).toFixed(0)}k km
` : nothing }
${ d.meaning?.description ? html`

${d.meaning.description}

` : nothing } ${ d.meaning?.keywords?.length ? html`
${d.meaning.keywords.map((k) => html`${k}`)}
` : nothing }
`; } private renderListItem(p: MoonListEntry) { const emoji = phaseEmoji(p.phase); return html`
${p.phase} ${p.date ?? ''}
`; } } /** * Map a phase name to its emoji, tolerant of the live API naming. The API sends * suffixed names ("Waxing Gibbous Moon") and "Third Quarter Moon" where the map * keys are unsuffixed and use "last quarter"; only "new moon"/"full moon" keep * the suffix. Try the raw lowercase, then the suffix stripped, then the * third->last quarter alias, then the alias re-suffixed, so every one of the * eight phases resolves in both the suffixed and unsuffixed forms. */ function phaseEmoji(phase: string | undefined): string { if (!phase) return '🌙'; const lc = phase.toLowerCase().trim(); const noMoon = lc.replace(/\s*moon$/, '').trim(); const alias = noMoon === 'third quarter' ? 'last quarter' : noMoon; return ( MOON_PHASE_EMOJI[lc] ?? MOON_PHASE_EMOJI[noMoon] ?? MOON_PHASE_EMOJI[alias] ?? MOON_PHASE_EMOJI[`${alias} moon`] ?? '🌙' ); } function formatIllumination(v: number): string { const pct = v <= 1 ? v * 100 : v; return `${Math.round(pct)}%`; } declare global { interface HTMLElementTagNameMap { 'roxy-moon-phase': RoxyMoonPhase; } }