'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const index = require('./index-cd6fab62.js');
const defaultI18n = require('./default-i18n-072e75d7.js');
require('./index-20b57ec7.js');
const consumer = require('./consumer-1372755b.js');
const index$1 = require('./index-2fb0a3b9.js');
const processor = require('./processor-94c05c73.js');
require('./_commonjsHelpers-537d719a.js');

const scPaymentCss = "sc-payment{display:flex !important;flex-direction:column;gap:var(--sc-input-label-margin);position:relative;font-family:var(--sc-font-sans)}.sc-payment-toggle-summary{line-height:1;display:flex;align-items:center;gap:0.5em}.sc-payment-label{display:flex;justify-content:space-between}.sc-payment-instructions{color:var(--sc-color-gray-600)}.sc-payment__stripe-card-element{display:flex !important;flex-direction:column;gap:var(--sc-input-label-margin);position:relative}";

const ScPayment = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.scSetProcessor = index.createEvent(this, "scSetProcessor", 7);
    /** The current payment method for the payment */
    this.processor = 'stripe';
    /** List of available processors. */
    this.processors = [];
    /** Is this created in "test" mode */
    this.mode = 'live';
    /** The currency code. */
    this.currencyCode = 'usd';
    /** Default */
    this.defaultProcessor = 'stripe';
  }
  handleOrderChange(_, prev) {
    if (!prev) {
      this.handleDefaultChange();
    }
  }
  handleDefaultChange() {
    if (this.defaultProcessor) {
      this.scSetProcessor.emit(this.defaultProcessor);
    }
  }
  componentWillLoad() {
    this.setProcessors();
  }
  /** Set the processors for this order. */
  setProcessors() {
    this.stripe = (this.processors || []).find(processor => processor.processor_type === 'stripe' && (processor === null || processor === void 0 ? void 0 : processor.live_mode) === (this.mode === 'live'));
    this.paypal = (this.processors || []).find(processor => processor.processor_type === 'paypal' && (processor === null || processor === void 0 ? void 0 : processor.live_mode) === (this.mode === 'live'));
  }
  /**
   * Render the payment element.
   */
  renderStripePaymentElement() {
    if (this.stripePaymentElement) {
      return index.h("sc-order-stripe-payment-element", { order: this.order, mode: this.mode, processors: this.processors, "currency-code": this.currencyCode });
    }
    const data = processor.getProcessorData(this.processors, 'stripe', this.mode);
    return (index.h("div", { class: "sc-payment__stripe-card-element" }, index.h("sc-stripe-element", { order: this.order, mode: this.mode, publishableKey: data === null || data === void 0 ? void 0 : data.publishable_key, accountId: data === null || data === void 0 ? void 0 : data.account_id, secureText: this.secureNotice }), index.h("sc-secure-notice", null, this.secureNotice)));
  }
  renderTestModeBadge() {
    if (this.hideTestModeBadge)
      return null;
    return (this.mode === 'test' && (index.h("sc-tag", { type: "warning", size: "small" }, defaultI18n.__('Test Mode', 'surecart'))));
  }
  /**
   * Render Stripe and Paypal radio buttons.
   */
  renderStripeAndPayPal() {
    return (index.h("sc-form-control", { label: this.label }, index.h("div", { class: "sc-payment-label", slot: "label" }, index.h("div", null, this.label), this.renderTestModeBadge()), index.h("sc-toggles", { collapsible: false, theme: "container" }, index.h("sc-toggle", { class: "sc-stripe-toggle", "show-control": true, shady: true, borderless: true, open: this.processor === 'stripe', onScShow: () => this.scSetProcessor.emit('stripe') }, index.h("span", { slot: "summary", class: "sc-payment-toggle-summary" }, index.h("sc-icon", { name: "credit-card", style: { fontSize: '24px' } }), index.h("span", null, defaultI18n.__('Credit Card', 'surecart'))), this.renderStripePaymentElement()), index.h("sc-toggle", { class: "sc-paypal-toggle", "show-control": true, shady: true, borderless: true, open: this.processor === 'paypal', onScShow: () => this.scSetProcessor.emit('paypal') }, index.h("span", { slot: "summary", class: "sc-payment-toggle-summary" }, index.h("sc-icon", { name: "paypal", style: { width: '80px', fontSize: '24px' } })), index.h("div", { class: "sc-payment-instructions" }, defaultI18n.__('You will be prompted by PayPal to complete your purchase securely.', 'surecart'))))));
  }
  /**
   * Render PayPal options.
   */
  renderPayPal() {
    return (index.h("sc-form-control", { label: this.label }, index.h("div", { class: "sc-payment-label", slot: "label" }, index.h("div", null, this.label), this.renderTestModeBadge()), index.h("sc-toggles", { collapsible: false, theme: "container" }, index.h("sc-toggle", { "data-test-id": "paypal-credit-card-toggle", "show-control": true, shady: true, borderless: true, open: this.processor === 'paypal-card', onScShow: () => this.scSetProcessor.emit('paypal-card') }, index.h("span", { slot: "summary", class: "sc-payment-toggle-summary" }, index.h("sc-icon", { name: "credit-card", style: { fontSize: '24px' } }), index.h("span", null, defaultI18n.__('Credit Card', 'surecart'))), index.h("div", { class: "sc-payment-instructions" }, defaultI18n.__('You will be prompted to complete your purchase securely.', 'surecart'))), index.h("sc-toggle", { "data-test-id": "paypal-toggle", "show-control": true, shady: true, borderless: true, open: this.processor === 'paypal', onScShow: () => this.scSetProcessor.emit('paypal') }, index.h("span", { slot: "summary", class: "sc-payment-toggle-summary" }, index.h("sc-icon", { name: "paypal", style: { width: '80px', fontSize: '24px' } })), index.h("div", { class: "sc-payment-instructions" }, defaultI18n.__('You will be prompted by PayPal to complete your purchase securely.', 'surecart'))))));
  }
  renderLoading() {
    return (index.h("div", null, index.h("sc-skeleton", { style: { width: '10%', marginBottom: '1em' } }), index.h("sc-skeleton", { style: { width: '100%', marginBottom: '1em' } }), index.h("sc-skeleton", { style: { width: '35%' } })));
  }
  getProcessor() {
    switch (this.processor) {
      case 'paypal':
      case 'paypal-card':
        return 'paypal';
      default:
        return 'stripe';
    }
  }
  renderNoProcessors() {
    return (index.h("sc-form-control", { label: this.label }, index.h("sc-alert", { type: "info", open: true }, defaultI18n.__('Please contact us for payment', 'surecart'))));
  }
  render() {
    var _a, _b, _c, _d, _e;
    // no payment is required if we dont have a subscription and the total amount is 0.
    if (!index$1.hasSubscription(this.order)) {
      if (((_c = (_b = (_a = this.order) === null || _a === void 0 ? void 0 : _a.line_items) === null || _b === void 0 ? void 0 : _b.pagination) === null || _c === void 0 ? void 0 : _c.count) >= 1 && ((_d = this.order) === null || _d === void 0 ? void 0 : _d.total_amount) === 0) {
        return null;
      }
    }
    // we don't have any processors.
    if (!((_e = this.processors) === null || _e === void 0 ? void 0 : _e.length)) {
      console.warn('No processors are configured for this merchant.');
      return this.renderNoProcessors();
    }
    // both stripe and paypal are enabled.
    if (this.stripe && this.paypal) {
      return this.renderStripeAndPayPal();
    }
    // we have stripe.
    if (this.stripe) {
      return (index.h("sc-form-control", { label: this.label }, index.h("div", { class: "sc-payment-label", slot: "label" }, index.h("div", null, this.label), this.renderTestModeBadge()), this.stripePaymentElement ? index.h("sc-card", null, this.renderStripePaymentElement()) : this.renderStripePaymentElement()));
    }
    // we have paypal.
    if (this.paypal) {
      return this.renderPayPal();
    }
    console.warn(`No processors are configured for the current cart items and mode. (${this.mode})`);
    // render no processors message.
    return this.renderNoProcessors();
  }
  static get watchers() { return {
    "order": ["handleOrderChange"],
    "defaultProcessor": ["handleDefaultChange"],
    "processors": ["setProcessors"],
    "mode": ["setProcessors"]
  }; }
};
consumer.openWormhole(ScPayment, ['processor', 'processors', 'order', 'mode', 'paymentMethod', 'loading', 'busy', 'currencyCode', 'stripePaymentElement'], false);
ScPayment.style = scPaymentCss;

exports.sc_payment = ScPayment;
