import { proxyCustomElement, HTMLElement, createEvent, h } from '@stencil/core/internal/client';
import { _ as __, s as sprintf } from './default-i18n.js';
import './index2.js';
import { a as isBumpInOrder } from './index5.js';
import { i as intervalString } from './price.js';
import { d as defineCustomElement$3 } from './sc-choice2.js';
import { d as defineCustomElement$2 } from './sc-divider2.js';
import { d as defineCustomElement$1 } from './sc-format-number2.js';

const scOrderBumpCss = ":host{display:block}.bump{display:grid;gap:1em}.bump__text{display:grid;gap:0.25em}.bump__tag{background:var(--sc-color-primary-500);color:var(--sc-color-white);border-radius:var(--sc-input-border-radius-medium);padding:var(--sc-spacing-x-small);font-size:var(--sc-font-size-x-small)}.bump__product{display:flex;align-items:center;gap:var(--sc-choice-padding, 1.3em 1.1em);line-height:var(--sc-line-height-dense)}.bump__product-title{font-weight:var(--sc-font-weight-semibold)}.bump__product-description{color:var(--sc-input-label-color)}.bump__image{width:var(--sc-product-line-item-image-size, 4em);height:var(--sc-product-line-item-image-size, 4em);flex:0 0 var(--sc-product-line-item-image-size, 4em);object-fit:cover;border-radius:4px;border:1px solid var(--sc-color-gray-200);display:block;box-shadow:var(--sc-input-box-shadow)}.bump__price--has-discount .bump__original-price{text-decoration:line-through;color:var(--sc-color-gray-500);font-size:var(--sc-font-size-small)}.bump__price .bump__new-price{font-size:var(--sc-font-size-large);color:var(--sc-color-gray-700)}.bump__price .bump__interval{color:var(--sc-color-gray-500)}.bump__amount{display:flex;align-items:center;gap:var(--sc-spacing-x-small);flex-wrap:wrap;margin-top:var(--sc-spacing-xx-small)}";

const ScOrderBump = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  constructor() {
    super();
    this.__registerHost();
    this.__attachShadow();
    this.scAddLineItem = createEvent(this, "scAddLineItem", 7);
    this.scRemoveLineItem = createEvent(this, "scRemoveLineItem", 7);
  }
  /** Update the line item. */
  updateLineItem(add) {
    var _a, _b, _c;
    const price_id = ((_a = this.bump.price) === null || _a === void 0 ? void 0 : _a.id) || ((_b = this.bump) === null || _b === void 0 ? void 0 : _b.price);
    if (add) {
      this.scAddLineItem.emit({
        bump: (_c = this.bump) === null || _c === void 0 ? void 0 : _c.id,
        price_id,
        quantity: 1,
      });
    }
    else {
      this.scRemoveLineItem.emit({
        price_id,
        quantity: 1,
      });
    }
  }
  newPrice() {
    var _a, _b, _c, _d, _e, _f;
    let amount = null;
    let initialAmount = ((_b = (_a = this.bump) === null || _a === void 0 ? void 0 : _a.price) === null || _b === void 0 ? void 0 : _b.amount) || 0;
    if ((_c = this.bump) === null || _c === void 0 ? void 0 : _c.amount_off) {
      amount = Math.max(0, initialAmount - ((_d = this.bump) === null || _d === void 0 ? void 0 : _d.amount_off));
    }
    if ((_e = this.bump) === null || _e === void 0 ? void 0 : _e.percent_off) {
      const off = initialAmount * (((_f = this.bump) === null || _f === void 0 ? void 0 : _f.percent_off) / 100);
      amount = Math.max(0, initialAmount - off);
    }
    return amount;
  }
  renderInterval() {
    var _a;
    const interval = intervalString((_a = this.bump) === null || _a === void 0 ? void 0 : _a.price, { labels: { interval: '/', period: __('for', 'surecart') } });
    if (!interval.trim().length)
      return null;
    return h("span", { class: "bump__interval" }, interval);
  }
  renderPrice() {
    var _a, _b, _c, _d, _e, _f, _g;
    return (h("div", { slot: "description", class: { 'bump__price': true, 'bump__price--has-discount': !!((_a = this.bump) === null || _a === void 0 ? void 0 : _a.percent_off) || !!((_b = this.bump) === null || _b === void 0 ? void 0 : _b.amount_off) }, part: "price" }, h("sc-format-number", { type: "currency", class: "bump__original-price", value: (_d = (_c = this.bump) === null || _c === void 0 ? void 0 : _c.price) === null || _d === void 0 ? void 0 : _d.amount, currency: (_f = (_e = this.bump) === null || _e === void 0 ? void 0 : _e.price) === null || _f === void 0 ? void 0 : _f.currency }), ' ', this.newPrice() === 0 && __('Free', 'surecart'), this.newPrice() !== null && this.newPrice() > 0 && (h("sc-format-number", { type: "currency", class: "bump__new-price", value: this.newPrice(), currency: ((_g = this.bump) === null || _g === void 0 ? void 0 : _g.price).currency })), this.renderInterval()));
  }
  renderDiscount() {
    var _a, _b, _c, _d, _e;
    if (!!((_a = this.bump) === null || _a === void 0 ? void 0 : _a.amount_off)) {
      return (h("div", { class: "bump__tag" }, __('Save', 'surecart'), " ", h("sc-format-number", { type: "currency", value: -((_b = this.bump) === null || _b === void 0 ? void 0 : _b.amount_off), currency: ((_c = this.bump) === null || _c === void 0 ? void 0 : _c.price).currency })));
    }
    if (!!((_d = this.bump) === null || _d === void 0 ? void 0 : _d.percent_off)) {
      return h("div", { class: "bump__tag" }, sprintf(__('Save %s%%', 'surecart'), (_e = this.bump) === null || _e === void 0 ? void 0 : _e.percent_off));
    }
  }
  render() {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
    const product = (_b = (_a = this.bump) === null || _a === void 0 ? void 0 : _a.price) === null || _b === void 0 ? void 0 : _b.product;
    return (h("sc-choice", { value: (_c = this.bump) === null || _c === void 0 ? void 0 : _c.id, type: "checkbox", showControl: this.showControl, checked: isBumpInOrder(this.bump, this.checkout), onScChange: e => this.updateLineItem(e.target.checked), exportparts: "base, control, checked-icon, title" }, h("div", { part: "base-content", class: "bump" }, h("div", { class: "bump__text" }, h("div", { class: "bump__title" }, ((_e = (_d = this.bump) === null || _d === void 0 ? void 0 : _d.metadata) === null || _e === void 0 ? void 0 : _e.cta) || ((_f = this.bump) === null || _f === void 0 ? void 0 : _f.name) || (product === null || product === void 0 ? void 0 : product.name)), h("div", { class: "bump__amount" }, this.renderPrice(), " ", this.renderDiscount()))), ((_h = (_g = this.bump) === null || _g === void 0 ? void 0 : _g.metadata) === null || _h === void 0 ? void 0 : _h.description) && (h("div", { slot: "footer" }, h("sc-divider", { style: { '--spacing': 'var(--sc-spacing-medium)' } }), h("div", { class: "bump__product" }, !!(product === null || product === void 0 ? void 0 : product.image_url) && h("img", { src: `${window.scData.cdn_root}fit=scale-down,format=auto,width=130/${product === null || product === void 0 ? void 0 : product.image_url}`, class: "bump__image" }), h("div", { class: "bump__product-text" }, !!((_k = (_j = this.bump) === null || _j === void 0 ? void 0 : _j.metadata) === null || _k === void 0 ? void 0 : _k.cta) && h("div", { class: "bump__product-title" }, this.bump.name || (product === null || product === void 0 ? void 0 : product.name)), !!((_m = (_l = this.bump) === null || _l === void 0 ? void 0 : _l.metadata) === null || _m === void 0 ? void 0 : _m.description) && h("div", { class: "bump__product-description" }, (_p = (_o = this.bump) === null || _o === void 0 ? void 0 : _o.metadata) === null || _p === void 0 ? void 0 : _p.description)))))));
  }
  static get style() { return scOrderBumpCss; }
}, [1, "sc-order-bump", {
    "bump": [16],
    "checkout": [16],
    "showControl": [516, "show-control"]
  }]);
function defineCustomElement() {
  if (typeof customElements === "undefined") {
    return;
  }
  const components = ["sc-order-bump", "sc-choice", "sc-divider", "sc-format-number"];
  components.forEach(tagName => { switch (tagName) {
    case "sc-order-bump":
      if (!customElements.get(tagName)) {
        customElements.define(tagName, ScOrderBump);
      }
      break;
    case "sc-choice":
      if (!customElements.get(tagName)) {
        defineCustomElement$3();
      }
      break;
    case "sc-divider":
      if (!customElements.get(tagName)) {
        defineCustomElement$2();
      }
      break;
    case "sc-format-number":
      if (!customElements.get(tagName)) {
        defineCustomElement$1();
      }
      break;
  } });
}
defineCustomElement();

export { ScOrderBump as S, defineCustomElement as d };
