import { Component, Event, h, Method, Prop, State, Watch } from '@stencil/core';
import { state as checkoutState } from '@store/checkout';
import { lockCheckout, unLockCheckout } from '@store/checkout/mutations';
import { __ } from '@wordpress/i18n';
import { createOrUpdateCheckout } from '../../../../services/session';
import { openWormhole } from 'stencil-wormhole';
export class ScOrderShippingAddress {
  constructor() {
    /** 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');
      checkoutState.checkout = (await createOrUpdateCheckout({
        id: checkoutState.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 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": true,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "Is this required (defaults to false)"
      },
      "attribute": "required",
      "reflect": false,
      "defaultValue": "false"
    },
    "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
    },
    "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
    },
    "taxEnabled": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "Tax enabled status of the order"
      },
      "attribute": "tax-enabled",
      "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
    },
    "namePlaceholder": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "Show the placeholder fields."
      },
      "attribute": "name-placeholder",
      "reflect": false,
      "defaultValue": "__('Name or Company Name', 'surecart')"
    },
    "countryPlaceholder": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "country-placeholder",
      "reflect": false,
      "defaultValue": "__('Country', 'surecart')"
    },
    "cityPlaceholder": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "city-placeholder",
      "reflect": false,
      "defaultValue": "__('City', 'surecart')"
    },
    "line1Placeholder": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "line-1-placeholder",
      "reflect": false,
      "defaultValue": "__('Address', 'surecart')"
    },
    "line2Placeholder": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "line-2-placeholder",
      "reflect": false,
      "defaultValue": "__('Address Line 2', 'surecart')"
    },
    "postalCodePlaceholder": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "postal-code-placeholder",
      "reflect": false,
      "defaultValue": "__('Postal Code/Zip', 'surecart')"
    },
    "statePlaceholder": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "state-placeholder",
      "reflect": false,
      "defaultValue": "__('State/Province/Region', 'surecart')"
    },
    "defaultCountry": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "Default country for address"
      },
      "attribute": "default-country",
      "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<Checkout>;\n    options?: { silent?: boolean };\n  }",
        "resolved": "{ data: Partial<Checkout>; options?: { silent?: boolean; }; }",
        "references": {
          "Partial": {
            "location": "global"
          },
          "Checkout": {
            "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": "shippingAddress",
      "methodName": "handleCustomerAddressChange"
    }, {
      "propName": "shippingEnabled",
      "methodName": "handleRequirementChange"
    }, {
      "propName": "taxEnabled",
      "methodName": "handleRequirementChange"
    }]; }
}
openWormhole(ScOrderShippingAddress, ['shippingAddress', 'loading', 'taxStatus', 'taxEnabled', 'shippingEnabled'], false);
