import { Component, Prop, h, State } from '@stencil/core';
import { __ } from '@wordpress/i18n';
import apiFetch from '../../../../functions/fetch';
import { addQueryArgs } from '@wordpress/url';
export class ScCustomerEdit {
  async handleSubmit(e) {
    var _a;
    this.loading = true;
    try {
      const { email, name, phone, billing_matches_shipping, shipping_name, shipping_city, 'tax_identifier.number_type': tax_identifier_number_type, 'tax_identifier.number': tax_identifier_number, shipping_country, shipping_line_1, shipping_postal_code, shipping_state, billing_name, billing_city, billing_country, billing_line_1, billing_postal_code, billing_state, } = await e.target.getFormJson();
      await apiFetch({
        path: addQueryArgs(`surecart/v1/customers/${(_a = this.customer) === null || _a === void 0 ? void 0 : _a.id}`, { expand: ['tax_identifier'] }),
        method: 'PATCH',
        data: {
          email,
          name,
          phone,
          billing_matches_shipping: billing_matches_shipping === 'on',
          shipping_address: {
            name: shipping_name,
            city: shipping_city,
            country: shipping_country,
            line_1: shipping_line_1,
            postal_code: shipping_postal_code,
            state: shipping_state,
          },
          billing_address: {
            name: billing_name,
            city: billing_city,
            country: billing_country,
            line_1: billing_line_1,
            postal_code: billing_postal_code,
            state: billing_state,
          },
          ...(tax_identifier_number && tax_identifier_number_type
            ? {
              tax_identifier: {
                number: tax_identifier_number,
                number_type: tax_identifier_number_type,
              },
            }
            : {}),
        },
      });
      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, _e, _f, _g, _h, _j, _k, _l, _m;
    return (h("sc-dashboard-module", { class: "customer-edit", error: this.error },
      h("span", { slot: "heading" },
        this.heading || __('Update Billing Details', 'surecart'),
        ' ',
        !((_a = this === null || this === void 0 ? void 0 : this.customer) === null || _a === void 0 ? void 0 : _a.live_mode) && (h("sc-tag", { type: "warning", size: "small" }, __('Test', 'surecart')))),
      h("sc-card", null,
        h("sc-form", { onScFormSubmit: e => this.handleSubmit(e) },
          h("sc-input", { label: __('Billing Email', 'surecart'), name: "email", value: (_b = this.customer) === null || _b === void 0 ? void 0 : _b.email, required: true }),
          h("sc-columns", { style: { '--sc-column-spacing': 'var(--sc-spacing-medium)' } },
            h("sc-column", null,
              h("sc-input", { label: __('Name', 'surecart'), name: "name", value: (_c = this.customer) === null || _c === void 0 ? void 0 : _c.name })),
            h("sc-column", null,
              h("sc-input", { label: __('Phone', 'surecart'), name: "phone", value: (_d = this.customer) === null || _d === void 0 ? void 0 : _d.phone }))),
          h("div", null,
            h("sc-address", { label: __('Shipping Address', 'surecart'), showName: true, address: {
                ...(_e = this.customer) === null || _e === void 0 ? void 0 : _e.shipping_address,
              }, required: false, names: {
                name: 'shipping_name',
                country: 'shipping_country',
                line_1: 'shipping_line_1',
                line_2: 'shipping_line_2',
                city: 'shipping_city',
                postal_code: 'shipping_postal_code',
                state: 'shipping_state',
              } })),
          h("div", null,
            h("sc-switch", { name: "billing_matches_shipping", checked: (_f = this.customer) === null || _f === void 0 ? void 0 : _f.billing_matches_shipping, onScChange: e => {
                this.customer = {
                  ...this.customer,
                  billing_matches_shipping: e.target.checked,
                };
              }, value: "on" }, __('Billing address same as shipping', 'surecart'))),
          h("div", { style: { display: ((_g = this.customer) === null || _g === void 0 ? void 0 : _g.billing_matches_shipping) ? 'none' : 'block' } },
            h("sc-address", { label: __('Billing Address', 'surecart'), showName: true, address: {
                ...(_h = this.customer) === null || _h === void 0 ? void 0 : _h.billing_address,
              }, names: {
                name: 'billing_name',
                country: 'billing_country',
                line_1: 'billing_line_1',
                line_2: 'billing_line_2',
                city: 'billing_city',
                postal_code: 'billing_postal_code',
                state: 'billing_state',
              }, required: false })),
          h("sc-tax-id-input", { show: true, number: (_k = (_j = this.customer) === null || _j === void 0 ? void 0 : _j.tax_identifier) === null || _k === void 0 ? void 0 : _k.number, type: (_m = (_l = this.customer) === null || _l === void 0 ? void 0 : _l.tax_identifier) === null || _m === void 0 ? void 0 : _m.number_type }),
          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-customer-edit"; }
  static get encapsulation() { return "shadow"; }
  static get originalStyleUrls() { return {
    "$": ["sc-customer-edit.scss"]
  }; }
  static get styleUrls() { return {
    "$": ["sc-customer-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
    },
    "customer": {
      "type": "unknown",
      "mutable": true,
      "complexType": {
        "original": "Customer",
        "resolved": "Customer",
        "references": {
          "Customer": {
            "location": "import",
            "path": "../../../../types"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "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": {},
    "error": {}
  }; }
}
