import { Component, h, Prop, State } from '@stencil/core';
import { __ } from '@wordpress/i18n';
import apiFetch from '../../../../functions/fetch';
export class ScWordPressUserEdit {
  renderEmpty() {
    return h("slot", { name: "empty" }, __('User not found.', 'surecart'));
  }
  async handleSubmit(e) {
    this.loading = true;
    try {
      const { email, first_name, last_name, name } = await e.target.getFormJson();
      await apiFetch({
        path: `wp/v2/users/me`,
        method: 'PATCH',
        data: {
          first_name,
          last_name,
          email,
          name,
        },
      });
      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() {
    var _a, _b, _c, _d;
    return (h("sc-dashboard-module", { class: "account-details", error: this.error },
      h("span", { slot: "heading" },
        this.heading || __('Account Details', 'surecart'),
        " "),
      h("sc-card", null,
        h("sc-form", { onScFormSubmit: e => this.handleSubmit(e) },
          h("sc-input", { label: __('Billing Email', 'surecart'), name: "email", value: (_a = this.user) === null || _a === void 0 ? void 0 : _a.email, required: true }),
          h("sc-columns", { style: { '--sc-column-spacing': 'var(--sc-spacing-medium)' } },
            h("sc-column", null,
              h("sc-input", { label: __('First Name', 'surecart'), name: "first_name", value: (_b = this.user) === null || _b === void 0 ? void 0 : _b.first_name })),
            h("sc-column", null,
              h("sc-input", { label: __('Last Name', 'surecart'), name: "last_name", value: (_c = this.user) === null || _c === void 0 ? void 0 : _c.last_name }))),
          h("sc-input", { label: __('Display Name', 'surecart'), name: "name", value: (_d = this.user) === null || _d === void 0 ? void 0 : _d.display_name }),
          h("div", null,
            h("sc-button", { type: "primary", full: true, submit: true }, __('Save', 'surecart'))))),
      this.loading && h("sc-block-ui", { spinner: true })));
  }
  static get is() { return "sc-wordpress-user-edit"; }
  static get encapsulation() { return "shadow"; }
  static get originalStyleUrls() { return {
    "$": ["sc-wordpress-user-edit.css"]
  }; }
  static get styleUrls() { return {
    "$": ["sc-wordpress-user-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": {}
  }; }
}
