import { jsx as t } from "../../../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/jsx-runtime/dist/jsxRuntime.module.js";
import { useMemo as s, useCallback as p, useEffect as A } from "../../../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/hooks/dist/hooks.module.js";
import { useWizardFormContext as E } from "../../../../../../../hooks/form/wizard/WizardFormContext.js";
import { CurrencyInput as I } from "../../../../../../internal/FormFields/CurrencyInput/CurrencyInput.js";
import { VisibleField as V } from "../../../../../../internal/FormWrappers/VisibleField.js";
import x from "../../../../../../internal/FormWrappers/FormField.js";
import { FieldError as L } from "../../../../../../internal/FormFields/FieldError/FieldError.js";
import M from "../../../../../../../core/Context/preact/useCoreContext.js";
import { Controller as R } from "../../../../../../../hooks/form/Controller.js";
import { Fragment as k } from "../../../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/dist/preact.module.js";
const l = "amount.value", n = "amount.currency", q = 1e13, Y = () => {
  const { i18n: a } = M(), { control: g, setValue: i, getValues: u, fieldsConfig: r, trigger: f } = E(), c = s(() => r?.[n]?.visible ?? !1, [r]), C = p(
    (e) => Number(e) < 0 ? { valid: !1, message: a.get("payByLink.creation.fields.amountValue.error.negativeNumber") } : c && !u(n) ? { valid: !1, message: a.get("payByLink.creation.fields.amountValue.error.currency") } : !e || Number(e) === 0 ? { valid: !1, message: a.get("common.errors.fieldRequired") } : { valid: !0 },
    [a, u, c]
  ), v = p(
    (e, o) => {
      i(n, e), o && f(l);
    },
    [i, f]
  ), b = s(() => r?.amount?.required, [r]), h = s(() => r?.["amount.value"]?.readOnly, [r]), F = s(() => r?.["amount.currency"]?.readOnly, [r]), m = s(() => r?.[n]?.options?.map((o) => ({ id: o, name: o })), [r]);
  return A(() => {
    m?.length === 1 && i(n, m[0]?.id);
  }, [m, i]), /* @__PURE__ */ t(V, { name: l, children: /* @__PURE__ */ t(
    x,
    {
      label: a.get("payByLink.creation.fields.amount.label"),
      optional: !1,
      supportText: void 0,
      className: void 0,
      testId: "form-field-amount.value",
      children: /* @__PURE__ */ t(
        R,
        {
          name: l,
          control: g,
          rules: {
            validate: C,
            required: b
          },
          render: ({ field: e, fieldState: o }) => {
            const d = !!o.error && o.isTouched, y = o.error?.message;
            return /* @__PURE__ */ t(k, { children: [
              /* @__PURE__ */ t(
                I,
                {
                  ...e,
                  hideCurrencySelector: !c,
                  selectedCurrencyCode: u(n),
                  onCurrencyChange: (N) => v(N, d),
                  currency: u(n),
                  currencyItems: m,
                  isInvalid: d,
                  name: l,
                  amount: e.value ? Number(e.value) : void 0,
                  onAmountChange: e.onInput,
                  maxValue: q,
                  readonly: { amount: h, currency: F }
                }
              ),
              d && y && /* @__PURE__ */ t(L, { errorMessage: y, testId: "field-error-amount.value" })
            ] });
          }
        }
      )
    }
  ) });
};
export {
  Y as AmountField
};
