import { jsx as b } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/jsx-runtime/dist/jsxRuntime.module.js";
import x from "classnames";
import { useMemo as e, useEffect as P } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/hooks/dist/hooks.module.js";
import { ButtonVariant as A } from "../../../../internal/Button/types.js";
import { ButtonActionsLayoutBasic as O } from "../../../../internal/Button/ButtonActions/types.js";
import { TX_DATA_CONTAINER as F, TX_DATA_ACTION_BAR as M, sharedTransactionDetailsEventProperties as W } from "../../constants.js";
import j from "../../../../internal/Button/ButtonActions/ButtonActions.js";
import k from "../../../../../hooks/useMutation/useMutation.js";
import I from "../../../../../core/Context/preact/useCoreContext.js";
import S from "../../../../../core/Context/analytics/preact/useAnalyticsContext.js";
import { useConfigContext as X } from "../../../../../core/ConfigContext/preact/context.js";
const Z = ({
  beginRefund: T,
  currency: a,
  disabled: s,
  maxAmount: i,
  refundAmount: n,
  refundedAmount: g,
  refundingAmounts: v,
  refundReason: r,
  setRefundInProgress: c,
  setRefundResult: l,
  showDetails: B,
  transactionId: h
}) => {
  const { initiateRefund: C } = X().endpoints, { isLoading: o, mutate: E } = k({ queryFn: C }), { i18n: t } = I(), u = n > 0 && n <= i, L = g === 0 && v.length === 0 && n === i, d = s || o || !u, N = e(() => t.get("transactions.details.refund.actions.back"), [t]), m = e(
    () => ({
      idle: t.get("transactions.details.refund.actions.refund.labels.payment"),
      inProgress: `${t.get("transactions.details.refund.actions.refund.labels.inProgress")}..`
    }),
    [t]
  ), f = e(() => {
    const y = { amount: t.amount(n, a) };
    return t.get("transactions.details.refund.actions.refund.labels.amount", { values: y });
  }, [t, a, n]), p = o ? m.inProgress : u ? f : m.idle, D = S(), R = {
    disabled: d,
    event: async () => {
      if (!d) {
        T(), D.addEvent?.("Completed refund", {
          ...W,
          isFullRefund: L,
          refundReason: r
        });
        try {
          await E({ contentType: "application/json", body: { amount: { currency: a, value: n }, refundReason: r } }, { path: { transactionId: h } }), l("done");
        } catch {
          l("error");
        }
      }
    },
    state: o ? "loading" : void 0,
    title: p,
    variant: A.PRIMARY,
    ariaLabel: o ? f : p
  }, _ = {
    disabled: s,
    event: B,
    title: N,
    variant: A.SECONDARY
  };
  return P(() => c(o), [o, c]), /* @__PURE__ */ b("div", { className: x(F, M), children: /* @__PURE__ */ b(j, { actions: [R, _], layout: O.BUTTONS_END }) });
};
export {
  Z as default
};
