import { Component, Element, h, Prop, State } from '@stencil/core';
import { __ } from '@wordpress/i18n';
import { addQueryArgs } from '@wordpress/url';
import apiFetch from '../../../../functions/fetch';
import { onFirstVisible } from '../../../../functions/lazy';
export class ScDashboardCustomerDetails {
  componentWillLoad() {
    onFirstVisible(this.el, () => {
      this.fetch();
    });
  }
  async fetch() {
    try {
      this.loading = true;
      this.customer = (await await apiFetch({
        path: addQueryArgs(`surecart/v1/customers/${this.customerId}`, {
          expand: ['shipping_address', 'billing_address', 'tax_identifier'],
        }),
      }));
    }
    catch (e) {
      if (e === null || e === void 0 ? void 0 : e.message) {
        this.error = e.message;
      }
      else {
        this.error = __('Something went wrong', 'surecart');
      }
      console.error(this.error);
    }
    finally {
      this.loading = false;
    }
  }
  render() {
    return (h("sc-customer-details", { customer: this.customer, loading: this.loading, error: this.error, "edit-link": addQueryArgs(window.location.href, {
        action: 'edit',
        model: 'customer',
        id: this.customerId,
      }) }));
  }
  static get is() { return "sc-dashboard-customer-details"; }
  static get encapsulation() { return "shadow"; }
  static get originalStyleUrls() { return {
    "$": ["sc-dashboard-customer-details.css"]
  }; }
  static get styleUrls() { return {
    "$": ["sc-dashboard-customer-details.css"]
  }; }
  static get properties() { return {
    "customerId": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "customer-id",
      "reflect": false
    },
    "heading": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "heading",
      "reflect": false
    }
  }; }
  static get states() { return {
    "customer": {},
    "loading": {},
    "error": {}
  }; }
  static get elementRef() { return "el"; }
}
