import { jsxs as H, jsx as P } from "react/jsx-runtime";
import v, { useState as I, useEffect as ee } from "react";
import { aF as be, aI as he, o as te, a as Ee, B as G, W as _e } from "./index-C62tPk5p.js";
var Pe = /* @__PURE__ */ he();
const C = /* @__PURE__ */ be(Pe);
function Y(t, e) {
  var n = Object.keys(t);
  if (Object.getOwnPropertySymbols) {
    var r = Object.getOwnPropertySymbols(t);
    e && (r = r.filter(function(f) {
      return Object.getOwnPropertyDescriptor(t, f).enumerable;
    })), n.push.apply(n, r);
  }
  return n;
}
function V(t) {
  for (var e = 1; e < arguments.length; e++) {
    var n = arguments[e] != null ? arguments[e] : {};
    e % 2 ? Y(Object(n), !0).forEach(function(r) {
      ne(t, r, n[r]);
    }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(t, Object.getOwnPropertyDescriptors(n)) : Y(Object(n)).forEach(function(r) {
      Object.defineProperty(t, r, Object.getOwnPropertyDescriptor(n, r));
    });
  }
  return t;
}
function R(t) {
  "@babel/helpers - typeof";
  return typeof Symbol == "function" && typeof Symbol.iterator == "symbol" ? R = function(e) {
    return typeof e;
  } : R = function(e) {
    return e && typeof Symbol == "function" && e.constructor === Symbol && e !== Symbol.prototype ? "symbol" : typeof e;
  }, R(t);
}
function ne(t, e, n) {
  return e in t ? Object.defineProperty(t, e, {
    value: n,
    enumerable: !0,
    configurable: !0,
    writable: !0
  }) : t[e] = n, t;
}
function Ce(t, e) {
  return Oe(t) || we(t, e) || je(t, e) || xe();
}
function Oe(t) {
  if (Array.isArray(t)) return t;
}
function we(t, e) {
  var n = t && (typeof Symbol < "u" && t[Symbol.iterator] || t["@@iterator"]);
  if (n != null) {
    var r = [], f = !0, i = !1, m, c;
    try {
      for (n = n.call(t); !(f = (m = n.next()).done) && (r.push(m.value), !(e && r.length === e)); f = !0)
        ;
    } catch (o) {
      i = !0, c = o;
    } finally {
      try {
        !f && n.return != null && n.return();
      } finally {
        if (i) throw c;
      }
    }
    return r;
  }
}
function je(t, e) {
  if (t) {
    if (typeof t == "string") return X(t, e);
    var n = Object.prototype.toString.call(t).slice(8, -1);
    if (n === "Object" && t.constructor && (n = t.constructor.name), n === "Map" || n === "Set") return Array.from(t);
    if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return X(t, e);
  }
}
function X(t, e) {
  (e == null || e > t.length) && (e = t.length);
  for (var n = 0, r = new Array(e); n < e; n++) r[n] = t[n];
  return r;
}
function xe() {
  throw new TypeError(`Invalid attempt to destructure non-iterable instance.
In order to be iterable, non-array objects must have a [Symbol.iterator]() method.`);
}
var $ = function(e) {
  var n = v.useRef(e);
  return v.useEffect(function() {
    n.current = e;
  }, [e]), n.current;
}, A = function(e) {
  return e !== null && R(e) === "object";
}, Ae = function(e) {
  return A(e) && typeof e.then == "function";
}, Le = function(e) {
  return A(e) && typeof e.elements == "function" && typeof e.createToken == "function" && typeof e.createPaymentMethod == "function" && typeof e.confirmCardPayment == "function";
}, z = "[object Object]", Re = function t(e, n) {
  if (!A(e) || !A(n))
    return e === n;
  var r = Array.isArray(e), f = Array.isArray(n);
  if (r !== f) return !1;
  var i = Object.prototype.toString.call(e) === z, m = Object.prototype.toString.call(n) === z;
  if (i !== m) return !1;
  if (!i && !r) return e === n;
  var c = Object.keys(e), o = Object.keys(n);
  if (c.length !== o.length) return !1;
  for (var s = {}, u = 0; u < c.length; u += 1)
    s[c[u]] = !0;
  for (var y = 0; y < o.length; y += 1)
    s[o[y]] = !0;
  var d = Object.keys(s);
  if (d.length !== c.length)
    return !1;
  var _ = e, g = n, a = function(p) {
    return t(_[p], g[p]);
  };
  return d.every(a);
}, re = function(e, n, r) {
  return A(e) ? Object.keys(e).reduce(function(f, i) {
    var m = !A(n) || !Re(e[i], n[i]);
    return r.includes(i) ? (m && console.warn("Unsupported prop change: options.".concat(i, " is not a mutable property.")), f) : m ? V(V({}, f || {}), {}, ne({}, i, e[i])) : f;
  }, null) : null;
}, Ie = "Invalid prop `stripe` supplied to `Elements`. We recommend using the `loadStripe` utility from `@stripe/stripe-js`. See https://stripe.com/docs/stripe-js/react#elements-props-stripe for details.", J = function(e) {
  if (e === null || Le(e))
    return e;
  throw new Error(Ie);
}, ke = function(e) {
  if (Ae(e))
    return {
      tag: "async",
      stripePromise: Promise.resolve(e).then(J)
    };
  var n = J(e);
  return n === null ? {
    tag: "empty"
  } : {
    tag: "sync",
    stripe: n
  };
}, U = /* @__PURE__ */ v.createContext(null);
U.displayName = "ElementsContext";
var Ne = function(e, n) {
  if (!e)
    throw new Error("Could not find Elements context; You need to wrap the part of your app that ".concat(n, " in an <Elements> provider."));
  return e;
}, ae = function(e) {
  var n = e.stripe, r = e.options, f = e.children, i = v.useMemo(function() {
    return ke(n);
  }, [n]), m = v.useState(function() {
    return {
      stripe: i.tag === "sync" ? i.stripe : null,
      elements: i.tag === "sync" ? i.stripe.elements(r) : null
    };
  }), c = Ce(m, 2), o = c[0], s = c[1];
  v.useEffect(function() {
    var d = !0, _ = function(a) {
      s(function(l) {
        return l.stripe ? l : {
          stripe: a,
          elements: a.elements(r)
        };
      });
    };
    return i.tag === "async" && !o.stripe ? i.stripePromise.then(function(g) {
      g && d && _(g);
    }) : i.tag === "sync" && !o.stripe && _(i.stripe), function() {
      d = !1;
    };
  }, [i, o, r]);
  var u = $(n);
  v.useEffect(function() {
    u !== null && u !== n && console.warn("Unsupported prop change on Elements: You cannot change the `stripe` prop after setting it.");
  }, [u, n]);
  var y = $(r);
  return v.useEffect(function() {
    if (o.elements) {
      var d = re(r, y, ["clientSecret", "fonts"]);
      d && o.elements.update(d);
    }
  }, [r, y, o.elements]), v.useEffect(function() {
    var d = o.stripe;
    !d || !d._registerWrapper || !d.registerAppInfo || (d._registerWrapper({
      name: "react-stripe-js",
      version: "1.10.0"
    }), d.registerAppInfo({
      name: "react-stripe-js",
      version: "1.10.0",
      url: "https://stripe.com/docs/stripe-js/react"
    }));
  }, [o.stripe]), /* @__PURE__ */ v.createElement(U.Provider, {
    value: o
  }, f);
};
ae.propTypes = {
  stripe: C.any,
  options: C.object
};
var k = function(e) {
  var n = v.useContext(U);
  return Ne(n, e);
}, Te = function() {
  var e = k("calls useElements()"), n = e.elements;
  return n;
}, Be = function() {
  var e = k("calls useStripe()"), n = e.stripe;
  return n;
};
C.func.isRequired;
var O = function(e) {
  var n = v.useRef(e);
  return v.useEffect(function() {
    n.current = e;
  }, [e]), function() {
    n.current && n.current.apply(n, arguments);
  };
}, w = function() {
}, $e = function(e) {
  return e.charAt(0).toUpperCase() + e.slice(1);
}, S = function(e, n) {
  var r = "".concat($e(e), "Element"), f = function(o) {
    var s = o.id, u = o.className, y = o.options, d = y === void 0 ? {} : y, _ = o.onBlur, g = _ === void 0 ? w : _, a = o.onFocus, l = a === void 0 ? w : a, p = o.onReady, h = p === void 0 ? w : p, x = o.onChange, oe = x === void 0 ? w : x, q = o.onEscape, ie = q === void 0 ? w : q, F = o.onClick, se = F === void 0 ? w : F, W = o.onLoadError, ce = W === void 0 ? w : W, D = o.onLoaderStart, ue = D === void 0 ? w : D, le = k("mounts <".concat(r, ">")), M = le.elements, j = v.useRef(null), N = v.useRef(null), fe = O(h), de = O(g), pe = O(l), me = O(se), ve = O(oe), ye = O(ie), ge = O(ce), Se = O(ue);
    v.useLayoutEffect(function() {
      if (j.current == null && M && N.current != null) {
        var E = M.create(e, d);
        j.current = E, E.mount(N.current), E.on("ready", function() {
          return fe(E);
        }), E.on("change", ve), E.on("blur", de), E.on("focus", pe), E.on("escape", ye), E.on("loaderror", ge), E.on("loaderstart", Se), E.on("click", me);
      }
    });
    var K = $(d);
    return v.useEffect(function() {
      if (j.current) {
        var E = re(d, K, ["paymentRequest"]);
        E && j.current.update(E);
      }
    }, [d, K]), v.useLayoutEffect(function() {
      return function() {
        j.current && (j.current.destroy(), j.current = null);
      };
    }, []), /* @__PURE__ */ v.createElement("div", {
      id: s,
      className: u,
      ref: N
    });
  }, i = function(o) {
    k("mounts <".concat(r, ">"));
    var s = o.id, u = o.className;
    return /* @__PURE__ */ v.createElement("div", {
      id: s,
      className: u
    });
  }, m = n ? i : f;
  return m.propTypes = {
    id: C.string,
    className: C.string,
    onChange: C.func,
    onBlur: C.func,
    onFocus: C.func,
    onReady: C.func,
    onClick: C.func,
    onLoadError: C.func,
    onLoaderStart: C.func,
    options: C.object
  }, m.displayName = r, m.__elementType = e, m;
}, b = typeof window > "u";
S("auBankAccount", b);
S("card", b);
S("cardNumber", b);
S("cardExpiry", b);
S("cardCvc", b);
S("fpxBank", b);
S("iban", b);
S("idealBank", b);
S("p24Bank", b);
S("epsBank", b);
var Ue = S("payment", b);
S("paymentRequestButton", b);
S("linkAuthentication", b);
S("shippingAddress", b);
S("affirmMessage", b);
S("afterpayClearpayMessage", b);
var L = {}, Q;
function qe() {
  if (Q) return L;
  Q = 1, Object.defineProperty(L, "__esModule", { value: !0 });
  function t(g) {
    "@babel/helpers - typeof";
    return typeof Symbol == "function" && typeof Symbol.iterator == "symbol" ? t = function(a) {
      return typeof a;
    } : t = function(a) {
      return a && typeof Symbol == "function" && a.constructor === Symbol && a !== Symbol.prototype ? "symbol" : typeof a;
    }, t(g);
  }
  var e = "https://js.stripe.com/v3", n = /^https:\/\/js\.stripe\.com\/v3\/?(\?.*)?$/, r = "loadStripe.setLoadParameters was called but an existing Stripe.js script already exists in the document; existing script parameters will be used", f = function() {
    for (var a = document.querySelectorAll('script[src^="'.concat(e, '"]')), l = 0; l < a.length; l++) {
      var p = a[l];
      if (n.test(p.src))
        return p;
    }
    return null;
  }, i = function(a) {
    var l = a && !a.advancedFraudSignals ? "?advancedFraudSignals=false" : "", p = document.createElement("script");
    p.src = "".concat(e).concat(l);
    var h = document.head || document.body;
    if (!h)
      throw new Error("Expected document.body not to be null. Stripe.js requires a <body> element.");
    return h.appendChild(p), p;
  }, m = function(a, l) {
    !a || !a._registerWrapper || a._registerWrapper({
      name: "stripe-js",
      version: "1.35.0",
      startTime: l
    });
  }, c = null, o = function(a) {
    return c !== null || (c = new Promise(function(l, p) {
      if (typeof window > "u") {
        l(null);
        return;
      }
      if (window.Stripe && a && console.warn(r), window.Stripe) {
        l(window.Stripe);
        return;
      }
      try {
        var h = f();
        h && a ? console.warn(r) : h || (h = i(a)), h.addEventListener("load", function() {
          window.Stripe ? l(window.Stripe) : p(new Error("Stripe.js not available"));
        }), h.addEventListener("error", function() {
          p(new Error("Failed to load Stripe.js"));
        });
      } catch (x) {
        p(x);
        return;
      }
    })), c;
  }, s = function(a, l, p) {
    if (a === null)
      return null;
    var h = a.apply(void 0, l);
    return m(h, p), h;
  }, u = function(a) {
    var l = `invalid load parameters; expected object of shape

    {advancedFraudSignals: boolean}

but received

    `.concat(JSON.stringify(a), `
`);
    if (a === null || t(a) !== "object")
      throw new Error(l);
    if (Object.keys(a).length === 1 && typeof a.advancedFraudSignals == "boolean")
      return a;
    throw new Error(l);
  }, y, d = !1, _ = function() {
    for (var a = arguments.length, l = new Array(a), p = 0; p < a; p++)
      l[p] = arguments[p];
    d = !0;
    var h = Date.now();
    return o(y).then(function(x) {
      return s(x, l, h);
    });
  };
  return _.setLoadParameters = function(g) {
    if (d)
      throw new Error("You cannot change load parameters after calling loadStripe");
    y = u(g);
  }, L.loadStripe = _, L;
}
var T, Z;
function Fe() {
  return Z || (Z = 1, T = qe()), T;
}
var We = Fe(), De = {};
const Me = De.NEXT_PUBLIC_STRIPE_PUBLIC_KEY || "";
let B;
const Ke = (t) => (B || (B = We.loadStripe(
  t || Me
  /* , {
    locale: "es-419" TODO: Handle multiple locales,
  } */
)), B), He = (t) => {
  const { t: e, i18n: n } = te(), { paymentOption: r, elements: f, state: i, onPaymentElementChange: m } = t, [c, o] = I(!1), [s, u] = I(r !== "HOLD"), y = c || !s || ["processing", "error"].includes(i.status), d = {
    layout: "accordion"
  };
  return ee(() => {
    f == null || f.update({ locale: n.language });
  }, [f, n.language]), /* @__PURE__ */ H("form", { id: "payment-form", className: "bg-subtle mt-4 rounded-md p-6", onSubmit: t.onSubmit, children: [
    /* @__PURE__ */ P("div", { children: /* @__PURE__ */ P(Ue, { options: d, onChange: (_) => m() }) }),
    r === "HOLD" && /* @__PURE__ */ P("div", { className: "bg-cal-info mb-5 mt-2 rounded-md p-3", children: /* @__PURE__ */ P(
      Ee,
      {
        description: e("acknowledge_booking_no_show_fee", {
          amount: t.payment.amount / 100,
          formatParams: { amount: { currency: t.payment.currency } }
        }),
        onChange: (_) => u(_.target.checked),
        descriptionClassName: "text-info font-semibold"
      }
    ) }),
    /* @__PURE__ */ H("div", { className: "mt-2 flex justify-end space-x-2", children: [
      /* @__PURE__ */ P(
        G,
        {
          color: "minimal",
          disabled: y,
          id: "cancel",
          type: "button",
          loading: c,
          onClick: () => {
            o(!0), t.onCancel();
          },
          children: /* @__PURE__ */ P("span", { id: "button-text", children: e("cancel") })
        }
      ),
      /* @__PURE__ */ P(
        G,
        {
          type: "submit",
          disabled: y,
          loading: i.status === "processing",
          id: "submit",
          color: "secondary",
          children: /* @__PURE__ */ P("span", { id: "button-text", children: i.status === "processing" ? /* @__PURE__ */ P("div", { className: "spinner", id: "spinner" }) : e(r === "HOLD" ? "submit_card" : "pay_now") })
        }
      )
    ] }),
    i.status === "error" && /* @__PURE__ */ P("div", { className: "mt-4 text-center text-red-900 dark:text-gray-300", role: "alert", children: i.error.message })
  ] });
}, Ge = (t) => {
  const { t: e } = te(), n = Te(), r = t.payment.paymentOption, f = t.booking.attendees[0].email, i = Be(), [m, c] = I({ status: "idle" });
  return /* @__PURE__ */ P(
    He,
    {
      ...t,
      elements: n,
      paymentOption: r,
      state: m,
      onSubmit: async (o) => {
        var y;
        if (o.preventDefault(), !i || !n)
          return;
        c({ status: "processing" });
        let s;
        const u = {
          uid: t.booking.uid,
          email: f
        };
        r === "HOLD" && "setupIntent" in t.payment.data ? (s = await i.confirmSetup({
          elements: n,
          redirect: "if_required"
        }), s.setupIntent && (u.payment_intent = s.setupIntent.id, u.payment_intent_client_secret = s.setupIntent.client_secret || void 0, u.redirect_status = s.setupIntent.status)) : r === "ON_BOOKING" && (s = await i.confirmPayment({
          elements: n,
          redirect: "if_required",
          confirmParams: {
            return_url: `${_e}/booking/${u.uid}`
          }
        }), s.paymentIntent && (u.payment_intent = s.paymentIntent.id, u.payment_intent_client_secret = s.paymentIntent.client_secret || void 0, u.redirect_status = s.paymentIntent.status)), s != null && s.error ? c({
          status: "error",
          error: new Error(`Payment failed: ${s.error.message}`)
        }) : (c({ status: "idle" }), (y = t.onPaymentSuccess) == null || y.call(t, t), t.location && (t.location.includes("integration") ? u.location = e("web_conferencing_details_to_follow") : u.location = t.location));
      },
      onCancel: () => {
        var o;
        (o = t.onPaymentCancellation) == null || o.call(t, t);
      },
      onPaymentElementChange: () => {
        c({ status: "idle" });
      }
    }
  );
}, ze = (t) => {
  const e = Ke(t.payment.data.stripe_publishable_key), [n, r] = I("stripe");
  return ee(() => {
    document.documentElement.classList.contains("dark") && r("night");
  }, []), /* @__PURE__ */ P(
    ae,
    {
      stripe: e,
      options: {
        clientSecret: t.clientSecret,
        appearance: {
          theme: n
        }
      },
      children: /* @__PURE__ */ P(Ge, { ...t })
    }
  );
};
export {
  ze as default
};
