import { proxyCustomElement, HTMLElement, createEvent, h } from '@stencil/core/internal/client';
import { o as openWormhole } from './consumer.js';
import { h as hasSubscription } from './index4.js';
import { i as intervalString } from './price.js';
import { d as defineCustomElement$7 } from './sc-format-number2.js';
import { d as defineCustomElement$6 } from './sc-icon2.js';
import { d as defineCustomElement$5 } from './sc-line-item2.js';
import { d as defineCustomElement$4 } from './sc-product-line-item2.js';
import { d as defineCustomElement$3 } from './sc-quantity-select2.js';
import { d as defineCustomElement$2 } from './sc-skeleton2.js';

const scLineItemsCss = ":host{display:block}:slotted(*~*){margin-top:20px}sc-product-line-item~sc-product-line-item,sc-line-item~sc-line-item{margin-top:20px}";

const ScLineItems$1 = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  constructor() {
    super();
    this.__registerHost();
    this.__attachShadow();
    this.scUpdateLineItem = createEvent(this, "scUpdateLineItem", 7);
    this.scRemoveLineItem = createEvent(this, "scRemoveLineItem", 7);
    this.editLineItems = true;
    this.removeLineItems = true;
    this.lockedChoices = [];
  }
  /** Update quantity for this line item. */
  updateQuantity(item, quantity) {
    this.scUpdateLineItem.emit({ price_id: item.price.id, quantity });
  }
  removeLineItem(item) {
    this.scRemoveLineItem.emit({ price_id: item.price.id, quantity: 1 });
  }
  /** Only append price name if there's more than one product price in the session. */
  getName(item) {
    var _a, _b, _c, _d, _e;
    const otherPrices = Object.keys(this.prices || {}).filter(key => {
      const price = this.prices[key];
      // @ts-ignore
      return price.product === item.price.product.id;
    });
    let name = '';
    if (otherPrices.length > 1) {
      name = `${(_b = (_a = item === null || item === void 0 ? void 0 : item.price) === null || _a === void 0 ? void 0 : _a.product) === null || _b === void 0 ? void 0 : _b.name} \u2013 ${(_c = item === null || item === void 0 ? void 0 : item.price) === null || _c === void 0 ? void 0 : _c.name}`;
    }
    else {
      name = (_e = (_d = item === null || item === void 0 ? void 0 : item.price) === null || _d === void 0 ? void 0 : _d.product) === null || _e === void 0 ? void 0 : _e.name;
    }
    return name;
  }
  // Is this price choice locked?
  isLocked(item) {
    return this.lockedChoices.some(choice => choice.id === item.price.id);
  }
  isEditable() {
    if (this.editable !== null)
      return this.editable;
    return this.editLineItems;
  }
  isRemovable() {
    if (this.removable !== null)
      return this.removable;
    return this.removeLineItems;
  }
  render() {
    var _a, _b, _c, _d, _e;
    if (!!this.loading && !((_c = (_b = (_a = this.order) === null || _a === void 0 ? void 0 : _a.line_items) === null || _b === void 0 ? void 0 : _b.data) === null || _c === void 0 ? void 0 : _c.length)) {
      return (h("sc-line-item", null, h("sc-skeleton", { style: { 'width': '50px', 'height': '50px', '--border-radius': '0' }, slot: "image" }), h("sc-skeleton", { slot: "title", style: { width: '120px', display: 'inline-block' } }), h("sc-skeleton", { slot: "description", style: { width: '60px', display: 'inline-block' } }), h("sc-skeleton", { style: { width: '120px', display: 'inline-block' }, slot: "price" }), h("sc-skeleton", { style: { width: '60px', display: 'inline-block' }, slot: "price-description" })));
    }
    return (h("div", { class: "line-items" }, (((_e = (_d = this.order) === null || _d === void 0 ? void 0 : _d.line_items) === null || _e === void 0 ? void 0 : _e.data) || [])
      .sort((a, b) => {
      var _a, _b, _c, _d;
      if (((_a = a.price) === null || _a === void 0 ? void 0 : _a.id) < ((_b = b.price) === null || _b === void 0 ? void 0 : _b.id))
        return -1;
      return ((_c = a.price) === null || _c === void 0 ? void 0 : _c.id) > ((_d = b.price) === null || _d === void 0 ? void 0 : _d.id) ? 1 : 0;
    })
      .map(item => {
      var _a, _b, _c, _d, _e, _f, _g;
      return (h("sc-product-line-item", { key: item.id, imageUrl: (_b = (_a = item === null || item === void 0 ? void 0 : item.price) === null || _a === void 0 ? void 0 : _a.product) === null || _b === void 0 ? void 0 : _b.image_url, name: (_d = (_c = item === null || item === void 0 ? void 0 : item.price) === null || _c === void 0 ? void 0 : _c.product) === null || _d === void 0 ? void 0 : _d.name, editable: this.isEditable() && !((_e = item === null || item === void 0 ? void 0 : item.price) === null || _e === void 0 ? void 0 : _e.ad_hoc), removable: this.isRemovable(), quantity: item.quantity, amount: item.ad_hoc_amount !== null ? item.ad_hoc_amount : item.subtotal_amount, currency: (_f = this.order) === null || _f === void 0 ? void 0 : _f.currency, trialDurationDays: (_g = item === null || item === void 0 ? void 0 : item.price) === null || _g === void 0 ? void 0 : _g.trial_duration_days, interval: !!(item === null || item === void 0 ? void 0 : item.price) && intervalString(item === null || item === void 0 ? void 0 : item.price, { showOnce: hasSubscription(this.order) }), onScUpdateQuantity: e => this.updateQuantity(item, e.detail), onScRemove: () => this.removeLineItem(item) }));
    })));
  }
  static get style() { return scLineItemsCss; }
}, [1, "sc-line-items", {
    "order": [16],
    "loading": [4],
    "prices": [16],
    "editable": [4],
    "removable": [4],
    "editLineItems": [4, "edit-line-items"],
    "removeLineItems": [4, "remove-line-items"],
    "lockedChoices": [16]
  }]);
openWormhole(ScLineItems$1, ['order', 'loading', 'prices', 'lockedChoices', 'editLineItems', 'removeLineItems'], false);
function defineCustomElement$1() {
  if (typeof customElements === "undefined") {
    return;
  }
  const components = ["sc-line-items", "sc-format-number", "sc-icon", "sc-line-item", "sc-product-line-item", "sc-quantity-select", "sc-skeleton"];
  components.forEach(tagName => { switch (tagName) {
    case "sc-line-items":
      if (!customElements.get(tagName)) {
        customElements.define(tagName, ScLineItems$1);
      }
      break;
    case "sc-format-number":
      if (!customElements.get(tagName)) {
        defineCustomElement$7();
      }
      break;
    case "sc-icon":
      if (!customElements.get(tagName)) {
        defineCustomElement$6();
      }
      break;
    case "sc-line-item":
      if (!customElements.get(tagName)) {
        defineCustomElement$5();
      }
      break;
    case "sc-product-line-item":
      if (!customElements.get(tagName)) {
        defineCustomElement$4();
      }
      break;
    case "sc-quantity-select":
      if (!customElements.get(tagName)) {
        defineCustomElement$3();
      }
      break;
    case "sc-skeleton":
      if (!customElements.get(tagName)) {
        defineCustomElement$2();
      }
      break;
  } });
}
defineCustomElement$1();

const ScLineItems = ScLineItems$1;
const defineCustomElement = defineCustomElement$1;

export { ScLineItems, defineCustomElement };
