import { Component, h, Prop, Event, Element, Fragment } from '@stencil/core';
import { __, _n, sprintf } from '@wordpress/i18n';
import { isRtl } from '../../../functions/page-align';
/**
 * @part base - The elements base wrapper.
 * @part text - The text wrapper.
 * @part title - The product title.
 * @part static-quantity - The statically displayed quantity.
 * @part quantity__base - The quantity base wrapper.
 * @part input - The input control.
 * @part minus - The minus control.
 * @part minus-icon - The minus icon.
 * @part plus - The plus control.
 * @part plus-icon - The plus icon.
 * @part remove-icon__base - The remove icon base wrapper.
 * @part price - The price wrapper.
 * @part price__amount - The price amount.
 * @part price__description - The price description.
 * @part suffix - The suffix items.
 */
export class ScProductLineItem {
  constructor() {
    /** Is the setup fee not included in the free trial? */
    this.setupFeeTrialEnabled = true;
    /** Can we select the quantity */
    this.editable = true;
    /** The max allowed. */
    this.max = 100;
  }
  renderPriceAndInterval() {
    const setupFee = (this.fees || []).find(fee => fee.fee_type === 'setup');
    if (this.trialDurationDays) {
      return (h("div", { class: "item__price", part: "price" },
        h("div", { class: "price", part: "price__amount" }, !!setupFee && !this.setupFeeTrialEnabled ? (h(Fragment, null, setupFee === null || setupFee === void 0 ? void 0 :
          setupFee.description,
          " ",
          h("sc-format-number", { part: "price__amount", type: "currency", currency: this.currency, value: setupFee.amount }))) : (sprintf(_n('%d day free', '%d days free', this.trialDurationDays, 'surecart'), this.trialDurationDays))),
        h("div", { class: "price__description", part: "price__description" },
          /** translators: 30 days free, Then $99 per month. */
          __('Then', 'surecart'),
          ' ',
          !!this.scratchAmount && this.scratchAmount > this.amount && (h(Fragment, null,
            h("sc-format-number", { class: "item__scratch-price", part: "price__scratch", type: "currency", currency: this.currency, value: this.scratchAmount }),
            ' ')),
          h("sc-format-number", { part: "price__amount", type: "currency", currency: this.currency, value: this.amount }),
          " ",
          !!this.interval && this.interval,
          !!setupFee && !this.setupFeeTrialEnabled && sprintf(_n('starting in %d day', 'starting in %d days', this.trialDurationDays, 'surecart'), this.trialDurationDays))));
    }
    return (h("div", { class: "item__price", part: "price" },
      h("div", { class: "price", part: "price__amount" },
        !!this.scratchAmount && this.scratchAmount !== this.amount && (h(Fragment, null,
          h("sc-format-number", { class: "item__scratch-price", type: "currency", currency: this.currency, value: this.scratchAmount }),
          ' ')),
        h("sc-format-number", { type: "currency", currency: this.currency, value: this.amount })),
      !!this.interval && (h("div", { class: "price__description", part: "price__description" }, this.interval))));
  }
  render() {
    return (h("div", { class: "base", part: "base" },
      h("div", { part: "product-line-item", class: { 'item': true, 'item--has-image': !!this.imageUrl, 'item--is-rtl': isRtl() } },
        !!this.imageUrl && h("img", { part: "image", src: `${window.scData.cdn_root}fit=scale-down,format=auto,width=130/${this.imageUrl}`, class: "item__image" }),
        h("div", { class: "item__text", part: "text" },
          h("div", { class: "item__title", part: "title" },
            h("slot", { name: "title" }, this.name)),
          this.editable && (h("sc-quantity-select", { max: this.max || Infinity, exportparts: "base:quantity__base, minus, minus-icon, plus, plus-icon, input", clickEl: this.el, quantity: this.quantity, onScChange: e => e.detail && this.scUpdateQuantity.emit(e.detail) })),
          !this.editable && this.quantity > 1 && (h("span", { class: "item__description", part: "static-quantity" },
            __('Qty:', 'surecart'),
            " ",
            this.quantity))),
        h("div", { class: "item__suffix", part: "suffix" },
          this.removable ? h("sc-icon", { exportparts: "base:remove-icon__base", class: "item__remove", name: "x", onClick: () => this.scRemove.emit() }) : h("div", null),
          this.renderPriceAndInterval())),
      (this.fees || []).map(fee => {
        if (this.trialDurationDays && !this.setupFeeTrialEnabled && fee.fee_type === 'setup')
          return null;
        return (h("sc-line-item", null,
          h("sc-format-number", { slot: "price-description", type: "currency", value: fee === null || fee === void 0 ? void 0 : fee.amount, currency: this.currency || 'usd' }),
          h("span", { slot: "price-description", class: "fee__description" }, fee === null || fee === void 0 ? void 0 : fee.description)));
      })));
  }
  static get is() { return "sc-product-line-item"; }
  static get encapsulation() { return "shadow"; }
  static get originalStyleUrls() { return {
    "$": ["sc-product-line-item.scss"]
  }; }
  static get styleUrls() { return {
    "$": ["sc-product-line-item.css"]
  }; }
  static get properties() { return {
    "imageUrl": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "Url for the product image"
      },
      "attribute": "image-url",
      "reflect": false
    },
    "name": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "Product name"
      },
      "attribute": "name",
      "reflect": false
    },
    "quantity": {
      "type": "number",
      "mutable": false,
      "complexType": {
        "original": "number",
        "resolved": "number",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "Quantity"
      },
      "attribute": "quantity",
      "reflect": false
    },
    "amount": {
      "type": "number",
      "mutable": false,
      "complexType": {
        "original": "number",
        "resolved": "number",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "Product monetary amount"
      },
      "attribute": "amount",
      "reflect": false
    },
    "fees": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "Fee[]",
        "resolved": "Fee[]",
        "references": {
          "Fee": {
            "location": "import",
            "path": "../../../types"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "Product line item fees."
      }
    },
    "setupFeeTrialEnabled": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "Is the setup fee not included in the free trial?"
      },
      "attribute": "setup-fee-trial-enabled",
      "reflect": false,
      "defaultValue": "true"
    },
    "scratchAmount": {
      "type": "number",
      "mutable": false,
      "complexType": {
        "original": "number",
        "resolved": "number",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "The line item scratch amount"
      },
      "attribute": "scratch-amount",
      "reflect": false
    },
    "currency": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "Currency for the product"
      },
      "attribute": "currency",
      "reflect": false
    },
    "interval": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "Recurring interval (i.e. monthly, once, etc.)"
      },
      "attribute": "interval",
      "reflect": false
    },
    "trialDurationDays": {
      "type": "number",
      "mutable": false,
      "complexType": {
        "original": "number",
        "resolved": "number",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "Trial duration days"
      },
      "attribute": "trial-duration-days",
      "reflect": false
    },
    "removable": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "Is the line item removable"
      },
      "attribute": "removable",
      "reflect": false
    },
    "editable": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "Can we select the quantity"
      },
      "attribute": "editable",
      "reflect": false,
      "defaultValue": "true"
    },
    "max": {
      "type": "number",
      "mutable": false,
      "complexType": {
        "original": "number",
        "resolved": "number",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "The max allowed."
      },
      "attribute": "max",
      "reflect": false,
      "defaultValue": "100"
    }
  }; }
  static get events() { return [{
      "method": "scUpdateQuantity",
      "name": "scUpdateQuantity",
      "bubbles": false,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": "Emitted when the quantity changes."
      },
      "complexType": {
        "original": "number",
        "resolved": "number",
        "references": {}
      }
    }, {
      "method": "scRemove",
      "name": "scRemove",
      "bubbles": false,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": "Emitted when the quantity changes."
      },
      "complexType": {
        "original": "void",
        "resolved": "void",
        "references": {}
      }
    }]; }
  static get elementRef() { return "el"; }
}
