import { jsx as n } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/jsx-runtime/dist/jsxRuntime.module.js";
import G from "classnames";
import "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/compat/dist/compat.module.js";
import { useState as O, useMemo as i, useEffect as T, useCallback as d } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/hooks/dist/hooks.module.js";
import { useFetch as Y } from "../../../../../hooks/useFetch.js";
import { useResponsiveContainer as j, containerQueries as Q } from "../../../../../hooks/useResponsiveContainer.js";
import { Alert as $ } from "../../../../internal/Alert/Alert.js";
import { AlertVariantOption as z, AlertTypeOption as J } from "../../../../internal/Alert/types.js";
import U from "../../../../internal/Button/ButtonActions/ButtonActions.js";
import { TypographyVariant as h, TypographyElement as v } from "../../../../internal/Typography/types.js";
import y from "../../../../internal/Typography/Typography.js";
import { useDisputeFlow as K } from "../../context/dispute/context.js";
import { getDefenseReasonContent as B } from "../../utils/index.js";
import W from "../../../../../core/Context/preact/useCoreContext.js";
import X from "../../../../internal/FormFields/Select/Select.js";
import { Fragment as Z } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/dist/preact.module.js";
import { useConfigContext as ee } from "../../../../../core/ConfigContext/preact/context.js";
import { EMPTY_OBJECT as te } from "../../../../../utils/value/constants.js";
const R = "adyen-pe-defend-dispute-reason", D = {
  selector: R + "__selector",
  description: R + "__description",
  dropdownList: R + "__dropdown-list",
  dropdownListMobile: R + "__dropdown-list--mobile"
}, _e = () => {
  const { i18n: t } = W(), {
    applicableDocuments: c,
    dispute: l,
    goBack: I,
    setFlowState: p,
    setSelectedDefenseReason: m,
    selectedDefenseReason: o,
    setApplicableDocuments: f,
    defenseReasonConfig: u
  } = K(), _ = l?.dispute?.allowedDefenseReasons, S = l?.dispute?.pspReference, [a, g] = O(!1), P = j(Q.down.xs), r = i(
    () => Object.freeze(
      _?.map((e) => ({
        id: e,
        disabled: _.length === 1,
        name: B(u, t, e)?.title ?? e
      }))
    ) ?? [],
    [t, _, u]
  ), s = i(
    () => o ? r.find((e) => e.id === o)?.id : r?.[0]?.id ?? null,
    [o, r]
  );
  T(() => {
    s && m(s);
  }, [s, a, m]);
  const { getApplicableDefenseDocuments: F } = ee().endpoints, k = d(async () => F?.(te, {
    query: {
      defenseReason: o
    },
    path: {
      disputePspReference: S
    }
  }), [o, S, F]), { error: E, isFetching: b } = Y({
    queryFn: k,
    fetchOptions: {
      enabled: a,
      onSuccess: d(
        (e) => {
          g(!1), f(e?.data ?? null), e?.data && e?.data.length > 0 && p("uploadDefenseFilesView");
        },
        [f, g, p]
      )
    }
  });
  T(() => {
    g(!1);
  }, [E]);
  const N = d(() => {
    if (c?.length) return p("uploadDefenseFilesView");
    g(!0);
  }, [c, p]), L = d(
    (e) => {
      o !== e.target.value && c?.length && f([]), e?.target?.value && m(e.target.value);
    },
    [c, o, f, m]
  ), M = i(() => [
    {
      title: t.get("disputes.management.defend.common.actions.continue"),
      disabled: a || b,
      event: N
    },
    {
      title: t.get("disputes.management.common.actions.goBack"),
      disabled: a || b,
      event: I
    }
  ], [b, a, t, I, N]), A = i(() => l?.dispute.type === "REQUEST_FOR_INFORMATION", [l?.dispute.type]), [x, q] = O(!A), H = d(() => {
    q(!1);
  }, []), C = i(
    () => s ? B(u, t, s) : void 0,
    [u, t, s]
  ), V = i(
    () => A ? t.get("disputes.management.defend.requestForInformation.selectDefenseReason") : t.get("disputes.management.defend.chargeback.selectDefenseReason"),
    [t, A]
  );
  return !r || !s ? null : /* @__PURE__ */ n(Z, { children: [
    /* @__PURE__ */ n("div", { className: D.selector, children: [
      /* @__PURE__ */ n(y, { className: "adyen-pe-defend-dispute__reason-description", variant: h.BODY, children: V }),
      /* @__PURE__ */ n(
        X,
        {
          items: r,
          onChange: L,
          selected: s,
          "aria-label": t.get("disputes.management.defend.common.inputs.reasonSelect.a11y.label"),
          popoverClassNameModifiers: [G(D.dropdownList, { [D.dropdownListMobile]: P })]
        }
      ),
      C?.primaryDescriptionItems?.map((e, w) => /* @__PURE__ */ n(
        y,
        {
          el: v.PARAGRAPH,
          className: D.description,
          variant: h.BODY,
          children: e
        },
        `description-${w}`
      )),
      C?.secondaryDescriptionItems?.length && /* @__PURE__ */ n("ul", { className: "adyen-pe-defend-dispute-reason__secondary-description-items-container", children: C.secondaryDescriptionItems.map((e, w) => /* @__PURE__ */ n("li", { className: "adyen-pe-defend-dispute-reason__secondary-description-item", children: /* @__PURE__ */ n(
        y,
        {
          el: v.PARAGRAPH,
          className: "adyen-pe-defend-dispute-reason__description",
          variant: h.BODY,
          children: e
        }
      ) }, `description-item-${w}`)) })
    ] }),
    x && /* @__PURE__ */ n($, { onClose: H, type: J.HIGHLIGHT, variant: z.TIP, closeButton: !0, children: /* @__PURE__ */ n(y, { className: "adyen-pe-alert__description", el: v.DIV, variant: h.CAPTION, wide: !0, children: t.get("disputes.management.defend.chargeback.feeInfo") }) }),
    /* @__PURE__ */ n("div", { className: "adyen-pe-defend-dispute__actions", children: /* @__PURE__ */ n(U, { actions: M }) })
  ] });
};
export {
  _e as DefendDisputeReason
};
