import { r as registerInstance, c as createEvent, h, g as getElement } from './index-f1d2a96f.js';
import { c as countryChoices, h as hasState } from './address-faa4860c.js';
import { _ as __ } from './default-i18n-91ef32a0.js';
import './index-0a61debd.js';
import { r as reportChildrenValidity } from './form-data-192b931a.js';
import { o as openWormhole } from './consumer-7a54eb9f.js';
import './_commonjsHelpers-9943807e.js';

const scCompactAddressCss = ":host{display:block}.sc-address{display:block;position:relative}.sc-address [hidden]{display:none}.sc-address--loading{min-height:70px}.sc-address--loading sc-skeleton{display:block;margin-bottom:1em}.sc-address__control{display:block}.sc-address__control>*{margin-bottom:-1px}.sc-address__columns{display:flex;flex-direction:row;align-items:center;flex-wrap:wrap;justify-content:space-between}.sc-address__columns>*{flex:1;width:50%;margin-left:-1px}.sc-address__columns>*:first-child{margin-left:0}";

const ScCompactAddress = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.scChangeAddress = createEvent(this, "scChangeAddress", 7);
    this.scInputAddress = createEvent(this, "scInputAddress", 7);
    /** The address. */
    this.address = {
      country: null,
      city: null,
      line_1: null,
      line_2: null,
      postal_code: null,
      state: null,
    };
    this.names = {
      country: 'shipping_country',
      city: 'shipping_city',
      line_1: 'shipping_line_1',
      line_2: 'shipping_line_2',
      postal_code: 'shipping_postal_code',
      state: 'shipping_state',
    };
    /** Label for the address */
    this.label = __('Country or region', 'surecart');
    /** Holds our country choices. */
    this.countryChoices = countryChoices;
  }
  /** When the state changes, we want to update city and postal fields. */
  handleAddressChange() {
    if (!this.address.country)
      return;
    this.setRegions();
    this.showState = ['US', 'CA'].includes(this.address.country);
    this.showPostal = ['US'].includes(this.address.country);
    this.scChangeAddress.emit(this.address);
    this.scInputAddress.emit(this.address);
  }
  updateAddress(address) {
    this.address = { ...this.address, ...address };
  }
  handleAddressInput(address) {
    this.scInputAddress.emit({ ...this.address, ...address });
  }
  clearAddress() {
    this.address = {
      country: null,
      line_1: null,
      line_2: null,
      city: null,
      postal_code: null,
      state: null,
    };
  }
  /** Set the regions based on the country. */
  setRegions() {
    if (hasState(this.address.country)) {
      import('./countries-dfee7456.js').then(module => {
        this.regions = module === null || module === void 0 ? void 0 : module[this.address.country];
      });
    }
    else {
      this.regions = [];
    }
  }
  componentWillLoad() {
    var _a;
    this.handleAddressChange();
    const country = (_a = this.countryChoices.find(country => country.value === this.address.country)) === null || _a === void 0 ? void 0 : _a.value;
    if (country) {
      this.updateAddress({ country });
    }
  }
  async reportValidity() {
    return reportChildrenValidity(this.el);
  }
  render() {
    var _a, _b, _c;
    return (h("div", { class: "sc-address" }, h("sc-form-control", { label: this.label, class: "sc-address__control", part: "control", required: this.required }, h("sc-select", { value: (_a = this.address) === null || _a === void 0 ? void 0 : _a.country, onScChange: (e) => {
        this.clearAddress();
        this.updateAddress({ country: e.target.value || null });
      }, choices: this.countryChoices, autocomplete: 'country-name', placeholder: __('Select Your Country', 'surecart'), name: this.names.country, search: true, unselect: false, "squared-bottom": this.showState || this.showPostal, required: this.required }), h("div", { class: "sc-address__columns" }, this.showState && (h("sc-select", { placeholder: this.address.country === 'US' ? __('State', 'surecart') : __('Province/Region', 'surecart'), name: this.names.state, autocomplete: 'address-level1', value: (_b = this === null || this === void 0 ? void 0 : this.address) === null || _b === void 0 ? void 0 : _b.state, onScChange: (e) => this.updateAddress({ state: e.target.value || null }), choices: this.regions, required: this.required, search: true, "squared-top": true, unselect: false, "squared-right": this.showPostal })), this.showPostal && (h("sc-input", { placeholder: __('Postal Code/Zip', 'surecart'), name: this.names.postal_code, onScChange: (e) => this.updateAddress({ postal_code: e.target.value || null }), onScInput: (e) => this.handleAddressInput({ name: e.target.value || null }), autocomplete: 'postal-code', required: this.required, value: (_c = this === null || this === void 0 ? void 0 : this.address) === null || _c === void 0 ? void 0 : _c.postal_code, "squared-top": true, maxlength: 5, "squared-left": this.showState })))), this.loading && h("sc-block-ui", null)));
  }
  get el() { return getElement(this); }
  static get watchers() { return {
    "address": ["handleAddressChange"]
  }; }
};
ScCompactAddress.style = scCompactAddressCss;

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

const ScOrderShippingAddress = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.scUpdateOrder = createEvent(this, "scUpdateOrder", 7);
    /** Is this required (defaults to true) */
    this.required = true;
    /** 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 customer shipping address changes, we want to use that instead of what's entered, if we have empty fields. */
  handleCustomerAddressChange(val, old) {
    // if the shipping address is blank, use the customer address.
    if (!Object.keys(this.shippingAddress || {}).length && !old) {
      this.address = { ...this.address, ...val };
    }
  }
  /** When the shipping address changes, we want to update the passed address to match. */
  handleShippingChange(val, old) {
    var _a;
    // let's only update it the first time.
    if (!old) {
      if (!(val === null || val === void 0 ? void 0 : val.country)) {
        const country = (_a = navigator === null || navigator === void 0 ? void 0 : navigator.language) === null || _a === void 0 ? void 0 : _a.slice(-2).toUpperCase();
        if (country) {
          this.address = {
            ...this.address,
            country,
          };
        }
      }
      this.address = { ...this.address, ...val };
    }
  }
  updateAddressState(address) {
    if (JSON.stringify(address) === JSON.stringify(this.address))
      return; // no change, don't update.
    this.address = address;
    this.scUpdateOrder.emit({
      data: {
        shipping_address: this.address,
      },
    });
  }
  componentWillLoad() {
    var _a, _b;
    /** Set the country by browser language if not set. */
    if (!((_a = this.address) === null || _a === void 0 ? void 0 : _a.country)) {
      const country = (_b = navigator === null || navigator === void 0 ? void 0 : navigator.language) === null || _b === void 0 ? void 0 : _b.slice(-2).toUpperCase();
      if (country) {
        this.address = {
          ...this.address,
          country,
        };
      }
    }
  }
  async reportValidity() {
    return this.input.reportValidity();
  }
  render() {
    if (this.shippingEnabled || this.full) {
      return (h("sc-address", { ref: el => (this.input = el), label: this.label || __('Shipping Address', 'surecart'), placeholders: this.placeholders, 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, onScChangeAddress: e => this.updateAddressState(e.detail) }));
  }
  static get watchers() { return {
    "customerShippingAddress": ["handleCustomerAddressChange"],
    "shippingAddress": ["handleShippingChange"]
  }; }
};
openWormhole(ScOrderShippingAddress, ['shippingAddress', 'loading', 'customerShippingAddress', 'taxStatus', 'shippingEnabled'], false);
ScOrderShippingAddress.style = scOrderShippingAddressCss;

const scOrderTaxIdInputCss = ":host{display:block}";

const ScOrderTaxIdInput = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.scUpdateOrder = createEvent(this, "scUpdateOrder", 7);
    /** Force show the field. */
    this.show = false;
    /** Is this busy */
    this.busy = false;
  }
  getStatus() {
    var _a, _b, _c;
    if (((_a = this.taxIdentifier) === null || _a === void 0 ? void 0 : _a.number_type) !== 'eu_vat') {
      return 'unknown';
    }
    if (((_b = this.taxProtocol) === null || _b === void 0 ? void 0 : _b.eu_vat_unverified_behavior) === 'apply_reverse_charge') {
      return 'unknown';
    }
    return ((_c = this.taxIdentifier) === null || _c === void 0 ? void 0 : _c.eu_vat_verified) ? 'valid' : 'invalid';
  }
  maybeUpdateOrder(tax_identifier) {
    var _a;
    if (((_a = this.taxProtocol) === null || _a === void 0 ? void 0 : _a.eu_vat_unverified_behavior) === 'apply_reverse_charge') {
      return;
    }
    this.scUpdateOrder.emit({
      data: { tax_identifier },
    });
  }
  render() {
    var _a, _b, _c, _d, _e, _f;
    return (h("sc-tax-id-input", { show: this.show, number: (_b = (_a = this.order) === null || _a === void 0 ? void 0 : _a.tax_identifier) === null || _b === void 0 ? void 0 : _b.number, type: (_d = (_c = this.order) === null || _c === void 0 ? void 0 : _c.tax_identifier) === null || _d === void 0 ? void 0 : _d.number_type, country: (_f = (_e = this === null || this === void 0 ? void 0 : this.order) === null || _e === void 0 ? void 0 : _e.shipping_address) === null || _f === void 0 ? void 0 : _f.country, status: this.getStatus(), loading: this.busy, onScChange: e => {
        e.stopImmediatePropagation();
        this.maybeUpdateOrder(e.detail);
      } }));
  }
};
openWormhole(ScOrderTaxIdInput, ['draft', 'order', 'tax_status', 'taxIdentifier', 'taxProtocol', 'busy'], false);
ScOrderTaxIdInput.style = scOrderTaxIdInputCss;

export { ScCompactAddress as sc_compact_address, ScOrderShippingAddress as sc_order_shipping_address, ScOrderTaxIdInput as sc_order_tax_id_input };
