import { Component, Element, h, Prop, State } from '@stencil/core';
import { __ } from '@wordpress/i18n';
export class ScPaymentMethodCreate {
  constructor() {
    this.loading = false;
  }
  /**
   * Handle form submission.
   */
  async handleSubmit() {
    const element = this.el.querySelector('sc-stripe-payment-element');
    if (!element) {
      this.error = __('Something went wrong', 'surecart');
      return;
    }
    this.loading = true;
    try {
      await element.confirm('setup', {
        successUrl: this.successUrl,
      });
      if (this.successUrl) {
        window.location.assign(this.successUrl);
      }
      else {
        this.loading = false;
      }
    }
    catch (e) {
      this.error = (e === null || e === void 0 ? void 0 : e.message) || __('Something went wrong', 'surecart');
      this.loading = false;
    }
  }
  render() {
    return (h("sc-card", null,
      h("sc-form", { onScFormSubmit: () => this.handleSubmit() },
        this.error && (h("sc-alert", { open: !!this.error, type: "danger" },
          h("span", { slot: "title" }, __('Error', 'surecart')),
          this.error)),
        h("slot", null),
        h("sc-button", { type: "primary", submit: true, full: true, loading: this.loading }, __('Save Payment Method', 'surecart')),
        this.loading && h("sc-block-ui", null))));
  }
  static get is() { return "sc-payment-method-create"; }
  static get originalStyleUrls() { return {
    "$": ["payment-method-create.scss"]
  }; }
  static get styleUrls() { return {
    "$": ["payment-method-create.css"]
  }; }
  static get properties() { return {
    "error": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "error",
      "reflect": false
    },
    "clientSecret": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "client-secret",
      "reflect": false
    },
    "successUrl": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "success-url",
      "reflect": false
    }
  }; }
  static get states() { return {
    "loading": {}
  }; }
  static get elementRef() { return "el"; }
}
