import { Component, h, Prop, State } from '@stencil/core';
import { __ } from '@wordpress/i18n';
import apiFetch from '../../../../functions/fetch';
export class ScWordPressPasswordEdit {
  renderEmpty() {
    return h("slot", { name: "empty" }, __('User not found.', 'surecart'));
  }
  async handleSubmit(e) {
    this.loading = true;
    this.error = '';
    try {
      const { password, password_confirm } = await e.target.getFormJson();
      if (password !== password_confirm) {
        throw { message: __('Passwords do not match.', 'surecart') };
      }
      await apiFetch({
        path: `wp/v2/users/me`,
        method: 'PATCH',
        data: {
          password,
        },
      });
      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-dashboard-module", { class: "customer-details", error: this.error },
      h("span", { slot: "heading" },
        this.heading || __('Update Password', 'surecart'),
        " "),
      h("sc-card", null,
        h("sc-form", { onScFormSubmit: e => this.handleSubmit(e) },
          h("sc-input", { label: __('New Password', 'surecart'), name: "password", type: "password", required: true }),
          h("sc-input", { label: __('Confirm New Password', 'surecart'), name: "password_confirm", type: "password", required: true }),
          h("div", null,
            h("sc-button", { type: "primary", full: true, submit: true }, __('Update Password', 'surecart'))))),
      this.loading && h("sc-block-ui", { spinner: true })));
  }
  static get is() { return "sc-wordpress-password-edit"; }
  static get encapsulation() { return "shadow"; }
  static get originalStyleUrls() { return {
    "$": ["sc-wordpress-password-edit.css"]
  }; }
  static get styleUrls() { return {
    "$": ["sc-wordpress-password-edit.css"]
  }; }
  static get properties() { return {
    "heading": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "heading",
      "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
    },
    "user": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "WordPressUser",
        "resolved": "WordPressUser",
        "references": {
          "WordPressUser": {
            "location": "import",
            "path": "../../../../types"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    }
  }; }
  static get states() { return {
    "loading": {},
    "error": {}
  }; }
}
