import { r as registerInstance, c as createEvent, h, g as getElement } from './index-f1d2a96f.js';
import { _ as __ } from './default-i18n-91ef32a0.js';
import './index-0a61debd.js';
import { U as Universe } from './universe-3b85f034.js';
import { g as getOrder } from './checkouts-d9b4e12a.js';
import './_commonjsHelpers-9943807e.js';

const scCheckoutCss = "sc-checkout{display:block;font-family:var(--sc-font-sans);font-size:var(--sc-checkout-font-size, 16px);position:relative}sc-checkout h3{font-size:var(--sc-input-label-font-size-medium)}sc-alert{margin-bottom:var(--sc-form-row-spacing)}.sc-checkout-container.sc-align-center{max-width:500px;margin-left:auto;margin-right:auto}.sc-checkout-container.sc-align-wide{max-width:800px;margin-left:auto;margin-right:auto}::slotted(*){font-family:var(--sc-font-sans)}";

const ScCheckout = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.scOrderUpdated = createEvent(this, "scOrderUpdated", 7);
    this.scOrderFinalized = createEvent(this, "scOrderFinalized", 7);
    this.scOrderError = createEvent(this, "scOrderError", 7);
    /** An array of prices to pre-fill in the form. */
    this.prices = [];
    /** Are we in test or live mode. */
    this.mode = 'live';
    /** Currency to use for this checkout. */
    this.currencyCode = 'usd';
    /** Whether to persist the session in the browser between visits. */
    this.persistSession = true;
    /** Where to go on success */
    this.successUrl = '';
    /** Can we edit line items? */
    this.editLineItems = true;
    /** Can we remove line items? */
    this.removeLineItems = true;
    /** Use the Stripe payment element. */
    this.stripePaymentElement = false;
    /** Stores fetched prices for use throughout component.  */
    this.pricesEntities = {};
    /** Stores fetched products for use throughout component.  */
    this.productsEntities = {};
    /** Loading states for different parts of the form. */
    this.checkoutState = 'idle';
    /** The currenly selected processor */
    this.processor = 'stripe';
    /** Holds the payment intents for the checkout. */
    this.paymentIntents = {};
  }
  handleSetPaymentIntent(e) {
    var _a, _b;
    const paymentIntent = (_a = e.detail) === null || _a === void 0 ? void 0 : _a.payment_intent;
    const processor = (_b = e.detail) === null || _b === void 0 ? void 0 : _b.processor;
    this.paymentIntents[processor] = paymentIntent;
  }
  handleProcessorChange(e) {
    this.processor = e.detail;
  }
  handleAddEntities(e) {
    const { products, prices } = e.detail;
    // add products.
    if (Object.keys((products === null || products === void 0 ? void 0 : products.length) || {})) {
      this.productsEntities = {
        ...this.productsEntities,
        ...products,
      };
    }
    // add prices.
    if (Object.keys((prices === null || prices === void 0 ? void 0 : prices.length) || {})) {
      this.pricesEntities = {
        ...this.pricesEntities,
        ...prices,
      };
    }
  }
  /**
   * Submit the form
   */
  async submit({ skip_validation } = { skip_validation: false }) {
    if (!skip_validation) {
      await this.validate();
    }
    return await this.sessionProvider.finalize();
  }
  /**
   * Validate the form.
   */
  async validate() {
    const form = this.el.querySelector('sc-form');
    return await form.validate();
  }
  componentWillLoad() {
    Universe.create(this, this.state());
    this.isDuplicate = document.querySelector('sc-checkout') !== this.el;
  }
  order() {
    return getOrder(this === null || this === void 0 ? void 0 : this.formId, this.mode);
  }
  state() {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
    return {
      processor: this.processor,
      processors: this.processors,
      processor_data: (_a = this.order()) === null || _a === void 0 ? void 0 : _a.processor_data,
      state: this.checkoutState,
      paymentIntents: this.paymentIntents,
      successUrl: this.successUrl,
      order: this.order(),
      shippingEnabled: (_b = this.order()) === null || _b === void 0 ? void 0 : _b.shipping_enabled,
      lineItems: ((_d = (_c = this.order()) === null || _c === void 0 ? void 0 : _c.line_items) === null || _d === void 0 ? void 0 : _d.data) || [],
      editLineItems: this.editLineItems,
      removeLineItems: this.removeLineItems,
      // checkout states
      loading: this.checkoutState === 'loading',
      busy: ['updating', 'finalizing', 'paid', 'confirmed'].includes(this.checkoutState),
      paying: ['finalizing', 'paid', 'confirmed'].includes(this.checkoutState),
      empty: !['loading', 'updating'].includes(this.checkoutState) && !((_g = (_f = (_e = this.order()) === null || _e === void 0 ? void 0 : _e.line_items) === null || _f === void 0 ? void 0 : _f.pagination) === null || _g === void 0 ? void 0 : _g.count),
      // checkout states
      // stripe.
      stripePaymentElement: this.stripePaymentElement,
      error: this.error,
      customer: this.customer,
      tax_status: (_h = this.order()) === null || _h === void 0 ? void 0 : _h.tax_status,
      customerShippingAddress: typeof ((_j = this.order()) === null || _j === void 0 ? void 0 : _j.customer) !== 'string' ? (_l = (_k = this.order()) === null || _k === void 0 ? void 0 : _k.customer) === null || _l === void 0 ? void 0 : _l.shipping_address : {},
      shippingAddress: (_m = this.order()) === null || _m === void 0 ? void 0 : _m.shipping_address,
      taxStatus: (_o = this.order()) === null || _o === void 0 ? void 0 : _o.tax_status,
      taxIdentifier: (_p = this.order()) === null || _p === void 0 ? void 0 : _p.tax_identifier,
      taxProtocol: this.taxProtocol,
      lockedChoices: this.prices,
      products: this.productsEntities,
      prices: this.pricesEntities,
      country: 'US',
      loggedIn: this.loggedIn,
      formId: this.formId,
      mode: this.mode,
      currencyCode: this.currencyCode,
    };
  }
  render() {
    if (this.isDuplicate) {
      return h("sc-alert", { open: true }, __('Due to processor restrictions, only one checkout form is allowed on the page.', 'surecart'));
    }
    return (h("div", { class: {
        'sc-checkout-container': true,
        'sc-align-center': this.alignment === 'center',
        'sc-align-wide': this.alignment === 'wide',
        'sc-align-full': this.alignment === 'full',
      } }, h(Universe.Provider, { state: this.state() }, h("sc-form-state-provider", { onScSetCheckoutFormState: e => (this.checkoutState = e.detail) }, h("sc-form-error-provider", { order: this.order(), onScUpdateError: e => (this.error = e.detail) }, h("sc-form-components-validator", { order: this.order(), disabled: this.disableComponentsValidation, taxProtocol: this.taxProtocol }, h("sc-order-confirm-provider", { order: this.order(), "success-url": this.successUrl, "form-id": this.formId, mode: this.mode }, h("sc-session-provider", { ref: el => (this.sessionProvider = el), prices: this.prices, stripePaymentElement: this.stripePaymentElement, paymentIntents: this.paymentIntents, persist: this.persistSession, modified: this.modified, mode: this.mode, "form-id": this.formId, "group-id": this.el.id, processor: this.processor, "currency-code": this.currencyCode, onScError: e => (this.error = e.detail) }, h("slot", null)))))), this.state().busy && h("sc-block-ui", { "z-index": 9 }))));
  }
  get el() { return getElement(this); }
};
ScCheckout.style = scCheckoutCss;

export { ScCheckout as sc_checkout };
