import { jsx as c, jsxs as U } from "react/jsx-runtime";
import { useMemo as ui, useCallback as g } from "react";
import { useSlotRecipe as li } from "@chakra-ui/react/styled-system";
import { designTokens as pi } from "@commercetools/nimbus-tokens";
import { HighPrecision as mi } from "@commercetools/nimbus-icons";
import { e as di } from "./extract-style-props-CMyV-es-.es.js";
import { createSlotRecipeContext as yi } from "@chakra-ui/react";
import { G as hi } from "./group-DaIVSXIi.es.js";
import { i as Mi } from "./index-N3Xxo-X9.es.js";
import { n as p, u as Si } from "./normalize-messages-BN9BAFXe.es.js";
import { a as A } from "./mergeProps-B-cqsBhL.es.js";
import { $ as Pi } from "./context-BfHy4NgG.es.js";
import { S as L } from "./select-DiSjgVZP.es.js";
import { N as Ci } from "./number-input-BAaT6aky.es.js";
import { M as Ii } from "./make-element-focusable-DPGCjzPa.es.js";
import { T as H } from "./tooltip-cwtXJhIK.es.js";
import { B as bi } from "./box-DuBoAZvq.es.js";
const u = {
  ADP: { fractionDigits: 0 },
  AED: { fractionDigits: 2 },
  AFA: { fractionDigits: 2 },
  AFN: { fractionDigits: 2 },
  ALL: { fractionDigits: 2 },
  AMD: { fractionDigits: 2 },
  ANG: { fractionDigits: 2 },
  AOA: { fractionDigits: 2 },
  ARS: { fractionDigits: 2 },
  ATS: { fractionDigits: 2 },
  AUD: { fractionDigits: 2 },
  AWG: { fractionDigits: 2 },
  AYM: { fractionDigits: 2 },
  AZM: { fractionDigits: 2 },
  AZN: { fractionDigits: 2 },
  BAM: { fractionDigits: 2 },
  BBD: { fractionDigits: 2 },
  BDT: { fractionDigits: 2 },
  BEF: { fractionDigits: 0 },
  BGL: { fractionDigits: 2 },
  BGN: { fractionDigits: 2 },
  BHD: { fractionDigits: 3 },
  BIF: { fractionDigits: 0 },
  BMD: { fractionDigits: 2 },
  BND: { fractionDigits: 2 },
  BOB: { fractionDigits: 2 },
  BOV: { fractionDigits: 2 },
  BRL: { fractionDigits: 2 },
  BSD: { fractionDigits: 2 },
  BTN: { fractionDigits: 2 },
  BWP: { fractionDigits: 2 },
  BYB: { fractionDigits: 0 },
  BYN: { fractionDigits: 2 },
  BYR: { fractionDigits: 0 },
  BZD: { fractionDigits: 2 },
  CAD: { fractionDigits: 2 },
  CDF: { fractionDigits: 2 },
  CHF: { fractionDigits: 2 },
  CLF: { fractionDigits: 0 },
  CLP: { fractionDigits: 0 },
  CNY: { fractionDigits: 2 },
  COP: { fractionDigits: 2 },
  CRC: { fractionDigits: 2 },
  CSD: { fractionDigits: 2 },
  CUC: { fractionDigits: 2 },
  CUP: { fractionDigits: 2 },
  CVE: { fractionDigits: 2 },
  CYP: { fractionDigits: 2 },
  CZK: { fractionDigits: 2 },
  DEM: { fractionDigits: 2 },
  DJF: { fractionDigits: 0 },
  DKK: { fractionDigits: 2 },
  DOP: { fractionDigits: 2 },
  DZD: { fractionDigits: 2 },
  EEK: { fractionDigits: 2 },
  EGP: { fractionDigits: 2 },
  ERN: { fractionDigits: 2 },
  ESP: { fractionDigits: 0 },
  ETB: { fractionDigits: 2 },
  EUR: { fractionDigits: 2 },
  FIM: { fractionDigits: 2 },
  FJD: { fractionDigits: 2 },
  FKP: { fractionDigits: 2 },
  FRF: { fractionDigits: 2 },
  GBP: { fractionDigits: 2 },
  GEL: { fractionDigits: 2 },
  GHC: { fractionDigits: 2 },
  GHS: { fractionDigits: 2 },
  GIP: { fractionDigits: 2 },
  GMD: { fractionDigits: 2 },
  GNF: { fractionDigits: 0 },
  GRD: { fractionDigits: 0 },
  GTQ: { fractionDigits: 2 },
  GWP: { fractionDigits: 2 },
  GYD: { fractionDigits: 2 },
  HKD: { fractionDigits: 2 },
  HNL: { fractionDigits: 2 },
  HRK: { fractionDigits: 2 },
  HTG: { fractionDigits: 2 },
  HUF: { fractionDigits: 2 },
  IDR: { fractionDigits: 2 },
  IEP: { fractionDigits: 2 },
  ILS: { fractionDigits: 2 },
  INR: { fractionDigits: 2 },
  IQD: { fractionDigits: 3 },
  IRR: { fractionDigits: 2 },
  ISK: { fractionDigits: 0 },
  ITL: { fractionDigits: 0 },
  JMD: { fractionDigits: 2 },
  JOD: { fractionDigits: 3 },
  JPY: { fractionDigits: 0 },
  KES: { fractionDigits: 2 },
  KGS: { fractionDigits: 2 },
  KHR: { fractionDigits: 2 },
  KMF: { fractionDigits: 0 },
  KPW: { fractionDigits: 2 },
  KRW: { fractionDigits: 0 },
  KWD: { fractionDigits: 3 },
  KYD: { fractionDigits: 2 },
  KZT: { fractionDigits: 2 },
  LAK: { fractionDigits: 2 },
  LBP: { fractionDigits: 2 },
  LKR: { fractionDigits: 2 },
  LRD: { fractionDigits: 2 },
  LSL: { fractionDigits: 2 },
  LTL: { fractionDigits: 2 },
  LUF: { fractionDigits: 0 },
  LVL: { fractionDigits: 2 },
  LYD: { fractionDigits: 3 },
  MAD: { fractionDigits: 2 },
  MDL: { fractionDigits: 2 },
  MGA: { fractionDigits: 2 },
  MGF: { fractionDigits: 0 },
  MKD: { fractionDigits: 2 },
  MMK: { fractionDigits: 2 },
  MNT: { fractionDigits: 2 },
  MOP: { fractionDigits: 2 },
  MRO: { fractionDigits: 2 },
  MTL: { fractionDigits: 2 },
  MUR: { fractionDigits: 2 },
  MVR: { fractionDigits: 2 },
  MWK: { fractionDigits: 2 },
  MXN: { fractionDigits: 2 },
  MXV: { fractionDigits: 2 },
  MYR: { fractionDigits: 2 },
  MZM: { fractionDigits: 2 },
  MZN: { fractionDigits: 2 },
  NAD: { fractionDigits: 2 },
  NGN: { fractionDigits: 2 },
  NIO: { fractionDigits: 2 },
  NLG: { fractionDigits: 2 },
  NOK: { fractionDigits: 2 },
  NPR: { fractionDigits: 2 },
  NZD: { fractionDigits: 2 },
  OMR: { fractionDigits: 3 },
  PAB: { fractionDigits: 2 },
  PEN: { fractionDigits: 2 },
  PGK: { fractionDigits: 2 },
  PHP: { fractionDigits: 2 },
  PKR: { fractionDigits: 2 },
  PLN: { fractionDigits: 2 },
  PTE: { fractionDigits: 0 },
  PYG: { fractionDigits: 0 },
  QAR: { fractionDigits: 2 },
  ROL: { fractionDigits: 2 },
  RON: { fractionDigits: 2 },
  RSD: { fractionDigits: 2 },
  RUB: { fractionDigits: 2 },
  RUR: { fractionDigits: 2 },
  RWF: { fractionDigits: 0 },
  SAR: { fractionDigits: 2 },
  SBD: { fractionDigits: 2 },
  SCR: { fractionDigits: 2 },
  SDD: { fractionDigits: 2 },
  SDG: { fractionDigits: 2 },
  SEK: { fractionDigits: 2 },
  SGD: { fractionDigits: 2 },
  SHP: { fractionDigits: 2 },
  SIT: { fractionDigits: 2 },
  SKK: { fractionDigits: 2 },
  SLL: { fractionDigits: 2 },
  SOS: { fractionDigits: 2 },
  SRD: { fractionDigits: 2 },
  SRG: { fractionDigits: 2 },
  SSP: { fractionDigits: 2 },
  STD: { fractionDigits: 2 },
  SVC: { fractionDigits: 2 },
  SYP: { fractionDigits: 2 },
  SZL: { fractionDigits: 2 },
  THB: { fractionDigits: 2 },
  TJS: { fractionDigits: 2 },
  TMM: { fractionDigits: 2 },
  TMT: { fractionDigits: 2 },
  TND: { fractionDigits: 3 },
  TOP: { fractionDigits: 2 },
  TPE: { fractionDigits: 0 },
  TRL: { fractionDigits: 0 },
  TRY: { fractionDigits: 2 },
  TTD: { fractionDigits: 2 },
  TWD: { fractionDigits: 2 },
  TZS: { fractionDigits: 2 },
  UAH: { fractionDigits: 2 },
  UGX: { fractionDigits: 0 },
  USD: { fractionDigits: 2 },
  USN: { fractionDigits: 2 },
  USS: { fractionDigits: 2 },
  UYU: { fractionDigits: 2 },
  UZS: { fractionDigits: 2 },
  VEB: { fractionDigits: 2 },
  VEF: { fractionDigits: 2 },
  VND: { fractionDigits: 0 },
  VUV: { fractionDigits: 0 },
  WST: { fractionDigits: 2 },
  XAF: { fractionDigits: 0 },
  XAG: { fractionDigits: -1 },
  XAU: { fractionDigits: -1 },
  XBA: { fractionDigits: -1 },
  XBB: { fractionDigits: -1 },
  XBC: { fractionDigits: -1 },
  XBD: { fractionDigits: -1 },
  XCD: { fractionDigits: 2 },
  XDR: { fractionDigits: -1 },
  XFO: { fractionDigits: -1 },
  XFU: { fractionDigits: -1 },
  XOF: { fractionDigits: 0 },
  XPD: { fractionDigits: -1 },
  XPF: { fractionDigits: 0 },
  XPT: { fractionDigits: -1 },
  XSU: { fractionDigits: -1 },
  XTS: { fractionDigits: -1 },
  XUA: { fractionDigits: -1 },
  XXX: { fractionDigits: -1 },
  YER: { fractionDigits: 2 },
  YUM: { fractionDigits: 2 },
  ZAR: { fractionDigits: 2 },
  ZMK: { fractionDigits: 2 },
  ZMW: { fractionDigits: 2 },
  ZWD: { fractionDigits: 2 },
  ZWL: { fractionDigits: 2 },
  ZWN: { fractionDigits: 2 },
  ZWR: { fractionDigits: 2 }
}, { withProvider: Ri, withContext: m } = /* @__PURE__ */ yi({
  key: "nimbusMoneyInput"
}), Ai = /* @__PURE__ */ Ri(hi, "root"), Li = /* @__PURE__ */ m("div", "container"), Bi = /* @__PURE__ */ m("div", "currencySelect"), Ni = /* @__PURE__ */ m("label", "currencyLabel"), Fi = /* @__PURE__ */ m("input", "amountInput"), Ti = /* @__PURE__ */ m("div", "badge"), Gi = (i, o, r) => {
  if (!r) return null;
  const t = u[r];
  if (!t || i.length === 0 || i.trim() === "") return null;
  !o && t.fractionDigits === 0 && console.warn(
    `MoneyInput: A locale must be provided when currency has no fraction digits (${r})`
  );
  const s = parseFloat(i);
  if (isNaN(s)) return null;
  const a = Math.trunc(
    Math.round(s * 10 ** t.fractionDigits)
  ), e = (
    // The conversion to a string will always use a dot as the separator.
    // That means we don't have to handle a comma.
    String(s).indexOf(".") === -1 ? 0 : String(s).length - String(s).indexOf(".") - 1
  );
  return e > t.fractionDigits ? {
    type: "highPrecision",
    currencyCode: r,
    centAmount: a,
    preciseAmount: parseInt(
      // Here we need to convert  a number like 8.066652 to its centamount
      // We could do that by multiplying it with 10 ** number-of-fraction-digits
      // but then we'll run into problems with JavaScript's floating point
      // number precision and end up with 8066651.9999999, and then parseInt
      // cuts off the remainder.
      // So instead of using maths to convert the number, we just replace
      // the dot inside the number which does the same thing.
      // We don't need to replace "," as well, as numbers always us a dot
      // when converted using String().
      //
      // The mathematical way: amountAsNumber * 10 ** fractionDigitsOfAmount,
      String(s).replace(".", ""),
      10
    ),
    fractionDigits: e
  } : {
    type: "centPrecision",
    currencyCode: r,
    centAmount: a,
    fractionDigits: t.fractionDigits
  };
}, Ki = (i) => i.type === "highPrecision" ? i.preciseAmount / 10 ** i.fractionDigits : i.centAmount / 10 ** u[i.currencyCode].fractionDigits, Z = (i, o) => Object.prototype.hasOwnProperty.call(i, o);
function f(i, o) {
  return i && o ? `${i}.${o}` : void 0;
}
const V = (i, o) => Gi(
  typeof i.amount == "string" ? i.amount.trim() : "",
  o,
  i.currencyCode
), Oi = (i, o) => {
  if (!i) return { currencyCode: "", amount: "" };
  if (console.warn(
    typeof o == "string",
    "MoneyInput.parseMoneyValue: A locale must be passed as the second argument"
  ), console.warn(
    typeof i == "object",
    "MoneyInput.parseMoneyValue: Value must be passed as an object or be undefined"
  ), console.warn(
    typeof i.currencyCode == "string",
    'MoneyInput.parseMoneyValue: Value must contain "currencyCode"'
  ), typeof i.currencyCode != "string")
    throw new Error(
      'MoneyInput.parseMoneyValue: Value must contain "currencyCode"'
    );
  if (console.warn(
    Z(u, i.currencyCode),
    "MoneyInput.parseMoneyValue: Value must use known currency code"
  ), !Z(u, i.currencyCode))
    throw new Error(
      "MoneyInput.parseMoneyValue: Value must use known currency code"
    );
  console.warn(
    // highPrecision or centPrecision values must be set
    typeof i.centAmount == "number" || typeof i.preciseAmount == "number" && typeof i.fractionDigits == "number",
    'MoneyInput.parseMoneyValue: Value must contain "amount"'
  );
  const r = Ki(i), t = i.preciseAmount ? i.fractionDigits : u[i.currencyCode].fractionDigits;
  return { amount: isNaN(r) ? "" : r.toLocaleString(o, {
    minimumFractionDigits: t
  }), currencyCode: i.currencyCode };
}, w = (i) => !i || i?.amount?.trim() === "" || i?.currencyCode?.trim() === "", Y = (i, o) => w(i) ? !1 : V(i, o)?.type === "highPrecision", Ei = {
  amountInputLabel: "Amount",
  currencySelectLabel: "Currency",
  highPrecisionPrice: "High precision price"
}, Xi = {
  amountInputLabel: "Betrag",
  currencySelectLabel: "Währung",
  highPrecisionPrice: "Hochpräzisionspreis"
}, vi = {
  amountInputLabel: "Cantidad",
  currencySelectLabel: "Moneda",
  highPrecisionPrice: "Precio de alta precisión"
}, Ui = {
  amountInputLabel: "Montant",
  currencySelectLabel: "Devise",
  highPrecisionPrice: "Prix haute précision"
}, Hi = {
  amountInputLabel: "Valor",
  currencySelectLabel: "Moeda",
  highPrecisionPrice: "Preço de alta precisão"
}, Zi = {
  en: /* @__PURE__ */ p(Ei),
  de: /* @__PURE__ */ p(Xi),
  es: /* @__PURE__ */ p(vi),
  "fr-FR": /* @__PURE__ */ p(Ui),
  "pt-BR": /* @__PURE__ */ p(Hi)
}, D = (i) => {
  const {
    id: o,
    name: r,
    value: t,
    currencies: s = [],
    onChange: a,
    onValueChange: e,
    onAmountChange: B,
    onCurrencyChange: N,
    onFocus: d,
    onBlur: y,
    isDisabled: l,
    isReadOnly: F,
    isInvalid: W,
    isRequired: x,
    hasHighPrecisionBadge: T = !0,
    isCurrencyInputDisabled: $,
    placeholder: j = "0.00",
    autoFocus: k,
    size: b,
    ...J
  } = i, _ = A(), R = o ?? _, { locale: G } = Pi(), h = Si(Zi), z = parseFloat(t.amount), Q = !s || s.length === 0, K = Y(t, G || "en"), q = ui(() => t.currencyCode ? {
    minimumFractionDigits: u[t.currencyCode].fractionDigits,
    // Always respect currency minimum (e.g., 2 for USD, 0 for JPY)
    maximumFractionDigits: 20,
    // Always use a maximum of 20 decimal places to match API precision
    useGrouping: !0,
    // Keep thousand separators for readability (formatted per locale)
    style: "decimal"
    // Use decimal to avoid currency symbol conflicts
  } : {}, [t.currencyCode, G]), ii = li({ recipe: Mi }), [ti, ni] = ii.splitVariantProps({
    ...J,
    size: b
  }), [oi, ri] = di(ni), M = f(R, "amount"), S = f(R, "currencyCode"), P = f(r, "amount"), C = f(r, "currencyCode"), ci = g(
    (n) => {
      const I = n.toString();
      a?.({
        target: {
          id: M,
          name: P,
          value: I
        }
      });
      const fi = { ...t, amount: I };
      e?.(fi), B?.(I);
    },
    [a, e, B, t, R, r]
  ), si = g(() => {
    const n = {
      target: {
        id: M,
        name: P,
        value: t.amount
      }
    };
    d?.(n);
  }, [d, t.amount, o, r]), ai = g(() => {
    const n = {
      target: {
        id: M,
        name: P,
        value: t.amount
      }
    };
    y?.(n);
  }, [y, t, o, r]), O = g(
    (n) => {
      a?.({
        target: {
          id: S,
          name: C,
          value: n
        }
      });
      const v = {
        ...t,
        currencyCode: n
      };
      e?.(v), N?.(n);
    },
    [a, e, N, t, o, r]
  ), ei = g(() => {
    const n = {
      target: {
        id: S,
        name: C,
        value: t.currencyCode
      }
    };
    d?.(n);
  }, [d, t.currencyCode, o, r]), gi = g(() => {
    const n = {
      target: {
        id: S,
        name: C,
        value: t.currencyCode
      }
    };
    y?.(n);
  }, [y, t.currencyCode, o, r]), Di = g(
    (n) => {
      n && typeof n == "string" && O(n);
    },
    [O]
  ), E = A(), X = A();
  return /* @__PURE__ */ c(Ai, { ...ti, ...oi, ...ri, children: /* @__PURE__ */ U(Li, { children: [
    /* @__PURE__ */ c(Bi, { children: Q ? /* @__PURE__ */ c(
      Ni,
      {
        ...l && { "data-disabled": l },
        asChild: !0,
        children: /* @__PURE__ */ c("label", { id: E, children: t.currencyCode })
      }
    ) : /* @__PURE__ */ c(
      L.Root,
      {
        id: S,
        name: C,
        selectedKey: t.currencyCode || null,
        onSelectionChange: Di,
        onFocus: ei,
        onBlur: gi,
        isDisabled: $ || l || F,
        isClearable: !1,
        placeholder: "",
        "aria-label": h.format("currencySelectLabel"),
        size: b,
        children: /* @__PURE__ */ c(L.Options, { children: s.map((n) => /* @__PURE__ */ c(L.Option, { id: n, children: n }, n)) })
      }
    ) }),
    /* @__PURE__ */ c(
      Fi,
      {
        "data-has-high-precision": T && K,
        asChild: !0,
        children: /* @__PURE__ */ c(
          Ci,
          {
            id: M,
            name: P,
            value: z,
            formatOptions: q,
            onChange: ci,
            onFocus: si,
            onBlur: ai,
            isDisabled: l,
            isReadOnly: F,
            isInvalid: W,
            isRequired: x,
            placeholder: j,
            autoFocus: k,
            size: b,
            "aria-label": h.format("amountInputLabel"),
            "aria-labelledby": E,
            "aria-describedby": X,
            step: void 0
          }
        )
      }
    ),
    T && K && /* @__PURE__ */ c(Ti, { children: /* @__PURE__ */ U(H.Root, { delay: 0, closeDelay: 0, children: [
      /* @__PURE__ */ c(Ii, { children: /* @__PURE__ */ c(
        bi,
        {
          as: mi,
          id: X,
          color: l ? "neutral.8" : "neutral.11",
          "aria-label": h.format("highPrecisionPrice"),
          transform: `translateX(-${pi.spacing[1200]})`
        }
      ) }),
      /* @__PURE__ */ c(H.Content, { placement: "top", children: h.format("highPrecisionPrice") })
    ] }) })
  ] }) });
};
D.getAmountInputId = (i) => f(i, "amount");
D.getCurrencyDropdownId = (i) => f(i, "currencyCode");
D.convertToMoneyValue = V;
D.parseMoneyValue = Oi;
D.isEmpty = w;
D.isHighPrecision = Y;
D.displayName = "MoneyInput";
export {
  D as M
};
//# sourceMappingURL=money-input-BuTBQcdW.es.js.map
