import { proxyCustomElement, HTMLElement, h, Fragment } from '@stencil/core/internal/client';
import { _ as __ } from './default-i18n.js';
import './index2.js';
import { a as apiFetch } from './fetch.js';
import { o as onFirstVisible } from './lazy.js';
import { i as intervalString } from './price.js';
import { d as defineCustomElement$g } from './sc-alert2.js';
import { d as defineCustomElement$f } from './sc-block-ui2.js';
import { d as defineCustomElement$e } from './sc-button2.js';
import { d as defineCustomElement$d } from './sc-choice2.js';
import { d as defineCustomElement$c } from './sc-choices2.js';
import { d as defineCustomElement$b } from './sc-dashboard-module2.js';
import { d as defineCustomElement$a } from './sc-flex2.js';
import { d as defineCustomElement$9 } from './sc-form2.js';
import { d as defineCustomElement$8 } from './sc-form-control2.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-skeleton2.js';
import { d as defineCustomElement$4 } from './sc-spinner2.js';
import { d as defineCustomElement$3 } from './sc-tag2.js';
import { d as defineCustomElement$2 } from './sc-tooltip2.js';
import { a as addQueryArgs } from './add-query-args.js';

const scSubscriptionSwitchCss = ":host{display:block;position:relative}[hidden]{display:none !important}.subscriptions-switch{display:grid;gap:0.5em}.subscriptions-switch__switcher{background:rgba(0, 0, 0, 0.035);padding:2px;line-height:1;border-radius:var(--sc-border-radius-small)}";

const ScSubscriptionSwitch$1 = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  constructor() {
    super();
    this.__registerHost();
    this.__attachShadow();
    this.filterAbove = 4;
    /** Holds the products */
    this.products = [];
    /** Filter state */
    this.filter = 'month';
  }
  componentWillLoad() {
    onFirstVisible(this.el, async () => {
      try {
        this.loading = true;
        await Promise.all([this.getGroup(), this.getProductPrices()]);
      }
      catch (e) {
        console.error(e);
        if (e === null || e === void 0 ? void 0 : e.message) {
          this.error = e.message;
        }
        else {
          this.error = __('Something went wrong', 'surecart');
        }
      }
      finally {
        this.loading = false;
      }
    });
    this.handleSubscriptionChange();
  }
  handleProductsChange() {
    var _a;
    this.prices = this.products
      .map(product => { var _a, _b; return (_b = (_a = product) === null || _a === void 0 ? void 0 : _a.prices) === null || _b === void 0 ? void 0 : _b.data; })
      .flat()
      .filter((v, i, a) => a.findIndex(t => t.id === v.id) === i) // remove duplicates.
      .filter(price => !(price === null || price === void 0 ? void 0 : price.archived)); // remove archived
    this.showFilters = ((_a = this.prices) === null || _a === void 0 ? void 0 : _a.length) > this.filterAbove;
  }
  handlePricesChange(val, prev) {
    if (!(prev === null || prev === void 0 ? void 0 : prev.length) && (val === null || val === void 0 ? void 0 : val.length)) {
      this.selectedPrice = val.find(price => { var _a, _b; return price.id === ((_b = (_a = this.subscription) === null || _a === void 0 ? void 0 : _a.price) === null || _b === void 0 ? void 0 : _b.id); });
    }
    this.hasFilters = {
      ...this.hasFilters,
      split: this.prices.some(price => price.recurring_interval === 'month' && (price === null || price === void 0 ? void 0 : price.recurring_period_count) && !(price === null || price === void 0 ? void 0 : price.archived)),
      month: this.prices.some(price => price.recurring_interval === 'month' && !(price === null || price === void 0 ? void 0 : price.recurring_period_count) && !(price === null || price === void 0 ? void 0 : price.archived)),
      year: this.prices.some(price => price.recurring_interval === 'year' && !(price === null || price === void 0 ? void 0 : price.archived)),
      never: this.prices.some(price => (price.recurring_interval === 'never' || !price.recurring_interval) && !(price === null || price === void 0 ? void 0 : price.archived)),
    };
  }
  handleSubscriptionChange() {
    var _a, _b;
    this.filter = ((_b = (_a = this.subscription) === null || _a === void 0 ? void 0 : _a.price) === null || _b === void 0 ? void 0 : _b.recurring_interval) || 'month';
  }
  /** Get all subscriptions */
  async getGroup() {
    if (!this.productGroupId)
      return;
    const products = (await await apiFetch({
      path: addQueryArgs(`surecart/v1/products/`, {
        product_group_ids: [this.productGroupId],
        expand: ['prices'],
        ...this.query,
      }),
    }));
    this.products = [...this.products, ...products];
  }
  /** Get the product's prices. */
  async getProductPrices() {
    if (!this.productId)
      return;
    const product = (await await apiFetch({
      path: addQueryArgs(`surecart/v1/products/${this.productId}`, {
        expand: ['prices'],
      }),
    }));
    this.products = [...this.products, ...[product]];
  }
  async handleSubmit(e) {
    var _a;
    const { plan } = await e.target.getFormJson();
    const price = this.prices.find(p => p.id === plan);
    const currentPlan = (_a = this.subscription) === null || _a === void 0 ? void 0 : _a.price;
    if ((price === null || price === void 0 ? void 0 : price.id) === currentPlan.id && !(price === null || price === void 0 ? void 0 : price.ad_hoc))
      return;
    // confirm ad_hoc amount.
    if (price === null || price === void 0 ? void 0 : price.ad_hoc) {
      this.busy = true;
      return window.location.assign(addQueryArgs(window.location.href, {
        action: 'confirm_amount',
        price_id: plan,
      }));
    }
    // confirm plan.
    this.busy = true;
    window.location.assign(addQueryArgs(window.location.href, {
      action: 'confirm',
      price_id: plan,
    }));
  }
  renderSwitcher() {
    const hasMultipleFilters = Object.values(this.hasFilters || {}).filter(v => !!v).length > 1;
    if (!hasMultipleFilters)
      return;
    if (!this.showFilters)
      return;
    return (h("sc-flex", { slot: "end", class: "subscriptions-switch__switcher" }, this.hasFilters.month && (h("sc-button", { onClick: () => (this.filter = 'month'), size: "small", type: this.filter === 'month' ? 'default' : 'text' }, __('Monthly', 'surecart'))), this.hasFilters.week && (h("sc-button", { onClick: () => (this.filter = 'week'), size: "small", type: this.filter === 'week' ? 'default' : 'text' }, __('Weekly', 'surecart'))), this.hasFilters.year && (h("sc-button", { onClick: () => (this.filter = 'year'), size: "small", type: this.filter === 'year' ? 'default' : 'text' }, __('Yearly', 'surecart'))), this.hasFilters.never && (h("sc-button", { onClick: () => (this.filter = 'never'), size: "small", type: this.filter === 'never' ? 'default' : 'text' }, __('Lifetime', 'surecart'))), this.hasFilters.split && (h("sc-button", { onClick: () => (this.filter = 'split'), size: "small", type: this.filter === 'split' ? 'default' : 'text' }, __('Payment Plan', 'surecart')))));
  }
  renderLoading() {
    return (h("sc-choice", { name: "loading", disabled: true }, h("sc-skeleton", { style: { width: '60px', display: 'inline-block' } }), h("sc-skeleton", { style: { width: '80px', display: 'inline-block' }, slot: "price" }), h("sc-skeleton", { style: { width: '120px', display: 'inline-block' }, slot: "description" })));
  }
  /** Is the price hidden or not */
  isHidden(price) {
    // don't hide if no filters.
    if (!this.showFilters)
      return false;
    // hide if the filter does not match the recurring interval.
    let hidden = this.filter !== price.recurring_interval;
    // if filter is never, show prices with non-recurring interval.
    if (this.filter === 'never' && !(price === null || price === void 0 ? void 0 : price.recurring_interval)) {
      hidden = false;
    }
    // if filter is split, show prices with a recurring_period_count.
    if (this.filter === 'split' && (price === null || price === void 0 ? void 0 : price.recurring_period_count)) {
      hidden = false;
    }
    return hidden;
  }
  renderContent() {
    if (this.loading) {
      return this.renderLoading();
    }
    return (h("sc-choices", { required: true }, h("div", null, (this.prices || [])
      .filter(price => !price.archived)
      .filter(price => { var _a; return (price === null || price === void 0 ? void 0 : price.currency) === ((_a = this.subscription) === null || _a === void 0 ? void 0 : _a.currency); })
      .map(price => {
      var _a, _b;
      const currentPlan = ((_b = (_a = this.subscription) === null || _a === void 0 ? void 0 : _a.price) === null || _b === void 0 ? void 0 : _b.id) === (price === null || price === void 0 ? void 0 : price.id);
      const product = this.products.find(product => product.id === (price === null || price === void 0 ? void 0 : price.product));
      return (h("sc-choice", { key: price === null || price === void 0 ? void 0 : price.id, checked: currentPlan, name: "plan", value: price === null || price === void 0 ? void 0 : price.id, hidden: this.isHidden(price), onScChange: e => {
          if (e.detail) {
            this.selectedPrice = this.prices.find(p => p.id === (price === null || price === void 0 ? void 0 : price.id));
          }
        } }, h("div", null, h("strong", null, (price === null || price === void 0 ? void 0 : price.name) || (product === null || product === void 0 ? void 0 : product.name))), h("div", { slot: "description" }, (price === null || price === void 0 ? void 0 : price.ad_hoc) ? (`${__('Custom amount', 'surecart')} ${intervalString(price)}`) : (h(Fragment, null, h("sc-format-number", { type: "currency", currency: (price === null || price === void 0 ? void 0 : price.currency) || 'usd', value: price === null || price === void 0 ? void 0 : price.amount }), " ", intervalString(price, { showOnce: true })))), currentPlan && (h("sc-tag", { type: "warning", slot: "price" }, __('Current Plan', 'surecart')))));
    }))));
  }
  buttonText() {
    var _a, _b, _c, _d;
    if ((_a = this.selectedPrice) === null || _a === void 0 ? void 0 : _a.ad_hoc) {
      if (((_b = this.selectedPrice) === null || _b === void 0 ? void 0 : _b.id) === ((_d = (_c = this.subscription) === null || _c === void 0 ? void 0 : _c.price) === null || _d === void 0 ? void 0 : _d.id)) {
        return __('Update Amount', 'surecart');
      }
      else {
        return __('Choose Amount', 'surecart');
      }
    }
    return __('Next', 'surecart');
  }
  render() {
    var _a, _b, _c, _d, _e, _f, _g, _h;
    // we are not loading and we don't have enough prices to switch.
    if (!this.loading && ((_a = this.prices) === null || _a === void 0 ? void 0 : _a.length) < 2) {
      if (!((_c = (_b = this.prices) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.ad_hoc)) {
        return null;
      }
    }
    // subscription is a payment plan.
    if ((_d = this.subscription) === null || _d === void 0 ? void 0 : _d.finite) {
      return (h("sc-alert", { type: "info", open: true }, __('To make changes to your payment plan, please contact us.', 'surecart')));
    }
    return (h("sc-dashboard-module", { heading: this.heading || __('Update Plan', 'surecart'), class: "subscription-switch", error: this.error }, h("span", { slot: "end" }, this.renderSwitcher()), h("sc-form", { class: "subscriptions-switch", onScFormSubmit: e => this.handleSubmit(e) }, this.renderContent(), h("sc-button", { type: "primary", full: true, submit: true, loading: this.loading || this.busy, disabled: ((_f = (_e = this.subscription) === null || _e === void 0 ? void 0 : _e.price) === null || _f === void 0 ? void 0 : _f.id) === ((_g = this.selectedPrice) === null || _g === void 0 ? void 0 : _g.id) && !((_h = this.selectedPrice) === null || _h === void 0 ? void 0 : _h.ad_hoc) }, this.buttonText(), " ", h("sc-icon", { name: "arrow-right", slot: "suffix" })), this.busy && h("sc-block-ui", { style: { zIndex: '9' } }))));
  }
  get el() { return this; }
  static get watchers() { return {
    "products": ["handleProductsChange"],
    "prices": ["handlePricesChange"],
    "subscription": ["handleSubscriptionChange"]
  }; }
  static get style() { return scSubscriptionSwitchCss; }
}, [1, "sc-subscription-switch", {
    "query": [16],
    "heading": [1],
    "productGroupId": [16],
    "productId": [1, "product-id"],
    "subscription": [16],
    "filterAbove": [2, "filter-above"],
    "selectedPrice": [32],
    "products": [32],
    "prices": [32],
    "filter": [32],
    "hasFilters": [32],
    "showFilters": [32],
    "loading": [32],
    "busy": [32],
    "error": [32]
  }]);
function defineCustomElement$1() {
  if (typeof customElements === "undefined") {
    return;
  }
  const components = ["sc-subscription-switch", "sc-alert", "sc-block-ui", "sc-button", "sc-choice", "sc-choices", "sc-dashboard-module", "sc-flex", "sc-form", "sc-form-control", "sc-format-number", "sc-icon", "sc-skeleton", "sc-spinner", "sc-tag", "sc-tooltip"];
  components.forEach(tagName => { switch (tagName) {
    case "sc-subscription-switch":
      if (!customElements.get(tagName)) {
        customElements.define(tagName, ScSubscriptionSwitch$1);
      }
      break;
    case "sc-alert":
      if (!customElements.get(tagName)) {
        defineCustomElement$g();
      }
      break;
    case "sc-block-ui":
      if (!customElements.get(tagName)) {
        defineCustomElement$f();
      }
      break;
    case "sc-button":
      if (!customElements.get(tagName)) {
        defineCustomElement$e();
      }
      break;
    case "sc-choice":
      if (!customElements.get(tagName)) {
        defineCustomElement$d();
      }
      break;
    case "sc-choices":
      if (!customElements.get(tagName)) {
        defineCustomElement$c();
      }
      break;
    case "sc-dashboard-module":
      if (!customElements.get(tagName)) {
        defineCustomElement$b();
      }
      break;
    case "sc-flex":
      if (!customElements.get(tagName)) {
        defineCustomElement$a();
      }
      break;
    case "sc-form":
      if (!customElements.get(tagName)) {
        defineCustomElement$9();
      }
      break;
    case "sc-form-control":
      if (!customElements.get(tagName)) {
        defineCustomElement$8();
      }
      break;
    case "sc-format-number":
      if (!customElements.get(tagName)) {
        defineCustomElement$7();
      }
      break;
    case "sc-icon":
      if (!customElements.get(tagName)) {
        defineCustomElement$6();
      }
      break;
    case "sc-skeleton":
      if (!customElements.get(tagName)) {
        defineCustomElement$5();
      }
      break;
    case "sc-spinner":
      if (!customElements.get(tagName)) {
        defineCustomElement$4();
      }
      break;
    case "sc-tag":
      if (!customElements.get(tagName)) {
        defineCustomElement$3();
      }
      break;
    case "sc-tooltip":
      if (!customElements.get(tagName)) {
        defineCustomElement$2();
      }
      break;
  } });
}
defineCustomElement$1();

const ScSubscriptionSwitch = ScSubscriptionSwitch$1;
const defineCustomElement = defineCustomElement$1;

export { ScSubscriptionSwitch, defineCustomElement };
