import { proxyCustomElement, HTMLElement, h } from '@stencil/core/internal/client';
import { _ as __ } from './default-i18n.js';
import './index2.js';
import { a as apiFetch } from './fetch.js';
import { d as defineCustomElement$n } from './sc-address2.js';
import { d as defineCustomElement$m } from './sc-alert2.js';
import { d as defineCustomElement$l } from './sc-block-ui2.js';
import { d as defineCustomElement$k } from './sc-button2.js';
import { d as defineCustomElement$j } from './sc-card2.js';
import { d as defineCustomElement$i } from './sc-column2.js';
import { d as defineCustomElement$h } from './sc-columns2.js';
import { d as defineCustomElement$g } from './sc-dashboard-module2.js';
import { d as defineCustomElement$f } from './dropdown.js';
import { d as defineCustomElement$e } from './sc-form2.js';
import { d as defineCustomElement$d } from './sc-form-control2.js';
import { d as defineCustomElement$c } from './sc-icon2.js';
import { d as defineCustomElement$b } from './sc-input2.js';
import { d as defineCustomElement$a } from './sc-menu2.js';
import { d as defineCustomElement$9 } from './sc-menu-item2.js';
import { d as defineCustomElement$8 } from './sc-menu-label2.js';
import { d as defineCustomElement$7 } from './sc-select2.js';
import { d as defineCustomElement$6 } from './sc-spinner2.js';
import { d as defineCustomElement$5 } from './sc-switch2.js';
import { d as defineCustomElement$4 } from './sc-tag2.js';
import { d as defineCustomElement$3 } from './sc-tax-id-input2.js';
import { d as defineCustomElement$2 } from './sc-tooltip2.js';
import { a as addQueryArgs } from './add-query-args.js';

const scCustomerEditCss = ":host{display:block;position:relative}.customer-edit{display:grid;gap:0.75em}";

const ScCustomerEdit$1 = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  constructor() {
    super();
    this.__registerHost();
    this.__attachShadow();
  }
  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 style() { return scCustomerEditCss; }
}, [1, "sc-customer-edit", {
    "heading": [1],
    "customer": [1040],
    "successUrl": [1, "success-url"],
    "loading": [32],
    "error": [32]
  }]);
function defineCustomElement$1() {
  if (typeof customElements === "undefined") {
    return;
  }
  const components = ["sc-customer-edit", "sc-address", "sc-alert", "sc-block-ui", "sc-button", "sc-card", "sc-column", "sc-columns", "sc-dashboard-module", "sc-dropdown", "sc-form", "sc-form-control", "sc-icon", "sc-input", "sc-menu", "sc-menu-item", "sc-menu-label", "sc-select", "sc-spinner", "sc-switch", "sc-tag", "sc-tax-id-input", "sc-tooltip"];
  components.forEach(tagName => { switch (tagName) {
    case "sc-customer-edit":
      if (!customElements.get(tagName)) {
        customElements.define(tagName, ScCustomerEdit$1);
      }
      break;
    case "sc-address":
      if (!customElements.get(tagName)) {
        defineCustomElement$n();
      }
      break;
    case "sc-alert":
      if (!customElements.get(tagName)) {
        defineCustomElement$m();
      }
      break;
    case "sc-block-ui":
      if (!customElements.get(tagName)) {
        defineCustomElement$l();
      }
      break;
    case "sc-button":
      if (!customElements.get(tagName)) {
        defineCustomElement$k();
      }
      break;
    case "sc-card":
      if (!customElements.get(tagName)) {
        defineCustomElement$j();
      }
      break;
    case "sc-column":
      if (!customElements.get(tagName)) {
        defineCustomElement$i();
      }
      break;
    case "sc-columns":
      if (!customElements.get(tagName)) {
        defineCustomElement$h();
      }
      break;
    case "sc-dashboard-module":
      if (!customElements.get(tagName)) {
        defineCustomElement$g();
      }
      break;
    case "sc-dropdown":
      if (!customElements.get(tagName)) {
        defineCustomElement$f();
      }
      break;
    case "sc-form":
      if (!customElements.get(tagName)) {
        defineCustomElement$e();
      }
      break;
    case "sc-form-control":
      if (!customElements.get(tagName)) {
        defineCustomElement$d();
      }
      break;
    case "sc-icon":
      if (!customElements.get(tagName)) {
        defineCustomElement$c();
      }
      break;
    case "sc-input":
      if (!customElements.get(tagName)) {
        defineCustomElement$b();
      }
      break;
    case "sc-menu":
      if (!customElements.get(tagName)) {
        defineCustomElement$a();
      }
      break;
    case "sc-menu-item":
      if (!customElements.get(tagName)) {
        defineCustomElement$9();
      }
      break;
    case "sc-menu-label":
      if (!customElements.get(tagName)) {
        defineCustomElement$8();
      }
      break;
    case "sc-select":
      if (!customElements.get(tagName)) {
        defineCustomElement$7();
      }
      break;
    case "sc-spinner":
      if (!customElements.get(tagName)) {
        defineCustomElement$6();
      }
      break;
    case "sc-switch":
      if (!customElements.get(tagName)) {
        defineCustomElement$5();
      }
      break;
    case "sc-tag":
      if (!customElements.get(tagName)) {
        defineCustomElement$4();
      }
      break;
    case "sc-tax-id-input":
      if (!customElements.get(tagName)) {
        defineCustomElement$3();
      }
      break;
    case "sc-tooltip":
      if (!customElements.get(tagName)) {
        defineCustomElement$2();
      }
      break;
  } });
}
defineCustomElement$1();

const ScCustomerEdit = ScCustomerEdit$1;
const defineCustomElement = defineCustomElement$1;

export { ScCustomerEdit, defineCustomElement };
