import { renderTypography } from "./typography/renderTypography.js";
import { Icon } from "./Icon/Icon.js";
import styled from "@emotion/styled";
import { jsx, jsxs } from "@emotion/react/jsx-runtime";
import { CurrencyUnit, formatCurrencyStr, getCurrencyAmount, isCurrencyMap, isUmaCurrencyAmount, mapCurrencyAmount } from "@lightsparkdev/core";
//#region src/components/CurrencyAmount.tsx
function CurrencyAmount({ amount, displayUnit: displayUnitProp, shortNumber = false, showUnits = false, includeEstimatedIndicator = false, fullPrecision = false, id, ml = 0, typography, unitsPerBtc, showCurrencyIcon = true }) {
	let value;
	let defaultFormattedNumber;
	let displayUnitStr;
	let currencyAmountForFormatting;
	if (isUmaCurrencyAmount(amount)) {
		value = amount.value;
		defaultFormattedNumber = formatCurrencyStr(amount);
		displayUnitStr = amount.currency.code === "SAT" ? CurrencyUnit.SATOSHI : amount.currency.code;
		currencyAmountForFormatting = amount;
	} else {
		let amountMap;
		let displayUnit;
		if (isCurrencyMap(amount)) {
			displayUnit = displayUnitProp || CurrencyUnit.SATOSHI;
			amountMap = amount;
		} else {
			const resolvedCurrencyAmount = getCurrencyAmount(amount);
			displayUnit = displayUnitProp || resolvedCurrencyAmount.unit;
			amountMap = mapCurrencyAmount(amount, unitsPerBtc);
		}
		value = amountMap[displayUnit];
		defaultFormattedNumber = amountMap.formatted[displayUnit];
		displayUnitStr = displayUnit;
		currencyAmountForFormatting = {
			value: Number(value),
			unit: displayUnit
		};
	}
	const appendUnits = showUnits === false ? void 0 : showUnits === true ? {
		plural: false,
		lowercase: false,
		showForCurrentLocaleUnit: false
	} : showUnits;
	let formattedNumber = defaultFormattedNumber;
	if (shortNumber) formattedNumber = formatCurrencyStr(currencyAmountForFormatting, {
		precision: 1,
		compact: true,
		appendUnits
	});
	else if (fullPrecision) formattedNumber = formatCurrencyStr(currencyAmountForFormatting, {
		precision: "full",
		appendUnits
	});
	else if (appendUnits) formattedNumber = formatCurrencyStr(currencyAmountForFormatting, { appendUnits });
	let content = formattedNumber;
	if (typography && typography.type) content = renderTypography(typography.type, {
		size: typography.size,
		color: typography.color,
		children: formattedNumber
	});
	return /* @__PURE__ */ jsxs(StyledCurrencyAmount, {
		ml,
		id,
		children: [
			includeEstimatedIndicator && "Est. ",
			showCurrencyIcon ? /* @__PURE__ */ jsx(CurrencyIcon, { unit: displayUnitStr }) : null,
			content
		]
	});
}
const CurrencyIcon = ({ unit }) => {
	switch (unit) {
		case CurrencyUnit.BITCOIN: return /* @__PURE__ */ jsx(Icon, {
			name: "BitcoinB",
			width: 8,
			verticalAlign: -2,
			mr: 2
		});
		case CurrencyUnit.SATOSHI: return /* @__PURE__ */ jsx(Icon, {
			name: "Satoshi",
			width: 8,
			verticalAlign: -2,
			mr: 2
		});
		default: return null;
	}
};
const StyledCurrencyAmount = styled.span`
  color: inherit !important;
  white-space: nowrap;
  font-feature-settings:
    "tnum" on,
    "lnum" on;
  margin-left: ${({ ml }) => ml}px;
`;
//#endregion
export { CurrencyAmount, CurrencyIcon };
