'use strict';
'use client';
import { c } from 'react/compiler-runtime';
import { jsx, jsxs } from 'react/jsx-runtime';
import { useState, useEffect } from 'react';
import { loadStripe } from '@stripe/stripe-js';
import { Elements, useStripe, useElements } from '@stripe/react-stripe-js';
import { useForm } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
import { getStringEnvSetting } from '../../../../fd-toolbox/infra/env-functions.js';
import { envSettingKeys } from '../../../common/infra/env-setting-keys.js';
import { Form } from '../../../fd-component-toolbox/form/Form.js';
import { cn } from '../../../../fd-toolbox/lib/utils.js';
import { isPaymentFormValid } from './checkout-form-validation.js';
import { schema, updateCursorOnFormSubmit, handleFormSubmit, onCardFieldChange } from './CheckoutForm.funcs.js';
import { getLoginState } from '../../../../fd-toolbox/auth/internal-login-states.js';
import { useTheme } from '../../../fd-app-toolbox/providers/ThemeProvider/ThemeProvider.js';
import { EmailField, CardInformationField, NameField, CountryAndZipFields } from './checkout-form-fields.js';
import { CheckoutFormSubmit } from './CheckoutFormSubmit.js';

var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", {
  value,
  configurable: true
});
const stripePublicKey = getStringEnvSetting(envSettingKeys.stripePublicKey);
const stripePromise = stripePublicKey && loadStripe(stripePublicKey);
function CheckoutForm({
  price,
  pricingId,
  onSubmitting,
  onSubmitSuccess,
  submitHidden,
  setIsButtonEnabled,
  rerouteAfterSuccess,
  redirectUrl,
  setIsSubmitting
}) {
  return stripePromise && /* @__PURE__ */jsx(Elements, {
    stripe: stripePromise,
    children: /* @__PURE__ */jsx(CheckoutFormInner, {
      price,
      pricingId,
      onSubmitting,
      onSubmitSuccess,
      submitHidden,
      setIsButtonEnabled,
      rerouteAfterSuccess,
      redirectUrl,
      setIsSubmitting
    })
  });
}
__name(CheckoutForm, "CheckoutForm");
function CheckoutFormInner(t0) {
  const $ = c(36);
  const {
    price,
    pricingId,
    onSubmitting,
    onSubmitSuccess,
    submitHidden,
    setIsButtonEnabled,
    rerouteAfterSuccess,
    redirectUrl,
    setIsSubmitting
  } = t0;
  const {
    appliedTheme
  } = useTheme();
  const [cardFieldValid, setCardFieldValid] = useState(false);
  const [cardFieldErrorShown, setCardFieldErrorShown] = useState(false);
  let t1;
  if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
    t1 = {
      defaultValues: {
        name: "",
        email: getLoginState().email ?? "",
        country: "",
        zip: ""
      },
      resolver: zodResolver(schema),
      mode: "onTouched"
    };
    $[0] = t1;
  } else {
    t1 = $[0];
  }
  const form = useForm(t1);
  let t2;
  let t3;
  if ($[1] !== form.formState.isSubmitting || $[2] !== onSubmitting) {
    t2 = () => {
      updateCursorOnFormSubmit(form.formState.isSubmitting, onSubmitting);
    };
    t3 = [form.formState.isSubmitting, onSubmitting];
    $[1] = form.formState.isSubmitting;
    $[2] = onSubmitting;
    $[3] = t2;
    $[4] = t3;
  } else {
    t2 = $[3];
    t3 = $[4];
  }
  useEffect(t2, t3);
  const stripe = useStripe() ?? void 0;
  const elements = useElements() ?? void 0;
  let t4;
  let t5;
  if ($[5] !== cardFieldValid || $[6] !== form.formState.isSubmitting || $[7] !== form.formState.isValid || $[8] !== setIsButtonEnabled || $[9] !== setIsSubmitting || $[10] !== stripe) {
    t4 = () => {
      if (setIsButtonEnabled) {
        if (!isPaymentFormValid(form.formState.isValid, cardFieldValid, form.formState.isSubmitting, !!stripe)) {
          setIsButtonEnabled(false);
          return;
        }
        if (stripe) {
          setIsSubmitting?.(false);
        }
        setIsButtonEnabled(true);
      }
    };
    t5 = [form.formState.isValid, form.formState.isSubmitting, stripe, setIsButtonEnabled, cardFieldValid, setIsSubmitting];
    $[5] = cardFieldValid;
    $[6] = form.formState.isSubmitting;
    $[7] = form.formState.isValid;
    $[8] = setIsButtonEnabled;
    $[9] = setIsSubmitting;
    $[10] = stripe;
    $[11] = t4;
    $[12] = t5;
  } else {
    t4 = $[11];
    t5 = $[12];
  }
  useEffect(t4, t5);
  let t6;
  if ($[13] !== appliedTheme || $[14] !== cardFieldErrorShown || $[15] !== cardFieldValid || $[16] !== elements || $[17] !== form || $[18] !== onSubmitSuccess || $[19] !== price || $[20] !== pricingId || $[21] !== redirectUrl || $[22] !== rerouteAfterSuccess || $[23] !== stripe || $[24] !== submitHidden) {
    let t7;
    if ($[26] !== elements || $[27] !== onSubmitSuccess || $[28] !== pricingId || $[29] !== redirectUrl || $[30] !== rerouteAfterSuccess || $[31] !== stripe) {
      t7 = data => handleFormSubmit(data, onSubmitSuccess, pricingId, elements, stripe, rerouteAfterSuccess, redirectUrl);
      $[26] = elements;
      $[27] = onSubmitSuccess;
      $[28] = pricingId;
      $[29] = redirectUrl;
      $[30] = rerouteAfterSuccess;
      $[31] = stripe;
      $[32] = t7;
    } else {
      t7 = $[32];
    }
    let t8;
    if ($[33] === Symbol.for("react.memo_cache_sentinel")) {
      t8 = event => onCardFieldChange(event, setCardFieldValid, setCardFieldErrorShown);
      $[33] = t8;
    } else {
      t8 = $[33];
    }
    let t9;
    if ($[34] !== form) {
      t9 = (value, fieldName) => form.setValue(fieldName, value, {
        shouldValidate: true
      });
      $[34] = form;
      $[35] = t9;
    } else {
      t9 = $[35];
    }
    t6 = jsx(Form, {
      ...form,
      children: jsxs("form", {
        onSubmit: form.handleSubmit(t7),
        className: cn("flex flex-col justify-center gap-4 py-10", "w-full", form.formState.isSubmitting && "pointer-events-none"),
        children: [jsx(EmailField, {
          control: form.control
        }), jsx(CardInformationField, {
          appliedTheme,
          onCardFieldChange: t8,
          cardFieldErrorShown
        }), jsx(NameField, {
          control: form.control
        }), jsx(CountryAndZipFields, {
          control: form.control,
          handleCountryChange: t9
        }), jsx(CheckoutFormSubmit, {
          price,
          submitting: form.formState.isSubmitting,
          submitHidden,
          valid: form.formState.isValid,
          cardFieldValid,
          stripe
        })]
      })
    });
    $[13] = appliedTheme;
    $[14] = cardFieldErrorShown;
    $[15] = cardFieldValid;
    $[16] = elements;
    $[17] = form;
    $[18] = onSubmitSuccess;
    $[19] = price;
    $[20] = pricingId;
    $[21] = redirectUrl;
    $[22] = rerouteAfterSuccess;
    $[23] = stripe;
    $[24] = submitHidden;
    $[25] = t6;
  } else {
    t6 = $[25];
  }
  return t6;
}
__name(CheckoutFormInner, "CheckoutFormInner");
CheckoutForm.displayName = "CheckoutForm";

export { CheckoutForm, CheckoutForm as default };
