import { Component, Event, h, Prop } from '@stencil/core';
import { state as checkoutState } from '@store/checkout';
import { lockCheckout, unLockCheckout } from '@store/checkout/mutations';
import { createOrUpdateCheckout } from '../../../../services/session';
import { openWormhole } from 'stencil-wormhole';
export class ScOrderTaxIdInput {
  constructor() {
    /** 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';
  }
  async maybeUpdateOrder(tax_identifier) {
    try {
      lockCheckout('tax_identifier');
      checkoutState.checkout = (await createOrUpdateCheckout({
        id: checkoutState.checkout.id,
        data: { tax_identifier },
      }));
    }
    catch (e) {
      console.error(e);
      this.scError.emit(e);
    }
    finally {
      unLockCheckout('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);
      }, otherLabel: this.otherLabel, caGstLabel: this.caGstLabel, auAbnLabel: this.auAbnLabel, gbVatLabel: this.gbVatLabel, euVatLabel: this.euVatLabel }));
  }
  static get is() { return "sc-order-tax-id-input"; }
  static get encapsulation() { return "shadow"; }
  static get originalStyleUrls() { return {
    "$": ["sc-order-tax-id-input.css"]
  }; }
  static get styleUrls() { return {
    "$": ["sc-order-tax-id-input.css"]
  }; }
  static get properties() { return {
    "order": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "Partial<Checkout>",
        "resolved": "{ id?: string; status?: \"draft\" | \"paid\" | \"canceled\" | \"finalized\" | \"payment_intent_canceled\" | \"payment_failed\" | \"processing\"; staged_payment_intents?: { object: \"list\"; pagination: Pagination; data: PaymentIntent[]; }; abandoned_checkout_enabled?: boolean; bump_amount?: number; payment_method_required?: boolean; manual_payment?: boolean; manual_payment_method?: string | ManualPaymentMethod; reusable_payment_method_required?: boolean; number?: string; amount_due?: number; trial_amount?: number; charge?: string | Charge; name?: string; first_name?: string; last_name?: string; email?: string; phone?: string; live_mode?: boolean; currency?: string; total_amount?: number; subtotal_amount?: number; full_amount?: number; proration_amount?: number; total_savings_amount?: number; applied_balance_amount?: number; discounts?: number; tax_enabled?: boolean; tax_amount?: number; email_exists?: boolean; tax_inclusive_amount?: number; tax_exclusive_amount?: number; tax_status?: \"disabled\" | \"address_invalid\" | \"estimated\" | \"calculated\"; tax_label?: string; tax_percent?: number; line_items?: lineItems; recommended_bumps?: { object: \"list\"; pagination: Pagination; data: Bump[]; }; metadata?: any; payment_intent?: PaymentIntent; payment_method?: PaymentMethod; order?: string | Order; customer?: string | Customer; subscriptions?: { object: \"list\"; pagination: Pagination; data: Subscription[]; }; purchases?: { object: \"list\"; pagination: Pagination; data: Purchase[]; }; discount_amount?: number; discount?: DiscountResponse; billing_address?: string | Address; shipping_address?: string | Address; shipping_enabled?: boolean; processor_data?: ProcessorData; tax_identifier?: { number: string; number_type: string; }; url?: string; created_at?: number; constructor?: Function; toString?: () => string; toLocaleString?: () => string; valueOf?: () => Object; hasOwnProperty?: (v: PropertyKey) => boolean; isPrototypeOf?: (v: Object) => boolean; propertyIsEnumerable?: (v: PropertyKey) => boolean; }",
        "references": {
          "Partial": {
            "location": "global"
          },
          "Checkout": {
            "location": "import",
            "path": "../../../../types"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "The order"
      }
    },
    "show": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "Force show the field."
      },
      "attribute": "show",
      "reflect": false,
      "defaultValue": "false"
    },
    "taxIdentifier": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "TaxIdentifier",
        "resolved": "TaxIdentifier",
        "references": {
          "TaxIdentifier": {
            "location": "import",
            "path": "../../../../types"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "Tax identifier"
      }
    },
    "taxProtocol": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "TaxProtocol",
        "resolved": "TaxProtocol",
        "references": {
          "TaxProtocol": {
            "location": "import",
            "path": "../../../../types"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "The tax protocol."
      }
    },
    "busy": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "Is this busy"
      },
      "attribute": "busy",
      "reflect": false,
      "defaultValue": "false"
    },
    "otherLabel": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "Other zones label"
      },
      "attribute": "other-label",
      "reflect": false
    },
    "caGstLabel": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "GST zone label"
      },
      "attribute": "ca-gst-label",
      "reflect": false
    },
    "auAbnLabel": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "AU zone label"
      },
      "attribute": "au-abn-label",
      "reflect": false
    },
    "gbVatLabel": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "UK zone label"
      },
      "attribute": "gb-vat-label",
      "reflect": false
    },
    "euVatLabel": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "EU zone label"
      },
      "attribute": "eu-vat-label",
      "reflect": false
    }
  }; }
  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"
          }
        }
      }
    }, {
      "method": "scError",
      "name": "scError",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": "Error event"
      },
      "complexType": {
        "original": "ResponseError",
        "resolved": "ResponseError",
        "references": {
          "ResponseError": {
            "location": "import",
            "path": "../../../../types"
          }
        }
      }
    }]; }
}
openWormhole(ScOrderTaxIdInput, ['draft', 'order', 'tax_status', 'taxIdentifier', 'taxProtocol', 'busy'], false);
