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

const scSubscriptionPaymentMethodCss = ":host{display:block}";

const ScSubscriptionPaymentMethod$1 = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  constructor() {
    super();
    this.__registerHost();
    this.__attachShadow();
  }
  renderLoading() {
    return (h("sc-card", { noPadding: true }, h("sc-stacked-list", null, h("sc-stacked-list-row", { style: { '--columns': '4' }, "mobile-size": 500 }, [...Array(4)].map(() => (h("sc-skeleton", { style: { width: '100px', display: 'inline-block' } })))))));
  }
  renderEmpty() {
    return (h("slot", { name: "empty" }, h("sc-card", null, h("sc-empty", { icon: "credit-card" }, __('You do not have any payment methods.', 'surecart')))));
  }
  componentWillLoad() {
    onFirstVisible(this.el, () => {
      this.getPaymentMethods();
    });
  }
  /** Get all subscriptions */
  async getPaymentMethods() {
    var _a, _b, _c, _d;
    if ((_a = this.paymentMethods) === null || _a === void 0 ? void 0 : _a.length)
      return;
    const customerId = ((_c = (_b = this.subscription) === null || _b === void 0 ? void 0 : _b.customer) === null || _c === void 0 ? void 0 : _c.id) || ((_d = this.subscription) === null || _d === void 0 ? void 0 : _d.customer);
    if (!customerId)
      return;
    try {
      this.loading = true;
      this.paymentMethods = await this.fetchMethods(customerId);
    }
    catch (e) {
      this.error = (e === null || e === void 0 ? void 0 : e.messsage) || __('Something went wrong', 'surecart');
      console.error(this.error);
    }
    finally {
      this.loading = false;
    }
  }
  async fetchMethods(customerId) {
    var _a;
    return (await apiFetch({
      path: addQueryArgs(`surecart/v1/payment_methods`, {
        expand: ['card', 'customer', 'billing_agreement', 'paypal_account', 'payment_instrument', 'bank_account'],
        customer_ids: [customerId],
        reusable: true,
        live_mode: (_a = this.subscription) === null || _a === void 0 ? void 0 : _a.live_mode,
      }),
    }));
  }
  async deleteMethod(method) {
    const r = confirm(__('Are you sure you want to remove this payment method?', 'surecart'));
    if (!r)
      return;
    try {
      this.busy = true;
      (await apiFetch({
        path: `surecart/v1/payment_methods/${method === null || method === void 0 ? void 0 : method.id}/detach`,
        method: 'PATCH',
      }));
      // remove from view.
      this.paymentMethods = this.paymentMethods.filter(m => m.id !== method.id);
    }
    catch (e) {
      this.error = (e === null || e === void 0 ? void 0 : e.messsage) || __('Something went wrong', 'surecart');
      console.error(this.error);
    }
    finally {
      this.busy = false;
    }
  }
  async updateMethod(e) {
    var _a, _b, _c, _d;
    const { payment_method } = await e.target.getFormJson();
    if (payment_method === (((_b = (_a = this.subscription) === null || _a === void 0 ? void 0 : _a.payment_method) === null || _b === void 0 ? void 0 : _b.id) || ((_c = this.subscription) === null || _c === void 0 ? void 0 : _c.payment_method))) {
      return;
    }
    try {
      this.busy = true;
      this.subscription = (await apiFetch({
        path: `surecart/v1/subscriptions/${(_d = this.subscription) === null || _d === void 0 ? void 0 : _d.id}`,
        method: 'PATCH',
        data: {
          payment_method,
        },
      }));
      // remove from view.
    }
    catch (e) {
      this.error = (e === null || e === void 0 ? void 0 : e.messsage) || __('Something went wrong', 'surecart');
      console.error(this.error);
    }
    finally {
      this.busy = false;
    }
  }
  renderContent() {
    var _a, _b, _c;
    if (this.loading) {
      return this.renderLoading();
    }
    if (!((_a = this.paymentMethods) === null || _a === void 0 ? void 0 : _a.length)) {
      return this.renderEmpty();
    }
    return (h("sc-form", { onScSubmit: e => this.updateMethod(e) }, h("sc-choices", null, this.renderList()), ((_b = this.paymentMethods) === null || _b === void 0 ? void 0 : _b.length) > 1 ? (h("sc-button", { type: "primary", submit: true, full: true, size: "large", busy: this.busy, disabled: this.busy }, __('Update Payment Method', 'surecart'))) : (h("sc-button", { type: "link", full: true, href: addQueryArgs(window.location.href, {
        action: 'create',
        model: 'payment_method',
        ...(((_c = this.subscription) === null || _c === void 0 ? void 0 : _c.live_mode) === false ? { live_mode: false } : {}),
        success_url: window.location.href,
      }) }, h("sc-icon", { name: "plus", slot: "prefix" }), __('Add New', 'surecart')))));
  }
  renderList() {
    return this.paymentMethods.map(paymentMethod => {
      var _a, _b, _c;
      const subscription_payment_method_id = ((_b = (_a = this.subscription) === null || _a === void 0 ? void 0 : _a.payment_method) === null || _b === void 0 ? void 0 : _b.id) || ((_c = this.subscription) === null || _c === void 0 ? void 0 : _c.payment_method);
      const { id, card, live_mode, paypal_account } = paymentMethod;
      return (h("sc-choice", { checked: subscription_payment_method_id === id, name: "payment_method", value: id, required: true }, h("sc-flex", { justifyContent: "flex-start", "align-items": "center" }, h("sc-payment-method", { paymentMethod: paymentMethod }), ' ', !live_mode && (h("sc-tag", { type: "warning", size: "small" }, __('Test', 'surecart')))), h("div", { slot: "description" }, !!(card === null || card === void 0 ? void 0 : card.exp_month) && (h("span", null, 
      /** Translators: Credit Card Expires (Exp. 11/27) */
      __('Exp.', 'surecart'), card === null || card === void 0 ? void 0 :
        card.exp_month, "/", card === null || card === void 0 ? void 0 :
        card.exp_year)), !!paypal_account && (paypal_account === null || paypal_account === void 0 ? void 0 : paypal_account.email)), subscription_payment_method_id === id && (h("sc-tag", { type: "info", slot: "price" }, __('Current Payment Method', 'surecart')))));
    });
  }
  render() {
    var _a, _b;
    return (h("sc-dashboard-module", { heading: this.heading || __('Update Payment Method', 'surecart'), class: "subscription", error: this.error }, ((_a = this.paymentMethods) === null || _a === void 0 ? void 0 : _a.length) > 1 && (h("sc-button", { slot: "end", type: "link", href: addQueryArgs(window.location.href, {
        action: 'create',
        model: 'payment_method',
        ...(((_b = this.subscription) === null || _b === void 0 ? void 0 : _b.live_mode) === false ? { live_mode: false } : {}),
        success_url: window.location.href,
      }) }, h("sc-icon", { name: "plus", slot: "prefix" }), __('Add New', 'surecart'))), this.renderContent(), this.busy && h("sc-block-ui", { spinner: true })));
  }
  get el() { return this; }
  static get style() { return scSubscriptionPaymentMethodCss; }
}, [1, "sc-subscription-payment-method", {
    "heading": [1],
    "subscription": [16],
    "paymentMethods": [32],
    "error": [32],
    "loading": [32],
    "busy": [32],
    "method": [32]
  }]);
function defineCustomElement$1() {
  if (typeof customElements === "undefined") {
    return;
  }
  const components = ["sc-subscription-payment-method", "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-payment-method", "sc-skeleton", "sc-spinner", "sc-stacked-list", "sc-stacked-list-row", "sc-tag", "sc-text", "sc-tooltip"];
  components.forEach(tagName => { switch (tagName) {
    case "sc-subscription-payment-method":
      if (!customElements.get(tagName)) {
        customElements.define(tagName, ScSubscriptionPaymentMethod$1);
      }
      break;
    case "sc-alert":
      if (!customElements.get(tagName)) {
        defineCustomElement$m();
      }
      break;
    case "sc-block-ui":
      if (!customElements.get(tagName)) {
        defineCustomElement$l();
      }
      break;
    case "sc-button":
      if (!customElements.get(tagName)) {
        defineCustomElement$k();
      }
      break;
    case "sc-card":
      if (!customElements.get(tagName)) {
        defineCustomElement$j();
      }
      break;
    case "sc-cc-logo":
      if (!customElements.get(tagName)) {
        defineCustomElement$i();
      }
      break;
    case "sc-choice":
      if (!customElements.get(tagName)) {
        defineCustomElement$h();
      }
      break;
    case "sc-choices":
      if (!customElements.get(tagName)) {
        defineCustomElement$g();
      }
      break;
    case "sc-dashboard-module":
      if (!customElements.get(tagName)) {
        defineCustomElement$f();
      }
      break;
    case "sc-empty":
      if (!customElements.get(tagName)) {
        defineCustomElement$e();
      }
      break;
    case "sc-flex":
      if (!customElements.get(tagName)) {
        defineCustomElement$d();
      }
      break;
    case "sc-form":
      if (!customElements.get(tagName)) {
        defineCustomElement$c();
      }
      break;
    case "sc-form-control":
      if (!customElements.get(tagName)) {
        defineCustomElement$b();
      }
      break;
    case "sc-icon":
      if (!customElements.get(tagName)) {
        defineCustomElement$a();
      }
      break;
    case "sc-payment-method":
      if (!customElements.get(tagName)) {
        defineCustomElement$9();
      }
      break;
    case "sc-skeleton":
      if (!customElements.get(tagName)) {
        defineCustomElement$8();
      }
      break;
    case "sc-spinner":
      if (!customElements.get(tagName)) {
        defineCustomElement$7();
      }
      break;
    case "sc-stacked-list":
      if (!customElements.get(tagName)) {
        defineCustomElement$6();
      }
      break;
    case "sc-stacked-list-row":
      if (!customElements.get(tagName)) {
        defineCustomElement$5();
      }
      break;
    case "sc-tag":
      if (!customElements.get(tagName)) {
        defineCustomElement$4();
      }
      break;
    case "sc-text":
      if (!customElements.get(tagName)) {
        defineCustomElement$3();
      }
      break;
    case "sc-tooltip":
      if (!customElements.get(tagName)) {
        defineCustomElement$2();
      }
      break;
  } });
}
defineCustomElement$1();

const ScSubscriptionPaymentMethod = ScSubscriptionPaymentMethod$1;
const defineCustomElement = defineCustomElement$1;

export { ScSubscriptionPaymentMethod, defineCustomElement };
