import clsx from "clsx" import React, { forwardRef, useContext, useEffect, useImperativeHandle, useRef, useState, } from "react" import AmountField from "react-currency-input-field" import { Option } from "../../../types/shared" import { currencies, CurrencyType } from "../../../utils/currencies" import { normalizeAmount, persistedPrice } from "../../../utils/prices" import InputError from "../../atoms/input-error" import Tooltip from "../../atoms/tooltip" import MinusIcon from "../../fundamentals/icons/minus-icon" import PlusIcon from "../../fundamentals/icons/plus-icon" import InputHeader from "../../fundamentals/input-header" import Input from "../../molecules/input" import Select from "../../molecules/select" import { useTranslation } from "react-i18next" type CurrencyInputProps = { currencyCodes?: string[] currentCurrency?: string size?: "small" | "medium" | "full" readOnly?: boolean hideCurrency?: boolean onChange?: (currencyCode: string) => void className?: React.HTMLAttributes["className"] children?: React.ReactNode } type CurrencyInputState = { currencyInfo: CurrencyType | undefined } type AmountInputProps = { label?: string amount: number | undefined required?: boolean step?: number allowNegative?: boolean onChange?: (amount: number | undefined) => void onValidate?: (amount: number | undefined) => boolean invalidMessage?: string errors?: { [x: string]: unknown } name?: string } & Omit, "onChange"> const CurrencyContext = React.createContext({ currencyInfo: undefined, }) const getCurrencyInfo = (currencyCode?: string) => { if (!currencyCode) { return undefined } const currencyInfo = currencies[currencyCode.toUpperCase()] return currencyInfo } const Root: React.FC = ({ currentCurrency, currencyCodes, size = "full", readOnly = false, hideCurrency = false, onChange, children, className, }) => { const options: Option[] = currencyCodes?.map((code) => ({ label: code.toUpperCase(), value: code, })) ?? [] const { t } = useTranslation() const [selectedCurrency, setSelectedCurrency] = useState< CurrencyType | undefined >(getCurrencyInfo(currentCurrency)) const [value, setValue] = useState