import { r as registerInstance, c as createEvent, h, g as getElement } from './index-f1d2a96f.js';
import { p as pure } from './pure-4f52cebf.js';
import './_commonjsHelpers-9943807e.js';

const scStripePaymentElementCss = "sc-stripe-payment-element{display:block}.loader{display:grid;height:128px;gap:2em}.loader__row{display:flex;align-items:flex-start;justify-content:space-between;gap:1em}.loader__details{display:grid;gap:0.5em}";

const ScStripePaymentElement = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.scPaid = createEvent(this, "scPaid", 7);
    this.scPayError = createEvent(this, "scPayError", 7);
    /** Is this yet loaded. */
    this.loaded = false;
    /** Are we confirming the order? */
    this.confirming = false;
  }
  /** Maybe load the stripe element on load. */
  async componentDidLoad() {
    if (!this.publishableKey || !this.accountId)
      return;
    this.stripe = await pure.loadStripe(this.publishableKey, { stripeAccount: this.accountId });
    this.loadElement();
  }
  /** Reload element if client secret changes. */
  handleClientSecretChange(val, prev) {
    if (val !== prev) {
      this.loaded = false;
      this.loadElement();
    }
  }
  /**
   * Watch order status and maybe confirm the order.
   */
  async maybeConfirmOrder(val) {
    var _a, _b, _c, _d, _e, _f, _g;
    // must be finalized
    if ((val === null || val === void 0 ? void 0 : val.status) !== 'finalized')
      return;
    // must be a stripe session
    if (((_a = val === null || val === void 0 ? void 0 : val.payment_intent) === null || _a === void 0 ? void 0 : _a.processor_type) !== 'stripe')
      return;
    // need an external_type
    if (!((_d = (_c = (_b = val === null || val === void 0 ? void 0 : val.payment_intent) === null || _b === void 0 ? void 0 : _b.processor_data) === null || _c === void 0 ? void 0 : _c.stripe) === null || _d === void 0 ? void 0 : _d.type))
      return;
    // confirm the intent.
    return await this.confirm((_g = (_f = (_e = val === null || val === void 0 ? void 0 : val.payment_intent) === null || _e === void 0 ? void 0 : _e.processor_data) === null || _f === void 0 ? void 0 : _f.stripe) === null || _g === void 0 ? void 0 : _g.type);
  }
  async confirm(type, args = {}) {
    const confirmArgs = {
      elements: this.elements,
      confirmParams: {
        return_url: this.successUrl,
      },
      redirect: 'if_required',
      ...args,
    };
    // prevent possible double-charges
    if (this.confirming)
      return;
    try {
      const response = type === 'setup' ? await this.stripe.confirmSetup(confirmArgs) : await this.stripe.confirmPayment(confirmArgs);
      if (response === null || response === void 0 ? void 0 : response.error) {
        this.error = response.error.message;
        throw response.error;
      }
      // paid
      this.scPaid.emit();
    }
    catch (e) {
      console.error(e);
      this.scPayError.emit(e);
      if (e.message) {
        this.error = e.message;
      }
    }
    finally {
      this.confirming = false;
    }
  }
  loadElement() {
    // we need a stripe instance and client secret.
    if (!this.stripe || !this.clientSecret || !this.container) {
      return;
    }
    // get the computed styles.
    const styles = getComputedStyle(document.body);
    // we have what we need, load elements.
    this.elements = this.stripe.elements({
      clientSecret: this.clientSecret,
      appearance: {
        variables: {
          colorPrimary: styles.getPropertyValue('--sc-color-primary-500'),
          colorText: styles.getPropertyValue('--sc-input-label-color'),
          borderRadius: styles.getPropertyValue('--sc-input-border-radius-medium'),
          colorBackground: styles.getPropertyValue('--sc-input-background-color'),
          fontSizeBase: styles.getPropertyValue('--sc-input-font-size-medium'),
        },
        rules: {
          '.Input': {
            border: styles.getPropertyValue('--sc-input-border'),
          },
          '.Input::placeholder': {
            color: styles.getPropertyValue('--sc-input-placeholder-color'),
          },
        },
      },
    });
    // create the payment element.
    this.elements
      .create('payment', {
      wallets: {
        applePay: 'never',
        googlePay: 'never',
      },
    })
      .mount('.sc-payment-element-container');
    this.element = this.elements.getElement('payment');
    this.element.on('ready', () => (this.loaded = true));
  }
  render() {
    return (h("div", { class: "sc-stripe-payment-element", "data-testid": "stripe-payment-element" }, !!this.error && (h("sc-text", { style: {
        'color': 'var(--sc-color-danger-500)',
        '--font-size': 'var(--sc-font-size-small)',
        'marginBottom': '0.5em',
      } }, this.error)), h("div", { class: "loader", hidden: this.loaded }, h("div", { class: "loader__row" }, h("div", { style: { width: '50%' } }, h("sc-skeleton", { style: { width: '50%', marginBottom: '0.5em' } }), h("sc-skeleton", null)), h("div", { style: { flex: '1' } }, h("sc-skeleton", { style: { width: '50%', marginBottom: '0.5em' } }), h("sc-skeleton", null)), h("div", { style: { flex: '1' } }, h("sc-skeleton", { style: { width: '50%', marginBottom: '0.5em' } }), h("sc-skeleton", null))), h("div", { class: "loader__details" }, h("sc-skeleton", { style: { height: '1rem' } }), h("sc-skeleton", { style: { height: '1rem', width: '30%' } }))), h("div", { hidden: !this.loaded, class: "sc-payment-element-container", ref: el => (this.container = el) })));
  }
  get el() { return getElement(this); }
  static get watchers() { return {
    "clientSecret": ["handleClientSecretChange"],
    "order": ["maybeConfirmOrder"]
  }; }
};
ScStripePaymentElement.style = scStripePaymentElementCss;

export { ScStripePaymentElement as sc_stripe_payment_element };
