import { CurrencyType as P } from "./accelbyte-widgets185.mjs";
import { isAfter as p, isBefore as f } from "date-fns";
class E {
  static isPurchasable(e, t = new Date()) {
    return !this.isExpired(e, t) && !this.isNotAvailableYet(e, t);
  }
  static isDiscounted(e, t = new Date()) {
    const { price: r, discountedPrice: c } = e;
    return r === c ? !1 : !this.isDiscountExpired(e, t) && !this.isDiscountNotAvailableYet(e, t);
  }
  static isExpired(e, t = new Date()) {
    return e.expireAt === void 0 || e.expireAt === null ? !1 : p(t, new Date(e.expireAt));
  }
  static isNotAvailableYet(e, t = new Date()) {
    return e.purchaseAt === void 0 || e.purchaseAt === null ? !1 : f(t, new Date(e.purchaseAt));
  }
  static isDiscountExpirable(e) {
    return e.discountExpireAt !== void 0;
  }
  static isDiscountNotAvailableYet(e, t = new Date()) {
    return e.discountPurchaseAt === void 0 || e.discountPurchaseAt === null ? !1 : f(t, new Date(e.discountPurchaseAt));
  }
  static isDiscountExpired(e, t = new Date()) {
    return e.discountExpireAt === void 0 || e.discountExpireAt === null ? !1 : p(t, new Date(e.discountExpireAt));
  }
  static hasMatchingCurrencyInMap(e, t) {
    return !!t.get(e.currencyCode);
  }
  static calculateDiscountPercentage(e) {
    const t = e.price || 0, r = e.discountedPrice || 0;
    return t === 0 ? 0 : Math.floor((t - r) / t * 100);
  }
  static isCurrencyTypeVirtual(e) {
    return e.currencyType === P.enum.VIRTUAL;
  }
  static getFinalPrice(e) {
    return this.isDiscounted(e) ? e.discountedPrice || 0 : e.price || 0;
  }
  static finalPriceIsFree(e) {
    return this.getFinalPrice(e) <= 0;
  }
  static getFirstFreeRegionDatum(e) {
    return e.find((t) => this.finalPriceIsFree(t)) || null;
  }
  static isFree(e) {
    return !!this.getFirstFreeRegionDatum(e);
  }
  static getEstimatedPriceMap(e) {
    return (e == null ? void 0 : e.reduce(
      (t, r) => t.set(`${r.currencyNamespace}_${r.currencyCode}`, r),
      /* @__PURE__ */ new Map()
    )) || /* @__PURE__ */ new Map();
  }
  static buildRegionDataWithEstimatedPrice(e, t) {
    var n;
    if (!e.flexible)
      return e;
    const { regionData: r, ...c } = e;
    return (t == null ? void 0 : t.size) !== (r == null ? void 0 : r.length) ? c : (t && (e.regionData = r == null ? void 0 : r.map((i) => {
      const s = t.get(`${i.currencyNamespace}_${i.currencyCode}`);
      return s ? {
        ...i,
        priceDetails: s.priceDetails
      } : {};
    }), e.regionData = (n = e.regionData) == null ? void 0 : n.map((i) => {
      var u;
      const s = t.get(`${i.currencyNamespace}_${i.currencyCode}`);
      if (s) {
        let { price: d, discountedPrice: o } = s;
        if (this.isOwned(e)) {
          const a = (u = s.priceDetails) == null ? void 0 : u.reduce(
            (h, l) => {
              const { price: y, discountedPrice: A } = h;
              return {
                price: y + l.price * (l.quantity || 1),
                discountedPrice: A + l.discountedPrice * (100 - ((i == null ? void 0 : i.discountPercentage) || 0)) * (l.quantity || 1)
              };
            },
            { price: 0, discountedPrice: 0 }
          );
          a && (d = a.price, o = a.discountedPrice / 100);
        }
        return {
          ...i,
          price: d,
          discountedPrice: o
        };
      }
      return {};
    })), e);
  }
  static buildRegionDataForOwnedFlexibleBundle(e) {
    var t;
    return e.flexible ? ((t = e.regionData) == null ? void 0 : t.map((r) => {
      const { priceDetails: c } = r;
      if (c) {
        const { price: n, discountedPrice: i } = c.reduce(
          (s, u) => {
            const { price: d, discountedPrice: o } = s;
            return {
              price: (d + u.price) * (u.quantity || 1),
              discountedPrice: (o + u.discountedPrice) * (u.quantity || 1)
            };
          },
          { price: 0, discountedPrice: 0 }
        );
        return {
          ...r,
          price: n,
          discountedPrice: i
        };
      }
      return r;
    })) || [] : e.regionData || [];
  }
  static isOwned(e) {
    const { flexible: t, regionData: r } = e;
    return !!t && (r == null ? void 0 : r.filter((c) => {
      const { priceDetails: n } = c;
      return n == null ? void 0 : n.some((i) => !i.owned);
    }).length) === 0;
  }
  static getCurrentRegionData(e, t) {
    var r;
    return ((r = e.regionData) == null ? void 0 : r.filter((c) => c.currencyCode === t || !e.flexible)) || [];
  }
}
export {
  E as RegionDataItemHelper
};
