import { Component, Prop, h, Watch, State, Event, Method } from '@stencil/core';
import { __ } from '@wordpress/i18n';
import { openWormhole } from 'stencil-wormhole';
export class ScOrderShippingAddress {
  constructor() {
    /** 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 is() { return "sc-order-shipping-address"; }
  static get encapsulation() { return "shadow"; }
  static get originalStyleUrls() { return {
    "$": ["sc-order-shipping-address.scss"]
  }; }
  static get styleUrls() { return {
    "$": ["sc-order-shipping-address.css"]
  }; }
  static get properties() { return {
    "label": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "Label for the field."
      },
      "attribute": "label",
      "reflect": false
    },
    "required": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "Is this required (defaults to true)"
      },
      "attribute": "required",
      "reflect": false,
      "defaultValue": "true"
    },
    "loading": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "Is this loading."
      },
      "attribute": "loading",
      "reflect": false
    },
    "customerShippingAddress": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "Address",
        "resolved": "Address",
        "references": {
          "Address": {
            "location": "import",
            "path": "../../../../types"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "Holds the customer's shipping address"
      }
    },
    "shippingAddress": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "Address",
        "resolved": "Address",
        "references": {
          "Address": {
            "location": "import",
            "path": "../../../../types"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "Holds the customer's billing address"
      }
    },
    "taxStatus": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "TaxStatus",
        "resolved": "\"address_invalid\" | \"calculated\" | \"disabled\" | \"estimated\" | \"reverse_charged\" | \"tax_registration_not_found\" | \"tax_zone_not_found\"",
        "references": {
          "TaxStatus": {
            "location": "import",
            "path": "../../../../types"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "Tax status of the order"
      },
      "attribute": "tax-status",
      "reflect": false
    },
    "shippingEnabled": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "Is shipping enabled for this order?"
      },
      "attribute": "shipping-enabled",
      "reflect": false
    },
    "full": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "Show the full address"
      },
      "attribute": "full",
      "reflect": false
    },
    "showName": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "Show the name field."
      },
      "attribute": "show-name",
      "reflect": false
    },
    "placeholders": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "Partial<Address>",
        "resolved": "{ name?: string; line_1?: string; line_2?: string; city?: string; state?: string; postal_code?: string; country?: string; constructor?: Function; toString?: () => string; toLocaleString?: () => string; valueOf?: () => Object; hasOwnProperty?: (v: PropertyKey) => boolean; isPrototypeOf?: (v: Object) => boolean; propertyIsEnumerable?: (v: PropertyKey) => boolean; }",
        "references": {
          "Partial": {
            "location": "global"
          },
          "Address": {
            "location": "import",
            "path": "../../../../types"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "Placeholder values."
      },
      "defaultValue": "{\n    name: __('Name or Company Name', 'surecart'),\n    country: __('Country', 'surecart'),\n    city: __('City', 'surecart'),\n    line_1: __('Address', 'surecart'),\n    line_2: __('Address Line 2', 'surecart'),\n    postal_code: __('Postal Code/Zip', 'surecart'),\n    state: __('State/Province/Region', 'surecart'),\n  }"
    }
  }; }
  static get states() { return {
    "address": {}
  }; }
  static get events() { return [{
      "method": "scUpdateOrder",
      "name": "scUpdateOrder",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": "Make a request to update the order."
      },
      "complexType": {
        "original": "{\n    data: Partial<Order>;\n    options?: { silent?: boolean };\n  }",
        "resolved": "{ data: Partial<Order>; options?: { silent?: boolean; }; }",
        "references": {
          "Partial": {
            "location": "global"
          },
          "Order": {
            "location": "import",
            "path": "../../../../types"
          }
        }
      }
    }]; }
  static get methods() { return {
    "reportValidity": {
      "complexType": {
        "signature": "() => Promise<boolean>",
        "parameters": [],
        "references": {
          "Promise": {
            "location": "global"
          }
        },
        "return": "Promise<boolean>"
      },
      "docs": {
        "text": "",
        "tags": []
      }
    }
  }; }
  static get watchers() { return [{
      "propName": "customerShippingAddress",
      "methodName": "handleCustomerAddressChange"
    }, {
      "propName": "shippingAddress",
      "methodName": "handleShippingChange"
    }]; }
}
openWormhole(ScOrderShippingAddress, ['shippingAddress', 'loading', 'customerShippingAddress', 'taxStatus', 'shippingEnabled'], false);
