import { r as registerInstance, c as createEvent, h, F as Fragment } from './index-fd4790f6.js';
import { _ as __, s as sprintf } from './default-i18n-2e663329.js';
import './index-53ddb833.js';
import { s as state } from './watchers-e86a5dda.js';
import './watchers-5d458aa9.js';
import { s as state$1, c as availableMethodTypes, d as hasMultipleMethodChoices, b as availableManualPaymentMethods } from './getters-a2fa2242.js';
import { d as on, a as state$2 } from './watchers-0c79cf4e.js';
import { a as checkoutIsLocked } from './getters-edc6a0db.js';
import { l as lockCheckout, u as unLockCheckout } from './mutations-8030ef46.js';
import { a as apiFetch } from './fetch-460a8bc3.js';
import { M as ManualPaymentMethods } from './ManualPaymentMethods-c10c8151.js';
import { a as addQueryArgs } from './add-query-args-f4c5962b.js';
import './index-6db066f7.js';
import './index-26bbcef3.js';
import './util-c1d762c0.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 scCheckoutMolliePaymentCss = ":host{display:block}";

const ScCheckoutMolliePayment = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    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' } })));
  }
};
ScCheckoutMolliePayment.style = scCheckoutMolliePaymentCss;

export { ScCheckoutMolliePayment as sc_checkout_mollie_payment };
