import { Component, Event, h, Prop, State, Watch } from '@stencil/core';
import { __ } from '@wordpress/i18n';
import { isRtl } from '../../../functions/page-align';
import { getHumanDiscount } from '../../../functions/price';
/**
 * @part base - The elements base wrapper.
 * @part form - The form.
 * @part input__base - The input base.
 * @part input - The input.
 * @part input__form-control - The input form control.
 * @part button__base - The button base element.
 * @part button__label - The button label.
 * @part info - The discount info.
 * @part discount - The discount displayed (% off)
 * @part amount - The discount amount.
 * @part discount-label - The discount label.
 * @part coupon-tag - The coupon tag.
 * @part error__base - The error base.
 * @part error__icon - The error icon
 * @part error__text - The error text.
 * @part error_title - The error title.
 * @part error__message - The error message.
 * @part block-ui - The block ui base component.
 * @part block-ui__content - The block ui content (spinner).
 */
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.value) {
      this.open = false;
      this.error = '';
    }
  }
  /** Apply the coupon. */
  applyCoupon() {
    this.scApplyCoupon.emit(this.input.value.toUpperCase());
  }
  handleKeyDown(e) {
    if ((e === null || e === void 0 ? void 0 : e.code) === 'Enter') {
      this.applyCoupon();
    }
  }
  render() {
    var _a, _b, _c, _d, _e, _f, _g;
    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) && ((_d = this === null || this === void 0 ? void 0 : this.discount) === null || _d === void 0 ? void 0 : _d.coupon.percent_off)) {
        humanDiscount = getHumanDiscount((_e = this === null || this === void 0 ? void 0 : this.discount) === null || _e === void 0 ? void 0 : _e.coupon);
      }
      return (h("sc-line-item", { exportparts: "description:info, price-description:discount, price:amount" },
        h("span", { slot: "description" },
          h("div", { part: "discount-label" }, __('Discount', 'surecart')),
          h("sc-tag", { exportparts: "base:coupon-tag", type: "success", class: "coupon-tag", clearable: true, onScClear: () => {
              this.scApplyCoupon.emit(null);
              this.open = false;
            } }, (_g = (_f = this === null || this === void 0 ? void 0 : this.discount) === null || _f === void 0 ? void 0 : _f.promotion) === null || _g === void 0 ? void 0 : _g.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 this.collapsed ? (h("div", { part: "base", class: {
        'coupon-form': true,
        'coupon-form--is-open': this.open || this.forceOpen,
        'coupon-form--has-value': !!this.value,
        'coupon-form--is-rtl': isRtl(),
      } },
      h("div", { part: "label", class: "trigger", onMouseDown: () => {
          if (this.open) {
            return;
          }
          this.open = true;
        } },
        h("slot", { name: "label" }, this.label)),
      h("div", { class: "form", part: "form" },
        h("sc-input", { exportparts: "base:input__base, input, form-control:input__form-control", value: this.value, onScInput: (e) => (this.value = e.target.value), placeholder: this.placeholder, onScBlur: () => this.handleBlur(), onKeyDown: e => this.handleKeyDown(e), ref: el => (this.input = el) },
          h("sc-button", { exportparts: "base:button__base, label:button_label", slot: "suffix", type: "text", loading: this.busy, size: "medium", class: "coupon-button", onClick: () => this.applyCoupon() },
            h("slot", null, this.buttonText))),
        h("sc-button", { exportparts: "base:button__base, label:button_label", type: "primary", outline: true, loading: this.busy, size: "medium", class: "coupon-button-mobile", onClick: () => this.applyCoupon() },
          h("slot", null, this.buttonText)),
        !!this.error && (h("sc-alert", { exportparts: "base:error__base, icon:error__icon, text:error__text, title:error_title, message:error__message", type: "danger", open: true },
          h("span", { slot: "title" }, this.error)))),
      this.loading && h("sc-block-ui", { exportparts: "base:block-ui, content:block-ui__content" }))) : (h("sc-form-control", { label: this.label, class: {
        'coupon-form': true,
        'coupon-form--has-value': !!this.value,
        'coupon-form--is-rtl': isRtl(),
      } },
      h("sc-input", { exportparts: "base:input__base, input, form-control:input__form-control", value: this.value, onScInput: (e) => (this.value = e.target.value), placeholder: this.placeholder, onScBlur: () => this.handleBlur(), onKeyDown: e => this.handleKeyDown(e), ref: el => (this.input = el) },
        h("sc-button", { exportparts: "base:button__base, label:button_label", slot: "suffix", type: "text", loading: this.busy, size: "medium", class: "coupon-button", onClick: () => this.applyCoupon() },
          h("slot", null, this.buttonText))),
      !!this.error && (h("sc-alert", { exportparts: "base:error__base, icon:error__icon, text:error__text, title:error_title, message:error__message", type: "danger", open: true },
        h("span", { slot: "title" }, this.error)))));
  }
  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
    },
    "placeholder": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "The placeholder for the input"
      },
      "attribute": "placeholder",
      "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
    },
    "collapsed": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "collapsed",
      "reflect": false
    },
    "buttonText": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "The text for apply button"
      },
      "attribute": "button-text",
      "reflect": true
    }
  }; }
  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"
    }]; }
}
