import { jsx as e } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/jsx-runtime/dist/jsxRuntime.module.js";
import { sharedTransactionDetailsEventProperties as P, TX_DATA_CLASS as M, TX_REFUND_RESPONSE as L, TX_REFUND_RESPONSE_ICON as F, TX_REFUND_RESPONSE_SUCCESS_ICON as b, TX_REFUND_RESPONSE_ERROR_ICON as w, REFUND_REASONS as K } from "../../constants.js";
import { memo as D } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/compat/dist/compat.module.js";
import { useState as T, useRef as C, useCallback as y, useEffect as S, useMemo as h } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/hooks/dist/hooks.module.js";
import k from "../../../../../hooks/useComponentTiming.js";
import X from "./PaymentRefundActions.js";
import B from "./PaymentRefundAmount.js";
import V from "./PaymentRefundNotice.js";
import H from "./PaymentRefundReason.js";
import { Alert as Y } from "../../../../internal/Alert/Alert.js";
import x from "../../../../internal/Button/Button.js";
import I from "../../../../internal/Typography/Typography.js";
import { ActiveView as G, RefundMode as m } from "../../types.js";
import { TypographyVariant as g, TypographyElement as W, TypographyModifier as j } from "../../../../internal/Typography/types.js";
import { AlertTypeOption as q, AlertVariantOption as z } from "../../../../internal/Alert/types.js";
import { ButtonVariant as J } from "../../../../internal/Button/types.js";
import Q from "classnames";
import Z from "../../../../../core/Context/analytics/preact/useAnalyticsContext.js";
import U from "../../../../../core/Context/preact/useCoreContext.js";
import { Icon as $ } from "../../../../internal/Icon/Icon.js";
import { clamp as ee } from "../../../../../utils/value/number.js";
const A = ({ disabled: i, refreshTransaction: s, setActiveView: r, setLocked: t, ...l }) => {
  const [o, R] = T(), d = C(!1), c = C(!1), a = y(() => {
    c.current = !0;
  }, []), u = y(() => t(!0), [t]), n = y(() => r(G.DETAILS), [r]), { duration: p } = k(), f = Z();
  return S(() => {
    d.current || (d.current = !0, f.addEvent?.("Switched to refund view", P));
  }, [f]), S(() => {
    i && !o && n(), o === "done" && u();
  }, [i, u, o, n]), S(() => {
    const _ = p;
    return () => {
      _.current !== void 0 && !c.current && f.addEvent?.("Cancelled refund", P);
    };
  }, [p, f]), o ? /* @__PURE__ */ e(A.Result, { result: o, refreshTransaction: s, showDetails: n }) : /* @__PURE__ */ e(A.Form, { ...l, beginRefund: a, setRefundResult: R, showDetails: n });
};
A.Form = D(
  ({
    beginRefund: i,
    currency: s,
    maxAmount: r,
    mode: t,
    refundedAmount: l,
    refundingAmounts: o,
    setRefundResult: R,
    showDetails: d,
    transaction: c
  }) => {
    const [a, u] = T(!1), [n, p] = T(K[0]), [f, _] = T(0), { i18n: E } = U(), O = h(() => {
      switch (t) {
        case m.FULL_AMOUNT:
        case m.PARTIAL_AMOUNT:
        case m.PARTIAL_LINE_ITEMS:
          return r;
        // case RefundMode.PARTIAL_LINE_ITEMS:
        //     return refundingItems.reduce((total, { amount, quantity }) => total + amount * quantity, 0);
        default:
          return 0;
      }
    }, [r, t]), v = h(() => {
      if (r > 0) {
        const N = { amount: E.amount(r, s) };
        switch (t) {
          case m.FULL_AMOUNT:
            return E.get("transactions.details.refund.alerts.refundableAmount", { values: N });
          case m.PARTIAL_AMOUNT:
            return E.get("transactions.details.refund.alerts.refundableMaximum", { values: N });
        }
      }
    }, [E, s, r, t]);
    return /* @__PURE__ */ e("div", { className: M, children: [
      /* @__PURE__ */ e(V, {}),
      /* @__PURE__ */ e(H, { disabled: a, reason: n, onChange: p }),
      /* @__PURE__ */ e(
        B,
        {
          currency: s,
          disabled: a || t !== m.PARTIAL_AMOUNT,
          onChange: (N) => _(ee(0, N, O)),
          value: O
        }
      ),
      v && /* @__PURE__ */ e(Y, { variant: z.TIP, type: q.HIGHLIGHT, children: /* @__PURE__ */ e(I, { className: "adyen-pe-alert__description", el: W.DIV, variant: g.BODY, wide: !0, children: v }) }),
      /* @__PURE__ */ e(
        X,
        {
          beginRefund: i,
          currency: s,
          disabled: a,
          maxAmount: r,
          refundAmount: f,
          refundedAmount: l,
          refundingAmounts: o,
          refundReason: n,
          setRefundInProgress: u,
          setRefundResult: R,
          showDetails: d,
          transactionId: c.id
        }
      )
    ] });
  }
);
A.Result = D(({ result: i, refreshTransaction: s, showDetails: r }) => {
  const { i18n: t } = U(), { isErrorResult: l, iconName: o, titleKey: R, descriptionKey: d } = h(() => {
    let c = "checkmark-circle-fill", a = "transactions.details.refund.alerts.refundSent", u = "transactions.details.refund.alerts.refundSuccess";
    const n = i === "error";
    return n && (c = "cross-circle-fill", a = "common.errors.somethingWentWrong", u = "transactions.details.refund.alerts.refundFailure"), { isErrorResult: n, iconName: c, titleKey: a, descriptionKey: u };
  }, [i]);
  return /* @__PURE__ */ e("div", { className: L, children: [
    /* @__PURE__ */ e(
      $,
      {
        className: Q(F, {
          [w]: l,
          [b]: !l
        }),
        name: o
      }
    ),
    /* @__PURE__ */ e(I, { className: j.MEDIUM, variant: g.TITLE, children: t.get(R) }),
    /* @__PURE__ */ e(I, { variant: g.BODY, children: t.get(d) }),
    /* @__PURE__ */ e(
      x,
      {
        onClick: () => {
          r(), s();
        },
        variant: J.SECONDARY,
        children: t.get("transactions.details.refund.actions.back")
      }
    )
  ] });
});
export {
  A as default
};
