import { Component, Event, h, Host, Listen, Prop, State, Watch } from '@stencil/core';
import apiFetch from '../../../../functions/fetch';
import { openWormhole } from 'stencil-wormhole';
import { __ } from '@wordpress/i18n';
export class ScCustomOrderPriceInput {
  constructor() {
    /** Is this loading */
    this.loading = false;
    /** Is this busy */
    this.busy = false;
    /** Label for the choice. */
    this.lineItems = [];
    /** Internal fetching state. */
    this.fetching = false;
  }
  handleBlur(e) {
    var _a;
    const ad_hoc_amount = parseInt(e.target.value);
    if (isNaN(ad_hoc_amount))
      return;
    if (((_a = this.lineItem) === null || _a === void 0 ? void 0 : _a.ad_hoc_amount) === ad_hoc_amount)
      return;
    this.scUpdateLineItem.emit({ price_id: this.priceId, quantity: 1, ad_hoc_amount });
  }
  /** Store current line item in state. */
  handleLineItemsChange() {
    var _a;
    if (!((_a = this.lineItems) === null || _a === void 0 ? void 0 : _a.length))
      return;
    this.lineItem = (this.lineItems || []).find(lineItem => lineItem.price.id === this.priceId);
  }
  componentDidLoad() {
    if (!this.price) {
      this.fetchPrice();
    }
  }
  /** Fetch prices and products */
  async fetchPrice() {
    if (!this.priceId)
      return;
    try {
      this.fetching = true;
      this.price = (await apiFetch({
        path: `surecart/v1/prices/${this.priceId}`,
      }));
    }
    catch (err) {
    }
    finally {
      this.fetching = false;
    }
  }
  renderEmpty() {
    var _a;
    if ((_a = window === null || window === void 0 ? void 0 : window.wp) === null || _a === void 0 ? void 0 : _a.blocks) {
      return (h("sc-alert", { type: "danger", open: true, style: { margin: '0px' } }, __('This price has been archived.', 'surecart')));
    }
    return h(Host, { style: { display: 'none' } });
  }
  render() {
    var _a, _b, _c, _d, _e, _f;
    if (this.loading || this.fetching) {
      return (h("div", null,
        h("sc-skeleton", { style: { width: '20%', marginBottom: '0.75em' } }),
        h("sc-skeleton", { style: { width: '100%' } })));
    }
    // Price needs to be active.
    if (!((_a = this === null || this === void 0 ? void 0 : this.price) === null || _a === void 0 ? void 0 : _a.id) || ((_b = this.price) === null || _b === void 0 ? void 0 : _b.archived))
      return this.renderEmpty();
    return (h("div", { class: "sc-custom-order-price-input" },
      h("sc-price-input", { "currency-code": ((_c = this.price) === null || _c === void 0 ? void 0 : _c.currency) || 'usd', label: this.label, min: (_d = this === null || this === void 0 ? void 0 : this.price) === null || _d === void 0 ? void 0 : _d.ad_hoc_min_amount, max: (_e = this === null || this === void 0 ? void 0 : this.price) === null || _e === void 0 ? void 0 : _e.ad_hoc_max_amount, placeholder: this.placeholder, required: this.required, value: (_f = this.lineItem) === null || _f === void 0 ? void 0 : _f.ad_hoc_amount.toString(), "show-code": this.showCode, help: this.help }),
      this.busy && h("sc-block-ui", { style: { zIndex: '9' } })));
  }
  static get is() { return "sc-custom-order-price-input"; }
  static get originalStyleUrls() { return {
    "$": ["sc-custom-order-price-input.css"]
  }; }
  static get styleUrls() { return {
    "$": ["sc-custom-order-price-input.css"]
  }; }
  static get properties() { return {
    "priceId": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "Id of the price."
      },
      "attribute": "price-id",
      "reflect": true
    },
    "price": {
      "type": "unknown",
      "mutable": true,
      "complexType": {
        "original": "Price",
        "resolved": "Price",
        "references": {
          "Price": {
            "location": "import",
            "path": "../../../../types"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "Stores the price"
      }
    },
    "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,
      "defaultValue": "false"
    },
    "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"
    },
    "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
    },
    "placeholder": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "Input placeholder."
      },
      "attribute": "placeholder",
      "reflect": false
    },
    "required": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "Is this required?"
      },
      "attribute": "required",
      "reflect": false
    },
    "help": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "Help text."
      },
      "attribute": "help",
      "reflect": false
    },
    "showCode": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "Show the currency code?"
      },
      "attribute": "show-code",
      "reflect": true
    },
    "lineItems": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "LineItem[]",
        "resolved": "LineItem[]",
        "references": {
          "LineItem": {
            "location": "import",
            "path": "../../../../types"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "Label for the choice."
      },
      "defaultValue": "[]"
    }
  }; }
  static get states() { return {
    "fetching": {},
    "lineItem": {}
  }; }
  static get events() { return [{
      "method": "scUpdateLineItem",
      "name": "scUpdateLineItem",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": "Toggle line item event"
      },
      "complexType": {
        "original": "LineItemData",
        "resolved": "LineItemData",
        "references": {
          "LineItemData": {
            "location": "import",
            "path": "../../../../types"
          }
        }
      }
    }]; }
  static get watchers() { return [{
      "propName": "lineItems",
      "methodName": "handleLineItemsChange"
    }]; }
  static get listeners() { return [{
      "name": "scBlur",
      "method": "handleBlur",
      "target": undefined,
      "capture": false,
      "passive": false
    }]; }
}
openWormhole(ScCustomOrderPriceInput, ['busy', 'lineItems'], false);
