import { Component, State, h, Watch, Prop, Event } from '@stencil/core';
import { __ } from '@wordpress/i18n';
import { getHumanDiscount } from '../../../functions/price';
export class ScCouponForm {
  /** Auto focus the input when opened. */
  handleOpenChange(val) {
    if (val) {
      setTimeout(() => this.input.triggerFocus(), 50);
    }
  }
  /** Close it when blurred and no value. */
  handleBlur() {
    if (!this.input.value) {
      this.open = false;
      this.error = '';
    }
  }
  /** Apply the coupon. */
  applyCoupon() {
    this.scApplyCoupon.emit(this.input.value.toUpperCase());
  }
  render() {
    var _a, _b, _c, _d, _e, _f;
    if (this.loading) {
      return h("sc-skeleton", { style: { width: '120px', display: 'inline-block' } });
    }
    if ((_b = (_a = this === null || this === void 0 ? void 0 : this.discount) === null || _a === void 0 ? void 0 : _a.promotion) === null || _b === void 0 ? void 0 : _b.code) {
      let humanDiscount = '';
      if ((_c = this === null || this === void 0 ? void 0 : this.discount) === null || _c === void 0 ? void 0 : _c.coupon) {
        humanDiscount = getHumanDiscount((_d = this === null || this === void 0 ? void 0 : this.discount) === null || _d === void 0 ? void 0 : _d.coupon);
      }
      return (h("sc-line-item", null,
        h("span", { slot: "description" },
          h("div", null, __('Discount')),
          h("sc-tag", { type: "success", class: "coupon-tag", clearable: true, onScClear: () => {
              this.scApplyCoupon.emit(null);
              this.open = false;
            } }, (_f = (_e = this === null || this === void 0 ? void 0 : this.discount) === null || _e === void 0 ? void 0 : _e.promotion) === null || _f === void 0 ? void 0 : _f.code)),
        humanDiscount && (h("span", { class: "coupon-human-discount", slot: "price-description" },
          "(",
          humanDiscount,
          ")")),
        h("span", { slot: "price" },
          h("sc-format-number", { type: "currency", currency: this === null || this === void 0 ? void 0 : this.currency, value: this === null || this === void 0 ? void 0 : this.discountAmount }))));
    }
    return (h("div", { class: {
        'coupon-form': true,
        'coupon-form--is-open': this.open || this.forceOpen,
      } },
      h("div", { class: "trigger", onMouseDown: () => {
          if (this.open) {
            return;
          }
          this.open = true;
        } },
        h("slot", { name: "label" }, this.label)),
      h("div", { class: "form" },
        h("sc-input", { onScBlur: () => this.handleBlur(), ref: el => (this.input = el), clearable: true }),
        !!this.error && (h("sc-alert", { type: "danger", open: true },
          h("span", { slot: "title" }, this.error))),
        h("sc-button", { type: "primary", loading: this.busy, full: true, onClick: () => this.applyCoupon() },
          h("slot", null))),
      this.loading && h("sc-block-ui", null)));
  }
  static get is() { return "sc-coupon-form"; }
  static get encapsulation() { return "shadow"; }
  static get originalStyleUrls() { return {
    "$": ["sc-coupon-form.scss"]
  }; }
  static get styleUrls() { return {
    "$": ["sc-coupon-form.css"]
  }; }
  static get properties() { return {
    "label": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "The label for the coupon form"
      },
      "attribute": "label",
      "reflect": false
    },
    "loading": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "Is the form loading"
      },
      "attribute": "loading",
      "reflect": false
    },
    "busy": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "Is the form calculating"
      },
      "attribute": "busy",
      "reflect": false
    },
    "error": {
      "type": "string",
      "mutable": true,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "The error message"
      },
      "attribute": "error",
      "reflect": false
    },
    "forceOpen": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "Force the form to show"
      },
      "attribute": "force-open",
      "reflect": false
    },
    "discount": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "DiscountResponse",
        "resolved": "DiscountResponse",
        "references": {
          "DiscountResponse": {
            "location": "import",
            "path": "../../../types"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "The discount"
      }
    },
    "currency": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "Currency"
      },
      "attribute": "currency",
      "reflect": false
    },
    "discountAmount": {
      "type": "number",
      "mutable": false,
      "complexType": {
        "original": "number",
        "resolved": "number",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "The discount amount"
      },
      "attribute": "discount-amount",
      "reflect": false
    },
    "open": {
      "type": "boolean",
      "mutable": true,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "Is it open"
      },
      "attribute": "open",
      "reflect": false
    }
  }; }
  static get states() { return {
    "value": {}
  }; }
  static get events() { return [{
      "method": "scApplyCoupon",
      "name": "scApplyCoupon",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": "When the coupon is applied"
      },
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      }
    }]; }
  static get watchers() { return [{
      "propName": "open",
      "methodName": "handleOpenChange"
    }]; }
}
