import { proxyCustomElement, HTMLElement, h, Fragment } from '@stencil/core/internal/client';
import { _ as __ } from './default-i18n.js';
import './index2.js';
import { a as apiFetch } from './fetch.js';
import { d as defineCustomElement$h } from './sc-alert2.js';
import { d as defineCustomElement$g } from './sc-block-ui2.js';
import { d as defineCustomElement$f } from './sc-button2.js';
import { d as defineCustomElement$e } from './sc-card2.js';
import { d as defineCustomElement$d } from './sc-cc-logo2.js';
import { d as defineCustomElement$c } from './sc-choice2.js';
import { d as defineCustomElement$b } from './sc-choices2.js';
import { d as defineCustomElement$a } from './sc-dashboard-module2.js';
import { d as defineCustomElement$9 } from './sc-empty2.js';
import { d as defineCustomElement$8 } from './sc-flex2.js';
import { d as defineCustomElement$7 } from './sc-form2.js';
import { d as defineCustomElement$6 } from './sc-form-control2.js';
import { d as defineCustomElement$5 } from './sc-icon2.js';
import { d as defineCustomElement$4 } from './sc-skeleton2.js';
import { d as defineCustomElement$3 } from './sc-spinner2.js';
import { d as defineCustomElement$2 } from './sc-tooltip2.js';
import { a as addQueryArgs } from './add-query-args.js';

const scSubscriptionPaymentCss = ":host{display:block;position:relative}.subscription-payment{display:grid;gap:0.5em}";

const ScSubscriptionPayment$1 = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  constructor() {
    super();
    this.__registerHost();
    this.__attachShadow();
    this.paymentMethods = [];
    this.customerIds = [];
  }
  componentWillLoad() {
    this.fetchItems();
  }
  async fetchItems() {
    try {
      this.loading = true;
      await Promise.all([this.fetchSubscription(), this.fetchPaymentMethods()]);
    }
    catch (e) {
      console.error(e);
      this.error = (e === null || e === void 0 ? void 0 : e.message) || __('Something went wrong', 'surecart');
    }
    finally {
      this.loading = false;
    }
  }
  async fetchSubscription() {
    if (!this.subscriptionId)
      return;
    this.subscription = (await apiFetch({
      path: addQueryArgs(`/surecart/v1/subscriptions/${this.subscriptionId}`, {
        expand: ['price', 'price.product', 'latest_invoice', 'product'],
      }),
    }));
  }
  async fetchPaymentMethods() {
    var _a;
    this.paymentMethods = (await apiFetch({
      path: addQueryArgs(`/surecart/v1/payment_methods`, {
        expand: ['card'],
        customer_ids: this.customerIds,
        ...(((_a = this.subscription) === null || _a === void 0 ? void 0 : _a.live_mode) !== null ? { live_mode: this.subscription.live_mode } : {}),
      }),
    }));
  }
  async handleSubmit(e) {
    var _a;
    const { method } = await e.target.getFormJson();
    try {
      this.error = '';
      this.busy = true;
      await apiFetch({
        path: `/surecart/v1/subscriptions/${(_a = this.subscription) === null || _a === void 0 ? void 0 : _a.id}`,
        method: 'PATCH',
        data: {
          payment_method: method,
        },
      });
      if (this.successUrl) {
        window.location.assign(this.successUrl);
      }
      else {
        this.busy = false;
      }
    }
    catch (e) {
      this.error = (e === null || e === void 0 ? void 0 : e.message) || __('Something went wrong', 'surecart');
      this.busy = false;
    }
  }
  renderLoading() {
    return (h(Fragment, null, h("sc-choice", { name: "loading", disabled: true }, h("sc-skeleton", { style: { width: '60px', display: 'inline-block' } }), h("sc-skeleton", { style: { width: '80px', display: 'inline-block' }, slot: "price" }), h("sc-skeleton", { style: { width: '120px', display: 'inline-block' }, slot: "description" })), h("sc-button", { type: "primary", full: true, submit: true, loading: true, busy: true }), !!this.backUrl && h("sc-button", { href: this.backUrl, full: true, loading: true, busy: true })));
  }
  renderContent() {
    if (this.loading) {
      return this.renderLoading();
    }
    const modeMethods = this.paymentMethods.filter(method => { var _a; return (method === null || method === void 0 ? void 0 : method.live_mode) === ((_a = this.subscription) === null || _a === void 0 ? void 0 : _a.live_mode); });
    if (!(modeMethods === null || modeMethods === void 0 ? void 0 : modeMethods.length)) {
      return (h(Fragment, null, h("sc-empty", { icon: "credit-card" }, __('You have no saved payment methods.', 'surecart')), !!this.backUrl && (h("sc-button", { href: this.backUrl, full: true }, __('Go Back', 'surecart')))));
    }
    return (h(Fragment, null, h("sc-choices", null, h("div", null, (this.paymentMethods || []).map(({ id, card, live_mode }) => {
      var _a, _b;
      if (live_mode !== ((_a = this === null || this === void 0 ? void 0 : this.subscription) === null || _a === void 0 ? void 0 : _a.live_mode))
        return null;
      return (h("sc-choice", { checked: ((_b = this.subscription) === null || _b === void 0 ? void 0 : _b.payment_method) === id, name: "method", value: id }, h("sc-flex", { "justify-content": "flex-start", "align-items": "center", style: { '--spacing': '0.5em' } }, h("sc-cc-logo", { style: { fontSize: '36px' }, brand: card === null || card === void 0 ? void 0 : card.brand }), h("span", { style: { fontSize: '7px', whiteSpace: 'nowrap' } }, '\u2B24', " ", '\u2B24', " ", '\u2B24', " ", '\u2B24'), h("span", null, card === null || card === void 0 ? void 0 : card.last4)), h("span", { slot: "per" }, __('Expires', 'surecart'), " ", card === null || card === void 0 ? void 0 :
        card.exp_month, "/", card === null || card === void 0 ? void 0 :
        card.exp_year)));
    }))), h("sc-button", { type: "primary", full: true, submit: true, loading: this.loading || this.busy, disabled: this.loading || this.busy }, __('Update', 'surecart')), !!this.backUrl && (h("sc-button", { href: this.backUrl, full: true, loading: this.loading || this.busy, disabled: this.loading || this.busy }, __('Go Back', 'surecart')))));
  }
  render() {
    return (h("sc-dashboard-module", { heading: __('Select a payment method', 'surecart'), class: "subscription-payment", error: this.error }, h("sc-form", { onScFormSubmit: e => this.handleSubmit(e) }, h("sc-card", null, this.renderContent())), this.busy && h("sc-block-ui", null)));
  }
  static get style() { return scSubscriptionPaymentCss; }
}, [1, "sc-subscription-payment", {
    "subscriptionId": [1, "subscription-id"],
    "backUrl": [1, "back-url"],
    "successUrl": [1, "success-url"],
    "subscription": [1040],
    "paymentMethods": [16],
    "customerIds": [16],
    "loading": [32],
    "busy": [32],
    "error": [32]
  }]);
function defineCustomElement$1() {
  if (typeof customElements === "undefined") {
    return;
  }
  const components = ["sc-subscription-payment", "sc-alert", "sc-block-ui", "sc-button", "sc-card", "sc-cc-logo", "sc-choice", "sc-choices", "sc-dashboard-module", "sc-empty", "sc-flex", "sc-form", "sc-form-control", "sc-icon", "sc-skeleton", "sc-spinner", "sc-tooltip"];
  components.forEach(tagName => { switch (tagName) {
    case "sc-subscription-payment":
      if (!customElements.get(tagName)) {
        customElements.define(tagName, ScSubscriptionPayment$1);
      }
      break;
    case "sc-alert":
      if (!customElements.get(tagName)) {
        defineCustomElement$h();
      }
      break;
    case "sc-block-ui":
      if (!customElements.get(tagName)) {
        defineCustomElement$g();
      }
      break;
    case "sc-button":
      if (!customElements.get(tagName)) {
        defineCustomElement$f();
      }
      break;
    case "sc-card":
      if (!customElements.get(tagName)) {
        defineCustomElement$e();
      }
      break;
    case "sc-cc-logo":
      if (!customElements.get(tagName)) {
        defineCustomElement$d();
      }
      break;
    case "sc-choice":
      if (!customElements.get(tagName)) {
        defineCustomElement$c();
      }
      break;
    case "sc-choices":
      if (!customElements.get(tagName)) {
        defineCustomElement$b();
      }
      break;
    case "sc-dashboard-module":
      if (!customElements.get(tagName)) {
        defineCustomElement$a();
      }
      break;
    case "sc-empty":
      if (!customElements.get(tagName)) {
        defineCustomElement$9();
      }
      break;
    case "sc-flex":
      if (!customElements.get(tagName)) {
        defineCustomElement$8();
      }
      break;
    case "sc-form":
      if (!customElements.get(tagName)) {
        defineCustomElement$7();
      }
      break;
    case "sc-form-control":
      if (!customElements.get(tagName)) {
        defineCustomElement$6();
      }
      break;
    case "sc-icon":
      if (!customElements.get(tagName)) {
        defineCustomElement$5();
      }
      break;
    case "sc-skeleton":
      if (!customElements.get(tagName)) {
        defineCustomElement$4();
      }
      break;
    case "sc-spinner":
      if (!customElements.get(tagName)) {
        defineCustomElement$3();
      }
      break;
    case "sc-tooltip":
      if (!customElements.get(tagName)) {
        defineCustomElement$2();
      }
      break;
  } });
}
defineCustomElement$1();

const ScSubscriptionPayment = ScSubscriptionPayment$1;
const defineCustomElement = defineCustomElement$1;

export { ScSubscriptionPayment, defineCustomElement };
