import { proxyCustomElement, HTMLElement, createEvent, h } from '@stencil/core/internal/client';
import { a as state } from './watchers.js';
import { l as lockCheckout, u as unLockCheckout } from './mutations.js';
import { _ as __ } from './default-i18n.js';
import './index2.js';
import { c as createOrUpdateCheckout } from './index4.js';
import { o as openWormhole } from './consumer.js';
import { d as defineCustomElement$d } from './sc-address2.js';
import { d as defineCustomElement$c } from './sc-block-ui2.js';
import { d as defineCustomElement$b } from './sc-compact-address2.js';
import { d as defineCustomElement$a } from './dropdown.js';
import { d as defineCustomElement$9 } from './sc-form-control2.js';
import { d as defineCustomElement$8 } from './sc-icon2.js';
import { d as defineCustomElement$7 } from './sc-input2.js';
import { d as defineCustomElement$6 } from './sc-menu2.js';
import { d as defineCustomElement$5 } from './sc-menu-item2.js';
import { d as defineCustomElement$4 } from './sc-menu-label2.js';
import { d as defineCustomElement$3 } from './sc-select2.js';
import { d as defineCustomElement$2 } from './sc-spinner2.js';
import { d as defineCustomElement$1 } from './sc-tooltip2.js';

const scOrderShippingAddressCss = ":host{display:block}.sc-order-shipping__loading{display:flex;flex-direction:column;gap:0.5em}";

const ScOrderShippingAddress = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  constructor() {
    super();
    this.__registerHost();
    this.__attachShadow();
    this.scUpdateOrder = createEvent(this, "scUpdateOrder", 7);
    /** Is this required (defaults to false) */
    this.required = false;
    /** Show the placeholder fields. */
    this.namePlaceholder = __('Name or Company Name', 'surecart');
    this.countryPlaceholder = __('Country', 'surecart');
    this.cityPlaceholder = __('City', 'surecart');
    this.line1Placeholder = __('Address', 'surecart');
    this.line2Placeholder = __('Address Line 2', 'surecart');
    this.postalCodePlaceholder = __('Postal Code/Zip', 'surecart');
    this.statePlaceholder = __('State/Province/Region', 'surecart');
    /** Placeholder values. */
    this.placeholders = {
      name: __('Name or Company Name', 'surecart'),
      country: __('Country', 'surecart'),
      city: __('City', 'surecart'),
      line_1: __('Address', 'surecart'),
      line_2: __('Address Line 2', 'surecart'),
      postal_code: __('Postal Code/Zip', 'surecart'),
      state: __('State/Province/Region', 'surecart'),
    };
    /** Address to pass to the component */
    this.address = {
      country: null,
      city: null,
      line_1: null,
      line_2: null,
      postal_code: null,
      state: null,
    };
  }
  /** When the shipping address changes, we want to use that instead of what's entered, if we have empty fields. */
  handleCustomerAddressChange(val, old) {
    if ((val === null || val === void 0 ? void 0 : val.id) && !old) {
      this.address = { ...this.address, ...val };
    }
  }
  async updateAddressState(address) {
    if (JSON.stringify(address) === JSON.stringify(this.address))
      return; // no change, don't update.
    this.address = address;
    try {
      lockCheckout('shipping-address');
      state.checkout = (await createOrUpdateCheckout({
        id: state.checkout.id,
        data: {
          shipping_address: this.address,
        },
      }));
    }
    catch (e) {
      console.error(e);
    }
    finally {
      unLockCheckout('shipping-address');
    }
  }
  async reportValidity() {
    return this.input.reportValidity();
  }
  componentWillLoad() {
    if (this.defaultCountry && !this.address.country) {
      this.address.country = this.defaultCountry;
    }
    this.handleRequirementChange();
  }
  handleRequirementChange() {
    if (this.shippingEnabled || this.taxEnabled) {
      this.required = true;
    }
  }
  render() {
    if (this.shippingEnabled || this.full) {
      return (h("sc-address", { exportparts: "label, help-text, form-control, input__base, select__base, columns, search__base, menu__base", ref: el => (this.input = el), label: this.label || __('Shipping Address', 'surecart'), placeholders: {
          name: this.namePlaceholder,
          country: this.countryPlaceholder,
          city: this.cityPlaceholder,
          line_1: this.line1Placeholder,
          line_2: this.line2Placeholder,
          postal_code: this.postalCodePlaceholder,
          state: this.statePlaceholder,
        }, required: this.required, loading: this.loading, address: this.address, "show-name": this.showName, onScChangeAddress: e => this.updateAddressState(e.detail) }));
    }
    return (h("sc-compact-address", { ref: el => (this.input = el), required: this.required, loading: this.loading, address: this.address, placeholders: {
        name: this.namePlaceholder,
        country: this.countryPlaceholder,
        city: this.cityPlaceholder,
        line_1: this.line1Placeholder,
        line_2: this.line2Placeholder,
        postal_code: this.postalCodePlaceholder,
        state: this.statePlaceholder,
      }, label: this.label, onScChangeAddress: e => this.updateAddressState(e.detail) }));
  }
  static get watchers() { return {
    "shippingAddress": ["handleCustomerAddressChange"],
    "shippingEnabled": ["handleRequirementChange"],
    "taxEnabled": ["handleRequirementChange"]
  }; }
  static get style() { return scOrderShippingAddressCss; }
}, [1, "sc-order-shipping-address", {
    "label": [1],
    "required": [1028],
    "loading": [4],
    "shippingAddress": [16],
    "taxStatus": [1, "tax-status"],
    "taxEnabled": [4, "tax-enabled"],
    "shippingEnabled": [4, "shipping-enabled"],
    "full": [4],
    "showName": [4, "show-name"],
    "namePlaceholder": [1, "name-placeholder"],
    "countryPlaceholder": [1, "country-placeholder"],
    "cityPlaceholder": [1, "city-placeholder"],
    "line1Placeholder": [1, "line-1-placeholder"],
    "line2Placeholder": [1, "line-2-placeholder"],
    "postalCodePlaceholder": [1, "postal-code-placeholder"],
    "statePlaceholder": [1, "state-placeholder"],
    "defaultCountry": [1, "default-country"],
    "placeholders": [16],
    "address": [32],
    "reportValidity": [64]
  }]);
openWormhole(ScOrderShippingAddress, ['shippingAddress', 'loading', 'taxStatus', 'taxEnabled', 'shippingEnabled'], false);
function defineCustomElement() {
  if (typeof customElements === "undefined") {
    return;
  }
  const components = ["sc-order-shipping-address", "sc-address", "sc-block-ui", "sc-compact-address", "sc-dropdown", "sc-form-control", "sc-icon", "sc-input", "sc-menu", "sc-menu-item", "sc-menu-label", "sc-select", "sc-spinner", "sc-tooltip"];
  components.forEach(tagName => { switch (tagName) {
    case "sc-order-shipping-address":
      if (!customElements.get(tagName)) {
        customElements.define(tagName, ScOrderShippingAddress);
      }
      break;
    case "sc-address":
      if (!customElements.get(tagName)) {
        defineCustomElement$d();
      }
      break;
    case "sc-block-ui":
      if (!customElements.get(tagName)) {
        defineCustomElement$c();
      }
      break;
    case "sc-compact-address":
      if (!customElements.get(tagName)) {
        defineCustomElement$b();
      }
      break;
    case "sc-dropdown":
      if (!customElements.get(tagName)) {
        defineCustomElement$a();
      }
      break;
    case "sc-form-control":
      if (!customElements.get(tagName)) {
        defineCustomElement$9();
      }
      break;
    case "sc-icon":
      if (!customElements.get(tagName)) {
        defineCustomElement$8();
      }
      break;
    case "sc-input":
      if (!customElements.get(tagName)) {
        defineCustomElement$7();
      }
      break;
    case "sc-menu":
      if (!customElements.get(tagName)) {
        defineCustomElement$6();
      }
      break;
    case "sc-menu-item":
      if (!customElements.get(tagName)) {
        defineCustomElement$5();
      }
      break;
    case "sc-menu-label":
      if (!customElements.get(tagName)) {
        defineCustomElement$4();
      }
      break;
    case "sc-select":
      if (!customElements.get(tagName)) {
        defineCustomElement$3();
      }
      break;
    case "sc-spinner":
      if (!customElements.get(tagName)) {
        defineCustomElement$2();
      }
      break;
    case "sc-tooltip":
      if (!customElements.get(tagName)) {
        defineCustomElement$1();
      }
      break;
  } });
}
defineCustomElement();

export { ScOrderShippingAddress as S, defineCustomElement as d };
