'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const index = require('./index-9c866aeb.js');
const defaultI18n = require('./default-i18n-d4248548.js');
require('./index-88baf240.js');
const fetch = require('./fetch-02d6cb7b.js');
const addQueryArgs = require('./add-query-args-17c551b6.js');
require('./index-04dc658f.js');

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

const ScMollieAddMethod = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.methods = [];
  }
  componentWillLoad() {
    this.fetchMethods();
  }
  async createPaymentIntent() {
    var _a, _b, _c, _d;
    try {
      this.loading = true;
      this.error = '';
      this.paymentIntent = await fetch.apiFetch({
        method: 'POST',
        path: 'surecart/v1/payment_intents',
        data: {
          processor_type: 'mollie',
          live_mode: this.liveMode,
          customer_id: this.customerId,
          return_url: this.successUrl,
          payment_method_type: this.selectedMethodId,
          currency: this.currency,
        },
      });
      console.log(this.paymentIntent);
      if ((_b = (_a = this.paymentIntent.processor_data) === null || _a === void 0 ? void 0 : _a.mollie) === null || _b === void 0 ? void 0 : _b.checkout_url) {
        window.location.assign((_d = (_c = this.paymentIntent.processor_data) === null || _c === void 0 ? void 0 : _c.mollie) === null || _d === void 0 ? void 0 : _d.checkout_url);
      }
    }
    catch (e) {
      console.error(e);
      this.error = (e === null || e === void 0 ? void 0 : e.message) || defaultI18n.__('Something went wrong', 'surecart');
      this.loading = false;
    }
  }
  async fetchMethods() {
    var _a, _b, _c;
    try {
      this.loading = true;
      const response = (await fetch.apiFetch({
        path: addQueryArgs.addQueryArgs(`surecart/v1/processors/${this.processorId}/payment_method_types`, {
          amount: 0,
          country: this.country,
          currency: this.currency,
          reusable: true,
        }),
      }));
      this.methods = (response === null || response === void 0 ? void 0 : response.data) || [];
      if ((_a = this.methods) === null || _a === void 0 ? void 0 : _a.length) {
        this.selectedMethodId = (_c = (_b = this.methods) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.id;
      }
    }
    catch (e) {
      this.error = (e === null || e === void 0 ? void 0 : e.message) || defaultI18n.__('Something went wrong', 'surecart');
      console.error(e);
    }
    finally {
      this.loading = false;
    }
  }
  handleSubmit() {
    this.createPaymentIntent();
  }
  renderLoading() {
    return (index.h("sc-card", null, index.h("sc-skeleton", { style: { width: '50%', marginBottom: '0.5em' } }), index.h("sc-skeleton", { style: { width: '30%', marginBottom: '0.5em' } }), index.h("sc-skeleton", { style: { width: '60%', marginBottom: '0.5em' } })));
  }
  render() {
    var _a;
    if (this.loading && !((_a = this.methods) === null || _a === void 0 ? void 0 : _a.length)) {
      return this.renderLoading();
    }
    return (index.h("sc-form", { onScFormSubmit: () => this.handleSubmit(), style: { position: 'relative' } }, index.h("sc-toggles", { collapsible: false, theme: "container" }, (this.methods || []).map(method => (index.h("sc-toggle", { "show-control": true, shady: true, borderless: true, open: this.selectedMethodId === (method === null || method === void 0 ? void 0 : method.id), onScShow: () => (this.selectedMethodId = method === null || method === void 0 ? void 0 : method.id) }, index.h("span", { slot: "summary", class: "sc-payment-toggle-summary" }, !!(method === null || method === void 0 ? void 0 : method.image) && index.h("img", { src: method === null || method === void 0 ? void 0 : method.image }), index.h("span", null, method === null || method === void 0 ? void 0 : method.description)), index.h("sc-card", null, index.h("sc-payment-selected", { label: defaultI18n.sprintf(defaultI18n.__('%s selected.', 'surecart'), method === null || method === void 0 ? void 0 : method.description) }, !!(method === null || method === void 0 ? void 0 : method.image) && index.h("img", { slot: "icon", src: method === null || method === void 0 ? void 0 : method.image, style: { width: '32px' } }), defaultI18n.__('Another step will appear after submitting your order to add this payment method.', 'surecart'))))))), index.h("sc-button", { type: "primary", submit: true, full: true, loading: this.loading }, defaultI18n.__('Add Payment Method', 'surecart')), this.loading && index.h("sc-block-ui", { "z-index": 9, style: { '--sc-block-ui-opacity': '0.75' } })));
  }
};
ScMollieAddMethod.style = scMollieAddMethodCss;

exports.sc_mollie_add_method = ScMollieAddMethod;
