import { h, proxyCustomElement, HTMLElement, createEvent, Fragment } from '@stencil/core/internal/client';
import { s as sprintf, _ as __ } from './default-i18n.js';
import './index2.js';
import { s as state } from './watchers3.js';
import './watchers2.js';
import { s as state$1, c as availableMethodTypes, d as hasMultipleMethodChoices, b as availableManualPaymentMethods } from './getters.js';
import { b as on, a as state$2 } from './watchers.js';
import { a as checkoutIsLocked } from './getters2.js';
import { l as lockCheckout, u as unLockCheckout } from './mutations.js';
import { a as apiFetch } from './fetch.js';
import { d as defineCustomElement$9 } from './sc-alert2.js';
import { d as defineCustomElement$8 } from './sc-block-ui2.js';
import { d as defineCustomElement$7 } from './sc-card2.js';
import { d as defineCustomElement$6 } from './sc-divider2.js';
import { d as defineCustomElement$5 } from './sc-icon2.js';
import { d as defineCustomElement$4 } from './sc-payment-method-choice2.js';
import { d as defineCustomElement$3 } from './sc-payment-selected2.js';
import { d as defineCustomElement$2 } from './sc-skeleton2.js';
import { d as defineCustomElement$1 } from './sc-spinner2.js';
import { a as addQueryArgs } from './add-query-args.js';

const listenTo = (prop, propKey, callback) => on('set', (key, newValue, oldValue) => {
  // ignore non-keys
  if (key !== prop)
    return;
  // handle an array, if one has changed, run callback.
  if (Array.isArray(propKey)) {
    if (propKey.some(key => JSON.stringify(newValue === null || newValue === void 0 ? void 0 : newValue[key]) !== JSON.stringify(oldValue === null || oldValue === void 0 ? void 0 : oldValue[key]))) {
      return callback(newValue, oldValue);
    }
  }
  // handle string.
  if (typeof propKey === 'string') {
    if (JSON.stringify(newValue === null || newValue === void 0 ? void 0 : newValue[propKey]) === JSON.stringify(oldValue === null || oldValue === void 0 ? void 0 : oldValue[propKey]))
      return;
    return callback(newValue === null || newValue === void 0 ? void 0 : newValue[propKey], oldValue === null || oldValue === void 0 ? void 0 : oldValue[propKey]);
  }
});

const ManualPaymentMethods = ({ methods }) => (methods || []).map(method => (h("sc-payment-method-choice", { "is-manual": true, "processor-id": method === null || method === void 0 ? void 0 : method.id },
  h("span", { slot: "summary" }, method === null || method === void 0 ? void 0 : method.name),
  h("sc-card", null,
    h("sc-payment-selected", { label: sprintf(
      // translators: Manual payment method.
      __('%s selected for check out.', 'surecart'), method === null || method === void 0 ? void 0 : method.name) }, method === null || method === void 0 ? void 0 : method.description)))));

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

const ScCheckoutMolliePayment = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  constructor() {
    super();
    this.__registerHost();
    this.__attachShadow();
    this.scError = createEvent(this, "scError", 7);
  }
  componentWillLoad() {
    state.id = 'mollie';
    this.fetchMethods();
    listenTo('checkout', ['total_amount', 'currency', 'reusabled_payment_method_required', 'shipping_address'], () => this.fetchMethods());
  }
  async fetchMethods() {
    var _a;
    const checkout = state$2.checkout;
    if (!(checkout === null || checkout === void 0 ? void 0 : checkout.currency) || !(checkout === null || checkout === void 0 ? void 0 : checkout.total_amount))
      return; // wait until we have a currency.
    try {
      lockCheckout('methods');
      const response = (await apiFetch({
        path: addQueryArgs(`surecart/v1/processors/${this.processorId}/payment_method_types`, {
          amount: checkout === null || checkout === void 0 ? void 0 : checkout.total_amount,
          country: ((_a = checkout === null || checkout === void 0 ? void 0 : checkout.shipping_address) === null || _a === void 0 ? void 0 : _a.country) || 'us',
          currency: checkout === null || checkout === void 0 ? void 0 : checkout.currency,
          ...((checkout === null || checkout === void 0 ? void 0 : checkout.reusable_payment_method_required) ? { reusable: checkout === null || checkout === void 0 ? void 0 : checkout.reusable_payment_method_required } : {}),
          per_page: 100,
        }),
      }));
      state$1.methods = (response === null || response === void 0 ? void 0 : response.data) || [];
    }
    catch (e) {
      this.scError.emit(e);
      console.error(e);
    }
    finally {
      unLockCheckout('methods');
    }
  }
  renderLoading() {
    return (h("sc-card", null, h("sc-skeleton", { style: { width: '50%', marginBottom: '0.5em' } }), h("sc-skeleton", { style: { width: '30%', marginBottom: '0.5em' } }), h("sc-skeleton", { style: { width: '60%', marginBottom: '0.5em' } })));
  }
  render() {
    var _a, _b, _c;
    if (checkoutIsLocked('methods') && !((_a = availableMethodTypes()) === null || _a === void 0 ? void 0 : _a.length)) {
      return this.renderLoading();
    }
    if (!((_b = state$2.checkout) === null || _b === void 0 ? void 0 : _b.currency)) {
      return this.renderLoading();
    }
    if (!((_c = availableMethodTypes()) === null || _c === void 0 ? void 0 : _c.length)) {
      return (h("sc-alert", { type: "warning", open: true }, __('No available payment methods', 'surecart'), ' '));
    }
    const Tag = hasMultipleMethodChoices() ? 'sc-toggles' : 'div';
    return (h(Fragment, null, h(Tag, { collapsible: false, theme: "container" }, (availableMethodTypes() || []).map(method => (h("sc-payment-method-choice", { "processor-id": "mollie", "method-id": method === null || method === void 0 ? void 0 : method.id, key: method === null || method === void 0 ? void 0 : method.id }, h("span", { slot: "summary", class: "sc-payment-toggle-summary" }, !!(method === null || method === void 0 ? void 0 : method.image) && h("img", { src: method === null || method === void 0 ? void 0 : method.image }), h("span", null, method === null || method === void 0 ? void 0 : method.description)), h("sc-card", null, h("sc-payment-selected", { label: sprintf(__('%s selected for check out.', 'surecart'), method === null || method === void 0 ? void 0 : method.description) }, !!(method === null || method === void 0 ? void 0 : method.image) && h("img", { slot: "icon", src: method === null || method === void 0 ? void 0 : method.image, style: { width: '32px' } }), __('Another step will appear after submitting your order to complete your purchase details.', 'surecart')))))), h(ManualPaymentMethods, { methods: availableManualPaymentMethods() })), !!checkoutIsLocked('methods') && h("sc-block-ui", { class: "busy-block-ui", "z-index": 9, style: { '--sc-block-ui-opacity': '0.4' } })));
  }
  static get style() { return scCheckoutMolliePaymentCss; }
}, [1, "sc-checkout-mollie-payment", {
    "processorId": [1, "processor-id"],
    "method": [1],
    "error": [32],
    "methods": [32]
  }]);
function defineCustomElement() {
  if (typeof customElements === "undefined") {
    return;
  }
  const components = ["sc-checkout-mollie-payment", "sc-alert", "sc-block-ui", "sc-card", "sc-divider", "sc-icon", "sc-payment-method-choice", "sc-payment-selected", "sc-skeleton", "sc-spinner"];
  components.forEach(tagName => { switch (tagName) {
    case "sc-checkout-mollie-payment":
      if (!customElements.get(tagName)) {
        customElements.define(tagName, ScCheckoutMolliePayment);
      }
      break;
    case "sc-alert":
      if (!customElements.get(tagName)) {
        defineCustomElement$9();
      }
      break;
    case "sc-block-ui":
      if (!customElements.get(tagName)) {
        defineCustomElement$8();
      }
      break;
    case "sc-card":
      if (!customElements.get(tagName)) {
        defineCustomElement$7();
      }
      break;
    case "sc-divider":
      if (!customElements.get(tagName)) {
        defineCustomElement$6();
      }
      break;
    case "sc-icon":
      if (!customElements.get(tagName)) {
        defineCustomElement$5();
      }
      break;
    case "sc-payment-method-choice":
      if (!customElements.get(tagName)) {
        defineCustomElement$4();
      }
      break;
    case "sc-payment-selected":
      if (!customElements.get(tagName)) {
        defineCustomElement$3();
      }
      break;
    case "sc-skeleton":
      if (!customElements.get(tagName)) {
        defineCustomElement$2();
      }
      break;
    case "sc-spinner":
      if (!customElements.get(tagName)) {
        defineCustomElement$1();
      }
      break;
  } });
}
defineCustomElement();

export { ManualPaymentMethods as M, ScCheckoutMolliePayment as S, defineCustomElement as d };
