'use client'
import { Popover, PopoverContent, PopoverTrigger, SizableText, XStack, YStack } from '@hanzo/gui'
import { useId, useState, type KeyboardEvent } from 'react'
import { useMarks } from './asset'
import { Caret, Tick } from './caret'
import { lands } from './choice'
import { SIZE } from './coin'
import { figures, type Reading } from './figure'
import { Picture } from './picture'
import { paint } from '../material/paint'
import { reach } from '../material/reach'
import { weight } from '../material/weight'
/**
* The currency a sum is stated in, and the way to state it in another.
*
* A form asking for an amount in nothing in particular asks for nothing, so a
* figure carries a currency and this is where a person changes it. Nothing is
* converted by the press: the amount is what somebody types and the currency is
* what they type it in, which is the pair the other side is handed. Converting
* would be a second claim, at a rate this component does not have.
*
* A mark and the stroke that opens the list, and no words. Words here read as
* the same value stated twice — once beside the sum and once above it.
*
* The reading arrives whole rather than as a currency and a language, because
* the symbols in the list come out of the same `Intl` table as the figures on
* the panel beside them, and two props are two chances for the list to write
* `US$` where the figures write `$`.
*/
/** What this control is painted with. Text left uncoloured resolves near-black
* on a black ground, so everything readable names its colour. */
const { plain, quiet, subtle } = paint
/**
* The region a currency belongs to.
*
* ISO 4217 builds a code out of the ISO 3166 region and a letter for the unit,
* so the region is the first two characters of every code — `EUR` included,
* whose `EU` is the Union's own region code. Nothing is looked up and no table
* can fall behind the list.
*
* Exported because it is the contract between this control and a build's
* `Marks.region`: the key a flag is filed under is this, lower-cased. Left
* implicit, a build files its flags under the currency code and the corner
* silently draws letters for every one of them.
*/
export const region = (of: string): string => of.slice(0, 2).toUpperCase()
/**
* The region spelled in regional indicators, which a reader's own system draws
* as its flag.
*
* What the corner falls back to where the tree serves no mark for a region. A
* system with no flag glyphs draws the two letters instead, which still names
* the region and is still not a sentence anybody has to have audited.
*/
const spell = (of: string): string =>
String.fromCodePoint(...[...of].map((c) => 0x1f1e6 + c.charCodeAt(0) - 65))
/** How wide the list stands. Off the longest code and its mark rather than off
* a device. */
const LIST = 200
/** How tall the list may stand before it scrolls. A panel taller than this is
* one a reader scrolls past rather than reads. */
const DROP = 320
/**
* How wide the symbol column is.
*
* `Intl` writes a dozen of these with three characters — `CA$`, `CN¥`, `ARS` —
* and at the width a single glyph needs they break over two lines and the row
* grows to hold them.
*/
const SIGN = 36
/** One currency: its own symbol, its code, and the tick where it is the one in
* force. The symbol comes out of `Intl` rather than from a flag, because a
* flag names a country — the euro has none and the dollar has a dozen. */
function Row({
id,
code,
sign,
here,
stop,
pick,
}: {
/** Where the arrows send focus. The list moves it by name rather than by
* position, so the id says which currency it lands on. */
id: string
code: string
sign: string
/** Whether this is the currency the figures are being written in. */
here: boolean
/** Whether this is the one entry the list is entered on. */
stop: boolean
pick: () => void
}) {
return (
}
alignItems="center"
gap="$3"
minHeight={reach.plain}
paddingHorizontal="$3"
borderRadius="$4"
cursor="pointer"
// Stated, because a button with no ground takes the browser's own. In a
// dark scheme that is `buttonface`, a mid grey, which paints every row of
// this panel as a slab with a hover fill a reader cannot see arrive.
backgroundColor="transparent"
hoverStyle={{ backgroundColor: subtle }}
focusVisibleStyle={paint.focus}
>
{sign}
{/* The alignment is stated because a button is centred by the browser's
own stylesheet, and a code given the whole row otherwise sits in the
middle of it — a column of codes on their centres, reading as a column
of nothing. */}
{code}
{/* The tick is the whole of what says which one is in force, and
`aria-selected` says the same thing to a reader who cannot see it.
Both, because a mark with no name is invisible to one reader and a
name with no mark is invisible to the other. */}
{here ? : null}
)
}
export function Money({
reading,
of,
set,
say,
}: {
/** Whose language the symbols are written in, and which currency is in
* force. The same reading the figures beside this are drawn from. */
reading: Reading
/** The codes offered, as ISO 4217. */
of: readonly string[]
set: (code: string) => void
/** What this control is called, in the reader's own words. It carries no
* visible label, so this is the whole of its accessible name. */
say: string
}) {
const [open, setOpen] = useState(false)
const named = useId()
const marks = useMarks()
const write = figures(reading)
const here = region(reading.currency)
const flag = marks.region?.(here.toLowerCase()) ?? null
/**
* Where the list is entered.
*
* The entry in force, and the first entry where the currency in force is not
* one of those offered — a saved preference that outlived a change to the
* list, which is the ordinary way this happens. Tied to the tick, the whole
* list took `-1`: nothing was a tab stop, so the popover had nothing to focus
* and the arrows had no entry to move from. A reader could open the list and
* not reach a single row in it.
*
* Which is why the entry in force and the entry focus lands on are two facts
* here rather than one. They agree whenever the reading is one of the codes,
* and a list has to be usable when it is not.
*/
const at = Math.max(0, of.indexOf(reading.currency))
return (
}
flexDirection="row"
alignItems="center"
gap="$1"
minHeight={reach.dense}
paddingHorizontal="$1"
borderRadius="$12"
borderWidth={0}
backgroundColor="transparent"
cursor="pointer"
hoverStyle={{ backgroundColor: subtle }}
focusVisibleStyle={paint.focus}
>
{/* Hidden from the accessible name, which is a currency's code and not
a country's flag — a reader hearing "flag: United States" for a
control that sets USD has been told the wrong fact.
A build that serves no flag for the region, and a build whose flag
does not arrive, both leave the two regional indicators, which name
the region either way. */}
{spell(here)}
}
/>
{/* Turned over while the list is open, which is what every other
control that opens one does. */}
{/* The arrows are handled where they arrive rather than on each entry:
a key pressed on a row reaches the list, and the list is the one
thing that knows what comes after what. `lands` is the same
function the radio group turns on, so the two answer Home, End and
the four arrows identically. */}
) => {
const from = of.findIndex((code) => document.activeElement?.id === `${named}${code}`)
if (from === -1) return
const to = lands(e.key, from, of.length)
if (to === null) return
// The page scrolls under an arrow key otherwise, and the list
// is what the key was meant for.
e.preventDefault()
document.getElementById(`${named}${of[to]}`)?.focus()
}}
/>
}
gap="$1"
>
{of.map((code, i) => (
{
set(code)
setOpen(false)
}}
/>
))}
)
}