import { Component, Element, Fragment, h, Prop, State } from '@stencil/core';
import { __ } from '@wordpress/i18n';
import { addQueryArgs } from '@wordpress/url';
import apiFetch from '../../../../functions/fetch';
import { onFirstVisible } from '../../../../functions/lazy';
import { intervalString } from '../../../../functions/price';
import { formatTaxDisplay } from '../../../../functions/tax';
export class ScUpcomingInvoice {
  constructor() {
    this.quantityUpdatesEnabled = true;
  }
  componentWillLoad() {
    onFirstVisible(this.el, () => {
      this.fetchItems();
    });
  }
  isFutureInvoice() {
    return this.invoice.start_at >= new Date().getTime() / 1000;
  }
  async fetchItems() {
    var _a, _b;
    try {
      this.loading = true;
      await Promise.all([this.getInvoice(), this.getPrice()]);
    }
    catch (e) {
      console.error(e);
      this.error = ((_b = (_a = e === null || e === void 0 ? void 0 : e.additional_errors) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.message) || (e === null || e === void 0 ? void 0 : e.message) || __('Something went wrong', 'surecart');
    }
    finally {
      this.loading = false;
    }
  }
  async getPrice() {
    if (!this.priceId)
      return;
    this.price = (await apiFetch({
      path: addQueryArgs(`surecart/v1/prices/${this.priceId}`, {
        expand: ['product'],
      }),
    }));
  }
  async getInvoice() {
    if (!this.subscriptionId)
      return;
    this.invoice = (await apiFetch({
      method: 'PATCH',
      path: addQueryArgs(`surecart/v1/subscriptions/${this.subscriptionId}/upcoming_period/`, {
        expand: [
          'period.checkout',
          'checkout.line_items',
          'line_item.price',
          'price.product',
          'checkout.payment_method',
          'checkout.discount',
          'discount.promotion',
          'discount.coupon',
          'payment_method.card',
          'payment_method.payment_instrument',
          'payment_method.paypal_account',
          'payment_method.bank_account',
        ],
      }),
      data: {
        price: this.priceId,
        quantity: this.quantity,
        ...(this.adHocAmount ? { ad_hoc_amount: this.adHocAmount } : {}),
        ...(this.discount ? { discount: this.discount } : {}),
      },
    }));
    return this.invoice;
  }
  async applyCoupon(e) {
    try {
      this.couponError = '';
      this.busy = true;
      this.discount = {
        promotion_code: e.detail,
      };
      await this.getInvoice();
    }
    catch (e) {
      this.couponError = (e === null || e === void 0 ? void 0 : e.message) || __('Something went wrong', 'surecart');
    }
    finally {
      this.busy = false;
    }
  }
  async updateQuantity(e) {
    try {
      this.error = '';
      this.busy = true;
      this.quantity = e.detail;
      await this.getInvoice();
    }
    catch (e) {
      this.error = (e === null || e === void 0 ? void 0 : e.message) || __('Something went wrong', 'surecart');
    }
    finally {
      this.busy = false;
    }
  }
  async onSubmit() {
    try {
      this.error = '';
      this.busy = true;
      await apiFetch({
        path: `surecart/v1/subscriptions/${this.subscriptionId}`,
        method: 'PATCH',
        data: {
          price: this.priceId,
          quantity: this.quantity,
          ...(this.adHocAmount ? { ad_hoc_amount: this.adHocAmount } : {}),
          ...(this.discount ? { discount: this.discount } : {}),
        },
      });
      if (this.successUrl) {
        window.location.assign(this.successUrl);
      }
      else {
        this.busy = false;
      }
    }
    catch (e) {
      this.error = (e === null || e === void 0 ? void 0 : e.message) || __('Something went wrong', 'surecart');
      this.busy = false;
    }
  }
  renderName(price) {
    var _a;
    if (typeof (price === null || price === void 0 ? void 0 : price.product) !== 'string') {
      return (_a = price === null || price === void 0 ? void 0 : price.product) === null || _a === void 0 ? void 0 : _a.name;
    }
    return __('Plan', 'surecart');
  }
  renderRenewalText() {
    var _a;
    if (this.isFutureInvoice()) {
      return (h("div", null,
        __("You'll be switched to this plan", 'surecart'),
        ' ',
        h("strong", null,
          __('at the end of your billing cycle on', 'surecart'),
          ' ',
          h("sc-format-date", { type: "timestamp", date: (_a = this.invoice) === null || _a === void 0 ? void 0 : _a.start_at, month: "short", day: "numeric", year: "numeric" }))));
    }
    return (h("div", null,
      __("You'll be switched to this plan", 'surecart'),
      " ",
      h("strong", null, __('immediately', 'surecart'))));
  }
  renderEmpty() {
    return h("slot", { name: "empty" }, __('Something went wrong.', 'surecart'));
  }
  renderLoading() {
    return (h("div", null,
      h("sc-skeleton", { style: { width: '30%', marginBottom: '0.75em' } }),
      h("sc-skeleton", { style: { width: '20%', marginBottom: '0.75em' } }),
      h("sc-skeleton", { style: { width: '40%' } })));
  }
  renderContent() {
    var _a;
    if (this.loading) {
      return this.renderLoading();
    }
    if (!((_a = this.invoice) === null || _a === void 0 ? void 0 : _a.checkout)) {
      return this.renderEmpty();
    }
    const checkout = this.invoice.checkout;
    return (h("div", { class: "new-plan" },
      h("div", { class: "new-plan__heading" }, this.renderName(this.price)),
      h("div", null,
        h("sc-format-number", { type: "currency", currency: checkout === null || checkout === void 0 ? void 0 : checkout.currency, value: checkout === null || checkout === void 0 ? void 0 : checkout.total_amount }),
        " ",
        intervalString(this.price)),
      h("div", { style: { fontSize: 'var(--sc-font-size-small)' } }, this.renderRenewalText())));
  }
  renderSummary() {
    var _a, _b;
    if (this.loading) {
      return this.renderLoading();
    }
    if (!this.invoice) {
      return this.renderEmpty();
    }
    const checkout = (_a = this.invoice) === null || _a === void 0 ? void 0 : _a.checkout;
    return (h(Fragment, null, (_b = checkout === null || checkout === void 0 ? void 0 : checkout.line_items) === null || _b === void 0 ? void 0 :
      _b.data.map(item => {
        var _a, _b, _c, _d, _e;
        return (h("sc-product-line-item", { imageUrl: (_b = (_a = item.price) === null || _a === void 0 ? void 0 : _a.product) === null || _b === void 0 ? void 0 : _b.image_url, name: (_d = (_c = item.price) === null || _c === void 0 ? void 0 : _c.product) === null || _d === void 0 ? void 0 : _d.name, editable: this.quantityUpdatesEnabled, removable: false, quantity: item === null || item === void 0 ? void 0 : item.quantity, amount: item === null || item === void 0 ? void 0 : item.total_amount, currency: (_e = item === null || item === void 0 ? void 0 : item.price) === null || _e === void 0 ? void 0 : _e.currency, interval: intervalString(item === null || item === void 0 ? void 0 : item.price), onScUpdateQuantity: e => this.updateQuantity(e) }));
      }),
      h("sc-line-item", null,
        h("span", { slot: "description" }, __('Subtotal', 'surecart')),
        h("sc-format-number", { slot: "price", type: "currency", currency: checkout === null || checkout === void 0 ? void 0 : checkout.currency, value: checkout === null || checkout === void 0 ? void 0 : checkout.subtotal_amount })),
      !!checkout.proration_amount && (h("sc-line-item", null,
        h("span", { slot: "description" }, __('Proration Credit', 'surecart')),
        h("sc-format-number", { slot: "price", type: "currency", currency: checkout === null || checkout === void 0 ? void 0 : checkout.currency, value: -(checkout === null || checkout === void 0 ? void 0 : checkout.proration_amount) }))),
      !!checkout.applied_balance_amount && (h("sc-line-item", null,
        h("span", { slot: "description" }, __('Applied Balance', 'surecart')),
        h("sc-format-number", { slot: "price", type: "currency", currency: checkout === null || checkout === void 0 ? void 0 : checkout.currency, value: -(checkout === null || checkout === void 0 ? void 0 : checkout.applied_balance_amount) }))),
      !!checkout.trial_amount && (h("sc-line-item", null,
        h("span", { slot: "description" }, __('Free Trial', 'surecart')),
        h("sc-format-number", { slot: "price", type: "currency", currency: checkout === null || checkout === void 0 ? void 0 : checkout.currency, value: checkout === null || checkout === void 0 ? void 0 : checkout.trial_amount }))),
      h("sc-coupon-form", { discount: checkout === null || checkout === void 0 ? void 0 : checkout.discount, label: __('Add Coupon Code'), onScApplyCoupon: e => this.applyCoupon(e), error: this.couponError, collapsed: true, buttonText: __('Add Coupon Code') }),
      !!checkout.tax_amount && (h("sc-line-item", null,
        h("span", { slot: "description" }, formatTaxDisplay(checkout === null || checkout === void 0 ? void 0 : checkout.tax_label)),
        h("sc-format-number", { slot: "price", type: "currency", currency: checkout === null || checkout === void 0 ? void 0 : checkout.currency, value: checkout === null || checkout === void 0 ? void 0 : checkout.tax_amount }))),
      h("sc-divider", { style: { '--spacing': '0' } }),
      h("sc-line-item", null,
        h("span", { slot: "description" }, __('Payment', 'surecart')),
        h("a", { href: addQueryArgs(window.location.href, {
            action: 'payment',
          }), slot: "price-description" },
          h("sc-flex", { "justify-content": "flex-start", "align-items": "center", style: { '--spacing': '0.5em' } },
            h("sc-payment-method", { paymentMethod: checkout === null || checkout === void 0 ? void 0 : checkout.payment_method }),
            h("sc-icon", { name: "edit-3" })))),
      h("sc-line-item", { style: { '--price-size': 'var(--sc-font-size-x-large)' } },
        h("span", { slot: "title" }, __('Total Due', 'surecart')),
        h("sc-format-number", { slot: "price", type: "currency", currency: checkout === null || checkout === void 0 ? void 0 : checkout.currency, value: checkout === null || checkout === void 0 ? void 0 : checkout.amount_due }),
        h("span", { slot: "currency" }, checkout.currency))));
  }
  render() {
    return (h("div", { class: "upcoming-invoice" },
      this.error && (h("sc-alert", { open: !!this.error, type: "danger" },
        h("span", { slot: "title" }, __('Error', 'surecart')),
        this.error)),
      h(Fragment, null,
        h("sc-dashboard-module", { heading: __('New Plan', 'surecart'), class: "plan-preview", error: this.error },
          h("sc-card", null, this.renderContent())),
        h("sc-dashboard-module", { heading: __('Summary', 'surecart'), class: "plan-summary" },
          h("sc-form", { onScFormSubmit: () => this.onSubmit() },
            h("sc-card", null, this.renderSummary()),
            h("sc-button", { type: "primary", full: true, submit: true, loading: this.loading || this.busy, disabled: this.loading || this.busy }, __('Confirm', 'surecart')))),
        h("sc-text", { style: { '--text-align': 'center', '--font-size': 'var(--sc-font-size-small)', '--line-height': 'var(--sc-line-height-normal)' } },
          h("slot", { name: "terms" }))),
      this.busy && h("sc-block-ui", null)));
  }
  static get is() { return "sc-upcoming-invoice"; }
  static get encapsulation() { return "shadow"; }
  static get originalStyleUrls() { return {
    "$": ["sc-upcoming-invoice.scss"]
  }; }
  static get styleUrls() { return {
    "$": ["sc-upcoming-invoice.css"]
  }; }
  static get properties() { return {
    "heading": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "heading",
      "reflect": false
    },
    "successUrl": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "success-url",
      "reflect": false
    },
    "subscriptionId": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "subscription-id",
      "reflect": false
    },
    "priceId": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "price-id",
      "reflect": false
    },
    "quantity": {
      "type": "number",
      "mutable": false,
      "complexType": {
        "original": "number",
        "resolved": "number",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "quantity",
      "reflect": false
    },
    "discount": {
      "type": "unknown",
      "mutable": true,
      "complexType": {
        "original": "{\n    promotion_code?: string;\n    coupon?: string;\n  }",
        "resolved": "{ promotion_code?: string; coupon?: string; }",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "payment_method": {
      "type": "unknown",
      "mutable": true,
      "complexType": {
        "original": "PaymentMethod",
        "resolved": "PaymentMethod",
        "references": {
          "PaymentMethod": {
            "location": "import",
            "path": "../../../../types"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "quantityUpdatesEnabled": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "quantity-updates-enabled",
      "reflect": false,
      "defaultValue": "true"
    },
    "adHocAmount": {
      "type": "number",
      "mutable": false,
      "complexType": {
        "original": "number",
        "resolved": "number",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "ad-hoc-amount",
      "reflect": false
    }
  }; }
  static get states() { return {
    "loading": {},
    "busy": {},
    "error": {},
    "price": {},
    "invoice": {},
    "couponError": {}
  }; }
  static get elementRef() { return "el"; }
}
