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$o } from './sc-alert2.js';
import { d as defineCustomElement$n } from './sc-block-ui2.js';
import { d as defineCustomElement$m } from './sc-button2.js';
import { d as defineCustomElement$l } from './sc-card2.js';
import { d as defineCustomElement$k } from './sc-cc-logo2.js';
import { d as defineCustomElement$j } from './sc-dashboard-module2.js';
import { d as defineCustomElement$i } from './sc-dialog2.js';
import { d as defineCustomElement$h } from './sc-divider2.js';
import { d as defineCustomElement$g } from './dropdown.js';
import { d as defineCustomElement$f } from './sc-empty2.js';
import { d as defineCustomElement$e } from './sc-flex2.js';
import { d as defineCustomElement$d } from './sc-icon2.js';
import { d as defineCustomElement$c } from './sc-menu2.js';
import { d as defineCustomElement$b } from './sc-menu-item2.js';
import { d as defineCustomElement$a } from './sc-payment-method2.js';
import { d as defineCustomElement$9 } from './sc-skeleton2.js';
import { d as defineCustomElement$8 } from './sc-spinner2.js';
import { d as defineCustomElement$7 } from './sc-stacked-list2.js';
import { d as defineCustomElement$6 } from './sc-stacked-list-row2.js';
import { d as defineCustomElement$5 } from './sc-switch2.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 scPaymentMethodsListCss = ":host{display:block;position:relative}.payment-methods-list{display:grid;gap:0.5em}.payment-methods-list sc-heading a{text-decoration:none;font-weight:var(--sc-font-weight-semibold);display:inline-flex;align-items:center;gap:0.25em;color:var(--sc-color-primary-500)}.payment-id{overflow:hidden;white-space:nowrap;text-overflow:ellipsis}";

const ScPaymentMethodsList$1 = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  constructor() {
    super();
    this.__registerHost();
    this.__attachShadow();
    /** Loaded payment methods */
    this.paymentMethods = [];
    /** Stores the currently selected payment method for editing */
    this.editPaymentMethod = false;
    /** Stores the currently selected payment method for editing */
    this.deletePaymentMethod = false;
    /** Whether to cascade default payment method */
    this.cascadeDefaultPaymentMethod = false;
  }
  /** Only fetch if visible */
  componentWillLoad() {
    onFirstVisible(this.el, () => this.getPaymentMethods());
    this.handleSlotChange();
  }
  handleSlotChange() {
    this.hasTitleSlot = !!this.el.querySelector('[slot="title"]');
  }
  /**
   * Delete the payment method.
   */
  async deleteMethod() {
    var _a;
    if (!this.deletePaymentMethod)
      return;
    try {
      this.busy = true;
      (await apiFetch({
        path: `surecart/v1/payment_methods/${(_a = this.deletePaymentMethod) === null || _a === void 0 ? void 0 : _a.id}/detach`,
        method: 'PATCH',
      }));
      // remove from view.
      this.paymentMethods = this.paymentMethods.filter(m => { var _a; return m.id !== ((_a = this.deletePaymentMethod) === null || _a === void 0 ? void 0 : _a.id); });
    }
    catch (e) {
      alert((e === null || e === void 0 ? void 0 : e.messsage) || __('Something went wrong', 'surecart'));
    }
    finally {
      this.busy = false;
    }
  }
  /**
   * Set the default payment method.
   */
  async setDefault() {
    var _a, _b, _c;
    if (!this.editPaymentMethod)
      return;
    try {
      this.error = '';
      this.busy = true;
      (await apiFetch({
        path: `surecart/v1/customers/${(_b = (_a = this.editPaymentMethod) === null || _a === void 0 ? void 0 : _a.customer) === null || _b === void 0 ? void 0 : _b.id}`,
        method: 'PATCH',
        data: {
          default_payment_method: (_c = this.editPaymentMethod) === null || _c === void 0 ? void 0 : _c.id,
          cascade_default_payment_method: this.cascadeDefaultPaymentMethod,
        },
      }));
      this.editPaymentMethod = false;
    }
    catch (e) {
      this.error = (e === null || e === void 0 ? void 0 : e.message) || __('Something went wrong', 'surecart');
    }
    finally {
      this.busy = false;
    }
    try {
      this.busy = true;
      this.paymentMethods = (await apiFetch({
        path: addQueryArgs(`surecart/v1/payment_methods/`, {
          expand: ['card', 'customer', 'billing_agreement', 'paypal_account', 'payment_instrument', 'bank_account'],
          ...this.query,
        }),
      }));
    }
    catch (e) {
      this.error = (e === null || e === void 0 ? void 0 : e.message) || __('Something went wrong', 'surecart');
    }
    finally {
      this.busy = false;
    }
  }
  /** Get all paymentMethods */
  async getPaymentMethods() {
    if (!this.isCustomer) {
      return;
    }
    try {
      this.loading = true;
      this.paymentMethods = (await apiFetch({
        path: addQueryArgs(`surecart/v1/payment_methods/`, {
          expand: ['card', 'customer', 'billing_agreement', 'paypal_account', 'payment_instrument', 'bank_account'],
          ...this.query,
          per_page: 100,
        }),
      }));
    }
    catch (e) {
      console.error(this.error);
      this.error = (e === null || e === void 0 ? void 0 : e.message) || __('Something went wrong', 'surecart');
    }
    finally {
      this.loading = false;
    }
  }
  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("div", null, h("sc-divider", { style: { '--spacing': '0' } }), h("slot", { name: "empty" }, h("sc-empty", { icon: "credit-card" }, __("You don't have any saved payment methods.", 'surecart')))));
  }
  renderList() {
    return this.paymentMethods.map(paymentMethod => {
      const { id, card, customer, live_mode, billing_agreement, paypal_account } = paymentMethod;
      return (h("sc-stacked-list-row", { style: { '--columns': billing_agreement ? '2' : '3' } }, h("sc-payment-method", { paymentMethod: paymentMethod }), h("div", { class: "payment-id" }, !!(card === null || card === void 0 ? void 0 : card.exp_month) && (h("span", null, __('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)), h("sc-flex", { "justify-content": "flex-start", "align-items": "center", style: { '--spacing': '0.5em', 'marginLeft': 'auto' } }, typeof customer !== 'string' && (customer === null || customer === void 0 ? void 0 : customer.default_payment_method) === id && h("sc-tag", { type: "info" }, __('Default', 'surecart')), !live_mode && h("sc-tag", { type: "warning" }, __('Test', 'surecart'))), h("sc-dropdown", { placement: "bottom-end", slot: "suffix" }, h("sc-icon", { name: "more-horizontal", slot: "trigger" }), h("sc-menu", null, typeof customer !== 'string' && (customer === null || customer === void 0 ? void 0 : customer.default_payment_method) !== id && (h("sc-menu-item", { onClick: () => (this.editPaymentMethod = paymentMethod) }, __('Make Default', 'surecart'))), h("sc-menu-item", { onClick: () => (this.deletePaymentMethod = paymentMethod) }, __('Delete', 'surecart'))))));
    });
  }
  renderContent() {
    var _a;
    if (!this.isCustomer) {
      return this.renderEmpty();
    }
    if (this.loading) {
      return this.renderLoading();
    }
    if (((_a = this.paymentMethods) === null || _a === void 0 ? void 0 : _a.length) === 0) {
      return this.renderEmpty();
    }
    return (h("sc-card", { "no-padding": true }, h("sc-stacked-list", null, this.renderList())));
  }
  handleEditPaymentMethodChange() {
    // reset when payment method edit changes
    this.cascadeDefaultPaymentMethod = false;
  }
  render() {
    return (h("sc-dashboard-module", { class: "payment-methods-list", error: this.error }, h("span", { slot: "heading" }, h("slot", { name: "heading" }, this.heading || __('Payment Methods', 'surecart'))), this.isCustomer && (h("sc-flex", { slot: "end" }, h("sc-button", { type: "link", href: addQueryArgs(window.location.href, {
        action: 'index',
        model: 'charge',
      }) }, h("sc-icon", { name: "clock", slot: "prefix" }), __('Payment History', 'surecart')), h("sc-button", { type: "link", href: addQueryArgs(window.location.href, {
        action: 'create',
        model: 'payment_method',
      }) }, h("sc-icon", { name: "plus", slot: "prefix" }), __('Add', 'surecart')))), this.renderContent(), h("sc-dialog", { open: !!this.editPaymentMethod, label: __('Update Default Payment Method', 'surecart'), onScRequestClose: () => (this.editPaymentMethod = false) }, h("sc-alert", { type: "danger", open: !!this.error }, this.error), h("sc-flex", { flexDirection: "column", style: { '--sc-flex-column-gap': 'var(--sc-spacing-small)' } }, h("sc-alert", { type: "info", open: true }, __('A default payment method will be used as a fallback in case other payment methods get removed from a subscription.', 'surecart')), h("sc-switch", { checked: this.cascadeDefaultPaymentMethod, onScChange: e => (this.cascadeDefaultPaymentMethod = e.target.checked) }, __('Update All Subscriptions', 'surecart'), h("span", { slot: "description" }, __('Update all existing subscriptions to use this payment method', 'surecart')))), h("div", { slot: "footer" }, h("sc-button", { type: "text", onClick: () => (this.editPaymentMethod = false) }, __('Cancel', 'surecart')), h("sc-button", { type: "primary", onClick: () => this.setDefault() }, __('Make Default', 'surecart'))), this.busy && h("sc-block-ui", { spinner: true })), h("sc-dialog", { open: !!this.deletePaymentMethod, label: __('Delete Payment Method', 'surecart'), onScRequestClose: () => (this.deletePaymentMethod = false) }, h("sc-alert", { type: "danger", open: !!this.error }, this.error), h("sc-text", null, __('Are you sure you want to remove this payment method?', 'surecart')), h("div", { slot: "footer" }, h("sc-button", { type: "text", onClick: () => (this.deletePaymentMethod = false) }, __('Cancel', 'surecart')), h("sc-button", { type: "primary", onClick: () => this.deleteMethod() }, __('Delete', 'surecart'))), this.busy && h("sc-block-ui", { spinner: true })), this.busy && h("sc-block-ui", { spinner: true })));
  }
  get el() { return this; }
  static get watchers() { return {
    "editPaymentMethod": ["handleEditPaymentMethodChange"]
  }; }
  static get style() { return scPaymentMethodsListCss; }
}, [1, "sc-payment-methods-list", {
    "query": [16],
    "heading": [1],
    "isCustomer": [4, "is-customer"],
    "paymentMethods": [32],
    "loading": [32],
    "busy": [32],
    "error": [32],
    "hasTitleSlot": [32],
    "editPaymentMethod": [32],
    "deletePaymentMethod": [32],
    "cascadeDefaultPaymentMethod": [32]
  }]);
function defineCustomElement$1() {
  if (typeof customElements === "undefined") {
    return;
  }
  const components = ["sc-payment-methods-list", "sc-alert", "sc-block-ui", "sc-button", "sc-card", "sc-cc-logo", "sc-dashboard-module", "sc-dialog", "sc-divider", "sc-dropdown", "sc-empty", "sc-flex", "sc-icon", "sc-menu", "sc-menu-item", "sc-payment-method", "sc-skeleton", "sc-spinner", "sc-stacked-list", "sc-stacked-list-row", "sc-switch", "sc-tag", "sc-text", "sc-tooltip"];
  components.forEach(tagName => { switch (tagName) {
    case "sc-payment-methods-list":
      if (!customElements.get(tagName)) {
        customElements.define(tagName, ScPaymentMethodsList$1);
      }
      break;
    case "sc-alert":
      if (!customElements.get(tagName)) {
        defineCustomElement$o();
      }
      break;
    case "sc-block-ui":
      if (!customElements.get(tagName)) {
        defineCustomElement$n();
      }
      break;
    case "sc-button":
      if (!customElements.get(tagName)) {
        defineCustomElement$m();
      }
      break;
    case "sc-card":
      if (!customElements.get(tagName)) {
        defineCustomElement$l();
      }
      break;
    case "sc-cc-logo":
      if (!customElements.get(tagName)) {
        defineCustomElement$k();
      }
      break;
    case "sc-dashboard-module":
      if (!customElements.get(tagName)) {
        defineCustomElement$j();
      }
      break;
    case "sc-dialog":
      if (!customElements.get(tagName)) {
        defineCustomElement$i();
      }
      break;
    case "sc-divider":
      if (!customElements.get(tagName)) {
        defineCustomElement$h();
      }
      break;
    case "sc-dropdown":
      if (!customElements.get(tagName)) {
        defineCustomElement$g();
      }
      break;
    case "sc-empty":
      if (!customElements.get(tagName)) {
        defineCustomElement$f();
      }
      break;
    case "sc-flex":
      if (!customElements.get(tagName)) {
        defineCustomElement$e();
      }
      break;
    case "sc-icon":
      if (!customElements.get(tagName)) {
        defineCustomElement$d();
      }
      break;
    case "sc-menu":
      if (!customElements.get(tagName)) {
        defineCustomElement$c();
      }
      break;
    case "sc-menu-item":
      if (!customElements.get(tagName)) {
        defineCustomElement$b();
      }
      break;
    case "sc-payment-method":
      if (!customElements.get(tagName)) {
        defineCustomElement$a();
      }
      break;
    case "sc-skeleton":
      if (!customElements.get(tagName)) {
        defineCustomElement$9();
      }
      break;
    case "sc-spinner":
      if (!customElements.get(tagName)) {
        defineCustomElement$8();
      }
      break;
    case "sc-stacked-list":
      if (!customElements.get(tagName)) {
        defineCustomElement$7();
      }
      break;
    case "sc-stacked-list-row":
      if (!customElements.get(tagName)) {
        defineCustomElement$6();
      }
      break;
    case "sc-switch":
      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 ScPaymentMethodsList = ScPaymentMethodsList$1;
const defineCustomElement = defineCustomElement$1;

export { ScPaymentMethodsList, defineCustomElement };
