'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const index = require('./index-cd6fab62.js');
const defaultI18n = require('./default-i18n-072e75d7.js');
require('./index-20b57ec7.js');
const price = require('./price-4a45d4fd.js');
require('./_commonjsHelpers-537d719a.js');

const scCouponFormCss = ":host{display:block}.coupon-form{position:relative}.form{opacity:0;visibility:hidden;height:0;transition:opacity var(--sc-transition-fast) ease-in-out}.coupon-form--is-open .form{opacity:1;visibility:visible;height:auto;margin-top:var(--sc-spacing-small);display:grid;gap:var(--sc-spacing-small)}.coupon-form--is-open .trigger{color:var(--sc-input-label-color)}.coupon-form--is-open .trigger:hover{text-decoration:none}.trigger{cursor:pointer;font-size:var(--sc-font-size-small);color:var(--sc-input-label-color);user-select:none}.trigger:hover{text-decoration:underline}";

const ScCouponForm = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.scApplyCoupon = index.createEvent(this, "scApplyCoupon", 7);
  }
  /** 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 index.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 = price.getHumanDiscount((_d = this === null || this === void 0 ? void 0 : this.discount) === null || _d === void 0 ? void 0 : _d.coupon);
      }
      return (index.h("sc-line-item", null, index.h("span", { slot: "description" }, index.h("div", null, defaultI18n.__('Discount')), index.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 && (index.h("span", { class: "coupon-human-discount", slot: "price-description" }, "(", humanDiscount, ")")), index.h("span", { slot: "price" }, index.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 (index.h("div", { class: {
        'coupon-form': true,
        'coupon-form--is-open': this.open || this.forceOpen,
      } }, index.h("div", { class: "trigger", onMouseDown: () => {
        if (this.open) {
          return;
        }
        this.open = true;
      } }, index.h("slot", { name: "label" }, this.label)), index.h("div", { class: "form" }, index.h("sc-input", { onScBlur: () => this.handleBlur(), ref: el => (this.input = el), clearable: true }), !!this.error && (index.h("sc-alert", { type: "danger", open: true }, index.h("span", { slot: "title" }, this.error))), index.h("sc-button", { type: "primary", loading: this.busy, full: true, onClick: () => this.applyCoupon() }, index.h("slot", null))), this.loading && index.h("sc-block-ui", null)));
  }
  static get watchers() { return {
    "open": ["handleOpenChange"]
  }; }
};
ScCouponForm.style = scCouponFormCss;

exports.sc_coupon_form = ScCouponForm;
