import { jsx as n } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/jsx-runtime/dist/jsxRuntime.module.js";
/* empty css                        */
import { useRef as U, useState as s, useEffect as p, useCallback as o, useMemo as H } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/hooks/dist/hooks.module.js";
import J from "../../../../internal/FormFields/InputText.js";
import h from "../../../../internal/Typography/Typography.js";
import { TypographyVariant as g, TypographyElement as V } from "../../../../internal/Typography/types.js";
import { AlertVariantOption as Q, AlertTypeOption as X } from "../../../../internal/Alert/types.js";
import { Alert as Z } from "../../../../internal/Alert/Alert.js";
import $ from "../PaymentLinkSettingsContainer/context/context.js";
import { isTermsAndConditionsData as ee } from "../PaymentLinkThemeContainer/types.js";
import { isValidURL as k } from "../PaymentLinkSettingsContainer/utils/validateTermsAndConditionsURL.js";
import { ButtonVariant as ne } from "../../../../internal/Button/types.js";
import { Requirements as te } from "./Requirements/Requirements.js";
import se from "classnames";
import re from "../../../../../core/Context/preact/useCoreContext.js";
import ie from "../../../../internal/Button/Button.js";
import { Translation as ae } from "../../../../internal/Translation/Translation.js";
import { Icon as w } from "../../../../internal/Icon/Icon.js";
import { Checkbox as oe } from "../../../../internal/Checkbox/Checkbox.js";
import ce from "../../../../../utils/random/uniqueId.js";
const xe = ({ data: D, initialData: v }) => {
  const { i18n: r } = re(), u = U(ce()), [t, E] = s(D?.termsOfServiceUrl ?? ""), [a, c] = s(void 0), [C, _] = s(!1), [I, A] = s(!1), [P, T] = s(!1), [Y, b] = s(!1), l = U(!1), {
    savedData: d,
    setPayload: m,
    saveActionCalled: N,
    setIsValid: f,
    isSaving: y,
    setSaveActionCalled: R,
    setIsSaveError: q,
    setIsSaveSuccess: S,
    isShowingRequirements: x,
    embeddedInOverview: W,
    setIsShowingRequirements: i
  } = $();
  p(() => {
    a && k(t) ? f(!0) : f(!1);
  }, [a, t, f, m]), p(() => {
    l.current = !1;
  }, [d]), p(() => {
    N && (A(!!(t && !k(t))), _(!a), R(!1), S(!1), q(!1));
  }, [t, a, N, R, m, q, S]), p(() => {
    let e = ee(d) ? d : void 0;
    e || (e = v);
    const z = !e || !e.termsOfServiceUrl;
    e && (e?.termsOfServiceUrl === t || !e.termsOfServiceUrl && t === "") ? (z || (b(!0), c(!0)), T(!1)) : (b(!1), T(!0), c(l.current));
  }, [t, d, v]);
  const G = o(
    (e) => {
      e.preventDefault(), A(!1), E(e?.currentTarget?.value), k(e?.currentTarget?.value) && m({ termsOfServiceUrl: e?.currentTarget?.value });
    },
    [m]
  ), [L, O] = s(!1), M = o(
    (e) => {
      e.preventDefault(), e.currentTarget?.checked && _(!1), !l.current && e.currentTarget?.checked && !L && (i(!0), O(!0)), l.current = e.currentTarget?.checked, c(e.currentTarget?.checked);
    },
    [L, i]
  ), B = o(() => {
    i(!0), O(!0);
  }, [i]), j = H(() => /* @__PURE__ */ n(
    ae,
    {
      translationKey: "payByLink.settings.termsAndConditions.requirement.checkbox.part1",
      fills: {
        requirements: /* @__PURE__ */ n(
          ie,
          {
            className: "adyen-pe-payment-link-settings-terms-and-conditions__requirements-link",
            variant: ne.TERTIARY,
            onClick: B,
            children: r.get("payByLink.settings.termsAndConditions.requirement.checkbox.part2")
          }
        )
      }
    }
  ), [r, B]), F = o(() => i(!1), [i]), K = o(() => c(!0), []);
  return /* @__PURE__ */ n("section", { className: "adyen-pe-payment-link-settings-terms-and-conditions", children: [
    /* @__PURE__ */ n(
      "div",
      {
        className: se("adyen-pe-payment-link-settings-terms-and-conditions__content", {
          "adyen-pe-payment-link-settings-terms-and-conditions__content--hidden": x
        }),
        children: [
          /* @__PURE__ */ n("div", { className: "adyen-pe-payment-link-settings__input-container", children: [
            /* @__PURE__ */ n(
              "label",
              {
                htmlFor: u.current,
                "aria-labelledby": u.current,
                className: "adyen-pe-payment-link-settings-terms-and-conditions-input__label",
                children: /* @__PURE__ */ n(
                  h,
                  {
                    variant: g.BODY,
                    stronger: !0,
                    className: "adyen-pe-payment-link-settings-terms-and-conditions-input__label--info-text",
                    children: r.get("payByLink.settings.termsAndConditions.urlInput.label")
                  }
                )
              }
            ),
            /* @__PURE__ */ n(
              J,
              {
                disabled: !!y,
                readonly: !!y,
                uniqueId: u.current,
                value: t,
                onInput: G,
                maxLength: 2e3,
                isInvalid: I
              }
            ),
            I && /* @__PURE__ */ n("div", { className: "adyen-pe-payment-link-settings-terms-and-conditions__error", children: [
              /* @__PURE__ */ n(w, { name: "cross-circle-fill", className: "adyen-pe-payment-link-settings-terms-and-conditions__error-icon" }),
              /* @__PURE__ */ n(
                h,
                {
                  className: "adyen-pe-payment-link-settings-terms-and-conditions__error-text",
                  el: V.SPAN,
                  variant: g.BODY,
                  children: r.get("payByLink.settings.termsAndConditions.error.urlValidation")
                }
              )
            ] })
          ] }),
          P && /* @__PURE__ */ n(
            Z,
            {
              type: X.WARNING,
              variant: Q.TIP,
              description: r.get("payByLink.settings.termsAndConditions.alert.urlChange"),
              className: "adyen-pe-payment-link-settings-terms-and-conditions__alert"
            }
          ),
          /* @__PURE__ */ n("div", { className: "adyen-pe-payment-link-settings-terms-and-conditions-checkbox__container", children: [
            /* @__PURE__ */ n(
              oe,
              {
                checked: a,
                disabled: Y || y,
                className: "adyen-pe-payment-link-settings-terms-and-conditions-checkbox",
                label: j,
                onInput: M,
                error: C
              }
            ),
            C && /* @__PURE__ */ n("div", { className: "adyen-pe-payment-link-settings-terms-and-conditions__error", children: [
              /* @__PURE__ */ n(w, { name: "cross-circle-fill", className: "adyen-pe-payment-link-settings-terms-and-conditions__error-icon" }),
              /* @__PURE__ */ n(
                h,
                {
                  className: "adyen-pe-payment-link-settings-terms-and-conditions__error-text",
                  el: V.SPAN,
                  variant: g.BODY,
                  children: r.get("payByLink.settings.termsAndConditions.error.requirementsNotChecked")
                }
              )
            ] })
          ] })
        ]
      }
    ),
    x && /* @__PURE__ */ n(
      te,
      {
        onGoBack: F,
        termsAndConditionsURL: t,
        acceptRequirements: K,
        embeddedInOverview: W
      }
    )
  ] });
};
export {
  xe as TermsAndConditions
};
