'use strict';

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

const index = require('./index-9c866aeb.js');
const defaultI18n = require('./default-i18n-d4248548.js');
require('./index-88baf240.js');
const pageAlign = require('./page-align-bf197eb4.js');
const price = require('./price-fcc0d0f6.js');
require('./index-04dc658f.js');

const scCouponFormCss = ":host {\n  display: block;\n}\n\nsc-button {\n  color: var(--sc-color-primary-500);\n}\n\n.coupon-form {\n  position: relative;\n  container-type: inline-size;\n}\n.coupon-form .coupon-button {\n  opacity: 0;\n  visibility: hidden;\n  transform: scale(0.9);\n  transition: all var(--sc-transition-fast) ease;\n}\n.coupon-form .coupon-button-mobile {\n  margin-top: var(--sc-input-label-margin);\n  display: none;\n}\n.coupon-form--has-value .coupon-button {\n  opacity: 1;\n  visibility: visible;\n  transform: scale(1);\n}\n\n@container (max-width: 320px) {\n  .coupon-form .coupon-button {\n    display: none;\n  }\n  .coupon-form .coupon-button-mobile {\n    display: block;\n  }\n}\n.form {\n  opacity: 0;\n  visibility: hidden;\n  height: 0;\n  transform: translateY(5px);\n  transition: opacity var(--sc-transition-medium) ease, transform var(--sc-transition-medium) ease;\n  position: relative;\n  gap: var(--sc-spacing-small);\n}\n\n.coupon-form--is-open .form {\n  opacity: 1;\n  visibility: visible;\n  transform: translateY(0);\n  height: auto;\n  margin: var(--sc-spacing-small) 0;\n}\n.coupon-form--is-open .trigger {\n  display: none;\n}\n\n.trigger {\n  cursor: pointer;\n  font-size: var(--sc-font-size-small);\n  line-height: var(--sc-line-height-dense);\n  color: var(--sc-input-label-color);\n  user-select: none;\n}\n.trigger:hover {\n  text-decoration: underline;\n}\n\n.coupon-form--is-rtl .trigger {\n  text-align: right;\n}";

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.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 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) && ((_d = this === null || this === void 0 ? void 0 : this.discount) === null || _d === void 0 ? void 0 : _d.coupon.percent_off)) {
        humanDiscount = price.getHumanDiscount((_e = this === null || this === void 0 ? void 0 : this.discount) === null || _e === void 0 ? void 0 : _e.coupon);
      }
      return (index.h("sc-line-item", { exportparts: "description:info, price-description:discount, price:amount" }, index.h("span", { slot: "description" }, index.h("div", { part: "discount-label" }, defaultI18n.__('Discount', 'surecart')), index.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 && (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 this.collapsed ? (index.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': pageAlign.isRtl(),
      } }, index.h("div", { part: "label", class: "trigger", onMouseDown: () => {
        if (this.open) {
          return;
        }
        this.open = true;
      } }, index.h("slot", { name: "label" }, this.label)), index.h("div", { class: "form", part: "form" }, index.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) }, index.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() }, index.h("slot", null, this.buttonText))), index.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() }, index.h("slot", null, this.buttonText)), !!this.error && (index.h("sc-alert", { exportparts: "base:error__base, icon:error__icon, text:error__text, title:error_title, message:error__message", type: "danger", open: true }, index.h("span", { slot: "title" }, this.error)))), this.loading && index.h("sc-block-ui", { exportparts: "base:block-ui, content:block-ui__content" }))) : (index.h("sc-form-control", { label: this.label, class: {
        'coupon-form': true,
        'coupon-form--has-value': !!this.value,
        'coupon-form--is-rtl': pageAlign.isRtl(),
      } }, index.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) }, index.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() }, index.h("slot", null, this.buttonText))), !!this.error && (index.h("sc-alert", { exportparts: "base:error__base, icon:error__icon, text:error__text, title:error_title, message:error__message", type: "danger", open: true }, index.h("span", { slot: "title" }, this.error)))));
  }
  static get watchers() { return {
    "open": ["handleOpenChange"]
  }; }
};
ScCouponForm.style = scCouponFormCss;

exports.sc_coupon_form = ScCouponForm;
