import { jsx as e } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/jsx-runtime/dist/jsxRuntime.module.js";
import { TX_DATA_CONTAINER as F, TX_DATA_INPUT_HEAD as M, TX_DATA_INPUT as $, TX_DATA_INPUT_CONTAINER_WITH_ERROR as B, TX_DATA_INPUT_CONTAINER_TEXT as H, TX_DATA_INPUT_CONTAINER_SHORT as L, TX_DATA_INPUT_CONTAINER as V } from "../../constants.js";
import Y from "classnames";
import { memo as j } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/compat/dist/compat.module.js";
import { Tag as k } from "../../../../internal/Tag/Tag.js";
import { TagVariant as W } from "../../../../internal/Tag/types.js";
import { ARIA_ERROR_SUFFIX as w } from "../../../../../core/Errors/constants.js";
import { getDecimalAmount as z, getDivider as G } from "../../../../../core/Localization/amount/amount-util.js";
import { TypographyVariant as R, TypographyElement as h } from "../../../../internal/Typography/types.js";
import { useState as E, useMemo as D, useRef as P, useCallback as J, useEffect as K } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/hooks/dist/hooks.module.js";
import b from "../../../../internal/Typography/Typography.js";
import Q from "../../../../internal/FormFields/InputBase.js";
import O from "../../../../../hooks/useUniqueId.js";
import Z from "../../../../../core/Context/preact/useCoreContext.js";
import { Icon as ee } from "../../../../internal/Icon/Icon.js";
const Ae = j(({ currency: n, disabled: d, onChange: g, value: x }) => {
  const { i18n: t } = Z(), f = O(), I = O(), i = Math.log10(G(n)), r = parseInt(`${x}`, 10), p = z(r, n).toFixed(i), [U, X] = E(p), [u, y] = E(), C = D(() => {
    const a = { amount: t.amount(r, n) };
    return {
      excess: t.get("transactions.details.refund.inputs.amount.errors.excess", { values: a }),
      negative: t.get("transactions.details.refund.inputs.amount.errors.negative"),
      required: t.get("transactions.details.refund.inputs.amount.errors.required")
    };
  }, [t, n, r]), T = u && C[u], S = D(() => t.get("transactions.details.refund.inputs.amount.label"), [t]), s = P(null), A = J(
    (a) => {
      let l, o = a.value.trim();
      const m = Math.trunc(+`${parseFloat(o)}e${i}`) || 0;
      m || o ? (m < 0 && (l = "negative"), m > r && (l = "excess")) : l = "required";
      const v = 1.1.toLocaleString(t.locale).match(/\d(.*?)\d/)?.[1] || ".", _ = o.split(v);
      if (_.length === 2) {
        const q = _[0];
        let c = _[1];
        c.length >= i && (c = c.substring(0, i), o = q + v + c, a.value = o);
      }
      X(o), y(l), g?.(l ? 0 : m);
    },
    [i, r, g, t.locale]
  ), N = P();
  return K(() => {
    if (N.current !== r)
      N.current = r;
    else if (d && u && s.current)
      s.current.value = p;
    else return;
    s.current && A(s.current);
  }, [A, d, p, r, u]), /* @__PURE__ */ e("div", { className: F, children: [
    /* @__PURE__ */ e("div", { className: M, children: /* @__PURE__ */ e("div", { id: I, children: /* @__PURE__ */ e(b, { el: h.SPAN, variant: R.BODY, stronger: !0, children: S }) }) }),
    /* @__PURE__ */ e(
      "div",
      {
        className: Y({
          [V]: !0,
          [L]: !0,
          [H]: !0,
          [B]: T
        }),
        children: [
          /* @__PURE__ */ e("label", { htmlFor: f, "aria-labelledby": I, children: [
            n && /* @__PURE__ */ e(k, { label: n, variant: W.DEFAULT }),
            /* @__PURE__ */ e(
              Q,
              {
                min: 0,
                type: "number",
                className: $,
                disabled: d,
                lang: t.locale,
                onInput: (a) => A(a.currentTarget),
                value: U,
                uniqueId: f,
                ref: s
              }
            )
          ] }),
          T && /* @__PURE__ */ e("div", { className: "adyen-pe-input__refund-invalid-value", id: `${f}${w}`, children: [
            /* @__PURE__ */ e(ee, { name: "cross-circle-fill" }),
            /* @__PURE__ */ e(b, { el: h.SPAN, variant: R.BODY, children: T })
          ] })
        ]
      }
    )
  ] });
});
export {
  Ae as default
};
