import { useMemo as m } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/hooks/dist/hooks.module.js";
import { RefundMode as f, RefundedState as R } from "../../types.js";
import { REFUND_STATUSES as S } from "../../constants.js";
import { boolOrFalse as U } from "../../../../../utils/value/bool.js";
import { isFunction as _ } from "../../../../../utils/value/is.js";
import { useConfigContext as h } from "../../../../../core/ConfigContext/preact/context.js";
const y = (s) => {
  const u = s?.refundDetails, l = u?.refundMode ?? f.FULL_AMOUNT, N = U(u?.refundLocked), F = l !== f.NON_REFUNDABLE, e = m(() => s ? Math.max(0, u?.refundableAmount?.value ?? 0) : 0, [u, s]), L = _(h().endpoints.initiateRefund), M = L && F && e > 0, p = u?.refundableAmount?.currency ?? s?.netAmount.currency ?? "", E = !M || N, r = m(() => {
    let d = -1;
    return (u?.refundStatuses ?? []).reduceRight(
      (t, { amount: o, status: n }, a) => {
        if (o.value !== 0 && S.includes(n)) {
          const i = n !== "failed", c = a > d;
          if (i && c && (d = a), i || c) {
            const g = (t[n] ?? []).concat(Math.abs(o.value));
            return { ...t, [n]: g };
          }
        }
        return t;
      },
      {}
    );
  }, [u?.refundStatuses]), { fullRefundFailed: I, fullRefundInProgress: T, refundedAmount: A } = m(() => {
    let d = !1, t = !1;
    const o = (r.completed ?? []).reduce((i, c) => i + c, 0), n = r.in_progress ?? [], a = r.failed ?? [];
    return o === 0 && (d = n.length === 0 && a.slice(-1)[0] === e, t = n.length === 1 && n[0] === e), { fullRefundFailed: d, fullRefundInProgress: t, refundedAmount: o };
  }, [e, r]), b = m(() => {
    if (A > 0)
      switch (l) {
        case f.NON_REFUNDABLE:
          if (e === 0) return R.FULL;
          break;
        case f.PARTIAL_AMOUNT:
        case f.PARTIAL_LINE_ITEMS:
          if (e > 0) return R.PARTIAL;
          break;
      }
    return R.INDETERMINATE;
  }, [e, A, l]);
  return {
    fullRefundFailed: I,
    // whether the last (and only) refund that failed is the full refundable amount
    fullRefundInProgress: T,
    // whether the only refund in progress is the full refundable amount
    refundableAmount: e,
    // the maximum amount still available for refund
    refundable: F,
    // whether the refund mode of the payment allows for refund
    refundAvailable: M,
    // whether a refund can be initiated for the payment
    refundAuthorization: L,
    // whether the authenticated user has sufficient permission to initiate refunds
    refundCurrency: p,
    // the payment currency for any initiated refund
    refundDisabled: E,
    // whether refund action for the payment is disabled (refund view should be prevented)
    refundAmounts: r,
    // lookup of refund amounts by refund statuses
    refundedAmount: A,
    // the total amount already refunded
    refundedState: b,
    // whether the payment is yet to be, partially or fully refunded
    refundLocked: N,
    // whether refund action for the payment is temporarily locked
    refundMode: l
    // the refund mode of the payment
  };
};
export {
  y as default,
  y as useRefundMetadata
};
