import { Component, Event, h, Prop } from '@stencil/core';
import { openWormhole } from 'stencil-wormhole';
import { hasSubscription } from '../../../../functions/line-items';
import { intervalString } from '../../../../functions/price';
export class ScLineItems {
  constructor() {
    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 is() { return "sc-line-items"; }
  static get encapsulation() { return "shadow"; }
  static get originalStyleUrls() { return {
    "$": ["sc-line-items.css"]
  }; }
  static get styleUrls() { return {
    "$": ["sc-line-items.css"]
  }; }
  static get properties() { return {
    "order": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "Order",
        "resolved": "Order",
        "references": {
          "Order": {
            "location": "import",
            "path": "../../../../types"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "loading": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "loading",
      "reflect": false
    },
    "prices": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "Prices",
        "resolved": "{ [id: string]: Price; }",
        "references": {
          "Prices": {
            "location": "import",
            "path": "../../../../types"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "editable": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "editable",
      "reflect": false
    },
    "removable": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "removable",
      "reflect": false
    },
    "editLineItems": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "edit-line-items",
      "reflect": false,
      "defaultValue": "true"
    },
    "removeLineItems": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "remove-line-items",
      "reflect": false,
      "defaultValue": "true"
    },
    "lockedChoices": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "Array<PriceChoice>",
        "resolved": "PriceChoice[]",
        "references": {
          "Array": {
            "location": "global"
          },
          "PriceChoice": {
            "location": "import",
            "path": "../../../../types"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "defaultValue": "[]"
    }
  }; }
  static get events() { return [{
      "method": "scUpdateLineItem",
      "name": "scUpdateLineItem",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": "Update the line item."
      },
      "complexType": {
        "original": "LineItemData",
        "resolved": "LineItemData",
        "references": {
          "LineItemData": {
            "location": "import",
            "path": "../../../../types"
          }
        }
      }
    }, {
      "method": "scRemoveLineItem",
      "name": "scRemoveLineItem",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": "Remove the line item."
      },
      "complexType": {
        "original": "LineItemData",
        "resolved": "LineItemData",
        "references": {
          "LineItemData": {
            "location": "import",
            "path": "../../../../types"
          }
        }
      }
    }]; }
}
openWormhole(ScLineItems, ['order', 'loading', 'prices', 'lockedChoices', 'editLineItems', 'removeLineItems'], false);
