@nextblock-cms/ecom 0.8.1 → 0.8.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.cjs.js +1 -1
- package/index.es.js +136 -3224
- package/lib/CurrencyProvider.cjs.js +1 -0
- package/lib/CurrencyProvider.es.js +99 -0
- package/lib/cart-store.cjs.js +1 -0
- package/lib/cart-store.es.js +116 -0
- package/lib/components/AccountNavigationMenu.cjs.js +1 -0
- package/lib/components/AccountNavigationMenu.es.js +44 -0
- package/lib/components/AddToCartButton.cjs.js +1 -0
- package/lib/components/AddToCartButton.es.js +60 -0
- package/lib/components/Cart.cjs.js +1 -0
- package/lib/components/Cart.es.js +148 -0
- package/lib/components/CartDrawer.cjs.js +1 -0
- package/lib/components/CartDrawer.es.js +119 -0
- package/lib/components/CartIcon.cjs.js +1 -0
- package/lib/components/CartIcon.es.js +34 -0
- package/lib/components/Checkout.cjs.js +1 -0
- package/lib/components/Checkout.es.js +987 -0
- package/lib/components/CouponForm.cjs.js +1 -0
- package/lib/components/CouponForm.es.js +136 -0
- package/lib/components/CurrencySwitcher.cjs.js +1 -0
- package/lib/components/CurrencySwitcher.es.js +18 -0
- package/lib/components/CustomerProfileForm.cjs.js +1 -0
- package/lib/components/CustomerProfileForm.es.js +355 -0
- package/lib/components/FeaturedProduct.cjs.js +1 -0
- package/lib/components/FeaturedProduct.es.js +72 -0
- package/lib/components/InvoiceDocument.cjs.js +1 -0
- package/lib/components/InvoiceDocument.es.js +196 -0
- package/lib/components/InvoiceViewerShell.cjs.js +29 -0
- package/lib/components/InvoiceViewerShell.es.js +100 -0
- package/lib/components/ProductCard.cjs.js +1 -0
- package/lib/components/ProductCard.es.js +119 -0
- package/lib/components/ProductDetailsLayout.cjs.js +1 -0
- package/lib/components/ProductDetailsLayout.es.js +361 -0
- package/lib/components/ProductGallery.cjs.js +1 -0
- package/lib/components/ProductGallery.es.js +41 -0
- package/lib/components/ProductGrid.cjs.js +1 -0
- package/lib/components/ProductGrid.es.js +19 -0
- package/lib/components/ShippingEstimator.cjs.js +1 -0
- package/lib/components/ShippingEstimator.es.js +125 -0
- package/lib/components/SimpleTiptapRenderer.cjs.js +1 -0
- package/lib/components/SimpleTiptapRenderer.es.js +187 -0
- package/lib/components/SubscriptionSelector.cjs.js +1 -0
- package/lib/components/SubscriptionSelector.es.js +140 -0
- package/lib/countries.cjs.js +1 -0
- package/lib/countries.es.js +77 -0
- package/lib/coupon-server.cjs.js +1 -0
- package/lib/coupon-server.es.js +234 -0
- package/lib/coupons.cjs.js +1 -0
- package/lib/coupons.es.js +17 -0
- package/lib/currency-constants.cjs.js +1 -0
- package/lib/currency-constants.es.js +4 -0
- package/lib/currency-store.cjs.js +1 -0
- package/lib/currency-store.es.js +27 -0
- package/lib/currency-sync.cjs.js +1 -0
- package/lib/currency-sync.es.js +180 -0
- package/lib/currency.cjs.js +1 -0
- package/lib/currency.es.js +312 -0
- package/lib/customer-addresses.cjs.js +1 -0
- package/lib/customer-addresses.es.js +116 -0
- package/lib/customer-orders.cjs.js +1 -0
- package/lib/customer-orders.es.js +45 -0
- package/lib/customer.cjs.js +1 -0
- package/lib/customer.es.js +58 -0
- package/lib/export-helpers.cjs.js +1 -0
- package/lib/export-helpers.es.js +56 -0
- package/lib/factory.cjs.js +1 -0
- package/lib/factory.es.js +8 -0
- package/lib/freemius-coupons.cjs.js +1 -0
- package/lib/freemius-coupons.es.js +272 -0
- package/lib/freemius-order-sync.cjs.js +1 -0
- package/lib/freemius-order-sync.es.js +284 -0
- package/lib/inventory-settings.cjs.js +1 -0
- package/lib/inventory-settings.es.js +86 -0
- package/lib/invoice-server.cjs.js +33 -0
- package/lib/invoice-server.es.js +141 -0
- package/lib/invoice-ui.cjs.js +1 -0
- package/lib/invoice-ui.es.js +71 -0
- package/lib/invoice.cjs.js +1 -0
- package/lib/invoice.es.js +102 -0
- package/lib/order-inventory.cjs.js +61 -0
- package/lib/order-inventory.es.js +128 -0
- package/lib/order-tax-details.cjs.js +1 -0
- package/lib/order-tax-details.es.js +164 -0
- package/lib/pages/cms/coupons/CouponEditorForm.cjs.js +1 -0
- package/lib/pages/cms/coupons/CouponEditorForm.es.js +189 -0
- package/lib/pages/cms/coupons/CouponsPage.cjs.js +5 -0
- package/lib/pages/cms/coupons/CouponsPage.es.js +171 -0
- package/lib/pages/cms/coupons/CreateCouponDialog.cjs.js +1 -0
- package/lib/pages/cms/coupons/CreateCouponDialog.es.js +35 -0
- package/lib/pages/cms/coupons/EditCouponPage.cjs.js +1 -0
- package/lib/pages/cms/coupons/EditCouponPage.es.js +48 -0
- package/lib/pages/cms/coupons/ProductScopePicker.cjs.js +1 -0
- package/lib/pages/cms/coupons/ProductScopePicker.es.js +120 -0
- package/lib/pages/cms/coupons/actions.cjs.js +1 -0
- package/lib/pages/cms/coupons/actions.es.js +128 -0
- package/lib/pages/cms/coupons/product-options.cjs.js +1 -0
- package/lib/pages/cms/coupons/product-options.es.js +27 -0
- package/lib/pages/cms/orders/ExportReportsDialog.cjs.js +1 -0
- package/lib/pages/cms/orders/ExportReportsDialog.es.js +130 -0
- package/lib/pages/cms/orders/OrderDetailPage.cjs.js +1 -0
- package/lib/pages/cms/orders/OrderDetailPage.es.js +137 -0
- package/lib/pages/cms/orders/OrderPrintButton.cjs.js +1 -0
- package/lib/pages/cms/orders/OrderPrintButton.es.js +22 -0
- package/lib/pages/cms/orders/OrderStatusForm.cjs.js +1 -0
- package/lib/pages/cms/orders/OrderStatusForm.es.js +71 -0
- package/lib/pages/cms/orders/OrdersPage.cjs.js +1 -0
- package/lib/pages/cms/orders/OrdersPage.es.js +104 -0
- package/lib/pages/cms/orders/actions.cjs.js +7 -0
- package/lib/pages/cms/orders/actions.es.js +71 -0
- package/lib/pages/cms/orders/export-actions.cjs.js +1 -0
- package/lib/pages/cms/orders/export-actions.es.js +27 -0
- package/lib/pages/cms/orders/server-actions.cjs.js +1 -0
- package/lib/pages/cms/orders/server-actions.es.js +55 -0
- package/lib/pages/cms/payments/PaymentsClient.cjs.js +1 -0
- package/lib/pages/cms/payments/PaymentsClient.es.js +188 -0
- package/lib/pages/cms/payments/PaymentsPage.cjs.js +1 -0
- package/lib/pages/cms/payments/PaymentsPage.es.js +29 -0
- package/lib/pages/cms/payments/actions.cjs.js +1 -0
- package/lib/pages/cms/payments/actions.es.js +23 -0
- package/lib/pages/cms/payments/queries.cjs.js +1 -0
- package/lib/pages/cms/payments/queries.es.js +30 -0
- package/lib/pages/cms/products/ProductsPage.cjs.js +1 -0
- package/lib/pages/cms/products/ProductsPage.es.js +46 -0
- package/lib/pages/cms/products/_id_/edit/EditProductPage.cjs.js +1 -0
- package/lib/pages/cms/products/_id_/edit/EditProductPage.es.js +156 -0
- package/lib/pages/cms/products/actions.cjs.js +44 -0
- package/lib/pages/cms/products/actions.es.js +175 -0
- package/lib/pages/cms/products/attributes/AttributeManagementPage.cjs.js +1 -0
- package/lib/pages/cms/products/attributes/AttributeManagementPage.es.js +48 -0
- package/lib/pages/cms/products/attributes/components/AttributeManager.cjs.js +1 -0
- package/lib/pages/cms/products/attributes/components/AttributeManager.es.js +291 -0
- package/lib/pages/cms/products/categories/CategoryManagementPage.cjs.js +1 -0
- package/lib/pages/cms/products/categories/CategoryManagementPage.es.js +36 -0
- package/lib/pages/cms/products/categories/components/CategoryManager.cjs.js +1 -0
- package/lib/pages/cms/products/categories/components/CategoryManager.es.js +263 -0
- package/lib/pages/cms/products/components/CopyProductFromLanguage.cjs.js +1 -0
- package/lib/pages/cms/products/components/CopyProductFromLanguage.es.js +114 -0
- package/lib/pages/cms/products/components/CurrencyPriceFields.cjs.js +1 -0
- package/lib/pages/cms/products/components/CurrencyPriceFields.es.js +107 -0
- package/lib/pages/cms/products/components/DeleteProductButton.cjs.js +1 -0
- package/lib/pages/cms/products/components/DeleteProductButton.es.js +48 -0
- package/lib/pages/cms/products/components/FreemiusPricingDashboard.cjs.js +1 -0
- package/lib/pages/cms/products/components/FreemiusPricingDashboard.es.js +86 -0
- package/lib/pages/cms/products/components/ProductCategorySelector.cjs.js +1 -0
- package/lib/pages/cms/products/components/ProductCategorySelector.es.js +160 -0
- package/lib/pages/cms/products/components/ProductForm.cjs.js +1 -0
- package/lib/pages/cms/products/components/ProductForm.es.js +702 -0
- package/lib/pages/cms/products/components/ProductMediaManager.cjs.js +1 -0
- package/lib/pages/cms/products/components/ProductMediaManager.es.js +104 -0
- package/lib/pages/cms/products/components/ProductsBulkTable.cjs.js +1 -0
- package/lib/pages/cms/products/components/ProductsBulkTable.es.js +228 -0
- package/lib/pages/cms/products/components/SaleScheduleFields.cjs.js +1 -0
- package/lib/pages/cms/products/components/SaleScheduleFields.es.js +137 -0
- package/lib/pages/cms/products/components/SyncFreemiusButton.cjs.js +1 -0
- package/lib/pages/cms/products/components/SyncFreemiusButton.es.js +36 -0
- package/lib/pages/cms/products/components/SyncFreemiusPricingButton.cjs.js +1 -0
- package/lib/pages/cms/products/components/SyncFreemiusPricingButton.es.js +40 -0
- package/lib/pages/cms/products/components/VariationsEditor.cjs.js +1 -0
- package/lib/pages/cms/products/components/VariationsEditor.es.js +408 -0
- package/lib/pages/cms/products/inventory/InventoryPage.cjs.js +1 -0
- package/lib/pages/cms/products/inventory/InventoryPage.es.js +16 -0
- package/lib/pages/cms/products/inventory/InventoryTableClient.cjs.js +2 -0
- package/lib/pages/cms/products/inventory/InventoryTableClient.es.js +214 -0
- package/lib/pages/cms/products/inventory/actions.cjs.js +3 -0
- package/lib/pages/cms/products/inventory/actions.es.js +95 -0
- package/lib/pages/cms/products/new/NewProductPage.cjs.js +1 -0
- package/lib/pages/cms/products/new/NewProductPage.es.js +76 -0
- package/lib/pages/cms/products/product-price-sync.cjs.js +1 -0
- package/lib/pages/cms/products/product-price-sync.es.js +98 -0
- package/lib/pages/cms/products/server-actions.cjs.js +1 -0
- package/lib/pages/cms/products/server-actions.es.js +193 -0
- package/lib/pages/cms/shipping/ShippingPage.cjs.js +5 -0
- package/lib/pages/cms/shipping/ShippingPage.es.js +211 -0
- package/lib/pages/cms/shipping/components/RateForm.cjs.js +1 -0
- package/lib/pages/cms/shipping/components/RateForm.es.js +441 -0
- package/lib/pages/cms/shipping/components/ZoneForm.cjs.js +1 -0
- package/lib/pages/cms/shipping/components/ZoneForm.es.js +228 -0
- package/lib/pages/cms/shipping/server-actions.cjs.js +1 -0
- package/lib/pages/cms/shipping/server-actions.es.js +159 -0
- package/lib/pages/cms/taxes/TaxesPage.cjs.js +1 -0
- package/lib/pages/cms/taxes/TaxesPage.es.js +172 -0
- package/lib/pages/cms/taxes/actions.cjs.js +1 -0
- package/lib/pages/cms/taxes/actions.es.js +57 -0
- package/lib/pages/cms/taxes/components/TaxRateForm.cjs.js +1 -0
- package/lib/pages/cms/taxes/components/TaxRateForm.es.js +100 -0
- package/lib/product-actions.cjs.js +237 -0
- package/lib/product-actions.es.js +524 -0
- package/lib/product-context.cjs.js +1 -0
- package/lib/product-context.es.js +16 -0
- package/lib/product-schema.cjs.js +1 -0
- package/lib/product-schema.es.js +134 -0
- package/lib/providers/freemius.cjs.js +5 -0
- package/lib/providers/freemius.es.js +538 -0
- package/lib/providers/stripe.cjs.js +1 -0
- package/lib/providers/stripe.es.js +473 -0
- package/lib/server-actions/coupon-actions.cjs.js +1 -0
- package/lib/server-actions/coupon-actions.es.js +23 -0
- package/lib/server-actions/customer-actions.cjs.js +1 -0
- package/lib/server-actions/customer-actions.es.js +30 -0
- package/lib/server-actions/product-actions.cjs.js +1 -0
- package/lib/server-actions/product-actions.es.js +16 -0
- package/lib/server-actions/shipping-actions.cjs.js +1 -0
- package/lib/server-actions/shipping-actions.es.js +27 -0
- package/lib/server-actions/tax-actions.cjs.js +1 -0
- package/lib/server-actions/tax-actions.es.js +40 -0
- package/lib/shared-inventory.cjs.js +15 -0
- package/lib/shared-inventory.es.js +171 -0
- package/lib/shipping/resolver.cjs.js +7 -0
- package/lib/shipping/resolver.es.js +62 -0
- package/lib/shipping-rate-currency.cjs.js +1 -0
- package/lib/shipping-rate-currency.es.js +72 -0
- package/lib/states.cjs.js +1 -0
- package/lib/states.es.js +127 -0
- package/lib/stripe/checkout.cjs.js +1 -0
- package/lib/stripe/checkout.es.js +182 -0
- package/lib/stripe/client.cjs.js +1 -0
- package/lib/stripe/client.es.js +7 -0
- package/lib/stripe/order-sync.cjs.js +1 -0
- package/lib/stripe/order-sync.es.js +130 -0
- package/lib/stripe/webhooks.cjs.js +1 -0
- package/lib/stripe/webhooks.es.js +30 -0
- package/lib/tax-calculation.cjs.js +1 -0
- package/lib/tax-calculation.es.js +101 -0
- package/lib/trials.cjs.js +1 -0
- package/lib/trials.es.js +29 -0
- package/lib/types.cjs.js +1 -0
- package/lib/types.es.js +33 -0
- package/lib/use-cart.cjs.js +1 -0
- package/lib/use-cart.es.js +44 -0
- package/lib/variation-utils.cjs.js +1 -0
- package/lib/variation-utils.es.js +246 -0
- package/lib/zod-config.cjs.js +1 -0
- package/lib/zod-config.es.js +6 -0
- package/package.json +10 -5
- package/server.cjs.js +1 -110
- package/server.es.js +178 -6037
- package/invoice-ui-BJZfedDd.cjs +0 -332
- package/invoice-ui-GJ1ADAjn.js +0 -5759
- /package/{currency-rest-client-uolJxUkL.cjs → lib/currency-rest-client.cjs.js} +0 -0
- /package/{currency-rest-client-CwoqdgAP.js → lib/currency-rest-client.es.js} +0 -0
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import { normalizeCouponCode as S, getCartLineCouponKey as q, emptyProviderDiscounts as E } from "./coupons.es.js";
|
|
2
|
+
import { getDefaultCurrency as x, resolveEffectivePriceForCurrency as A, normalizePriceMap as C, normalizeSalePriceMap as D, normalizeCurrencyRecord as T } from "./currency.es.js";
|
|
3
|
+
function M(e) {
|
|
4
|
+
if (!e)
|
|
5
|
+
return null;
|
|
6
|
+
const t = new Date(e);
|
|
7
|
+
return Number.isNaN(t.getTime()) ? null : t;
|
|
8
|
+
}
|
|
9
|
+
function k(e, t) {
|
|
10
|
+
return e === "all" || e === t;
|
|
11
|
+
}
|
|
12
|
+
function I(e) {
|
|
13
|
+
return e === "stripe" || e === "freemius" ? e : null;
|
|
14
|
+
}
|
|
15
|
+
async function K(e) {
|
|
16
|
+
const { data: t, error: c } = await e.from("currencies").select(
|
|
17
|
+
"code, symbol, exchange_rate, is_default, is_active, auto_sync_product_prices, auto_update_exchange_rate, exchange_rate_source, exchange_rate_updated_at, rounding_mode, rounding_increment, rounding_charm_amount"
|
|
18
|
+
).eq("is_active", !0).order("code", { ascending: !0 });
|
|
19
|
+
if (c)
|
|
20
|
+
throw new Error(c.message);
|
|
21
|
+
return (t || []).map((s) => T(s));
|
|
22
|
+
}
|
|
23
|
+
async function L(e) {
|
|
24
|
+
const { client: t, items: c } = e, s = await K(t);
|
|
25
|
+
if (s.length === 0)
|
|
26
|
+
throw new Error("No active currencies are configured.");
|
|
27
|
+
const r = s.find((n) => n.code === (e.currencyCode || "").toUpperCase()) ?? x(s), u = [...new Set(c.map((n) => n.product_id).filter(Boolean))], i = [
|
|
28
|
+
...new Set(
|
|
29
|
+
c.map((n) => n.variant_id).filter((n) => !!n)
|
|
30
|
+
)
|
|
31
|
+
];
|
|
32
|
+
if (u.length === 0)
|
|
33
|
+
return {
|
|
34
|
+
lines: [],
|
|
35
|
+
currencies: s,
|
|
36
|
+
selectedCurrency: r
|
|
37
|
+
};
|
|
38
|
+
const [{ data: p, error: f }, a] = await Promise.all([
|
|
39
|
+
t.from("products").select(
|
|
40
|
+
"id, title, price, prices, sale_price, sale_prices, sale_start_at, sale_end_at, scheduled_price, scheduled_prices, scheduled_price_at, product_type, payment_provider, freemius_product_id, freemius_plan_id"
|
|
41
|
+
).in("id", u),
|
|
42
|
+
i.length ? t.from("product_variants").select("id, product_id, price, prices, sale_price, sale_prices, sale_start_at, sale_end_at, scheduled_price, scheduled_prices, scheduled_price_at").in("id", i) : Promise.resolve({ data: [], error: null })
|
|
43
|
+
]);
|
|
44
|
+
if (f)
|
|
45
|
+
throw new Error(f.message);
|
|
46
|
+
if (a.error)
|
|
47
|
+
throw new Error(a.error.message);
|
|
48
|
+
const g = p || [], _ = a.data || [], v = new Map(
|
|
49
|
+
g.map((n) => [n.id, n])
|
|
50
|
+
), m = new Map(
|
|
51
|
+
_.map((n) => [n.id, n])
|
|
52
|
+
), h = [];
|
|
53
|
+
for (const n of c) {
|
|
54
|
+
const o = v.get(n.product_id);
|
|
55
|
+
if (!o)
|
|
56
|
+
continue;
|
|
57
|
+
const l = I(o.payment_provider) ?? (o.product_type === "digital" ? "freemius" : "stripe"), y = n.variant_id ? m.get(n.variant_id) : null, d = y && y.product_id === o.id ? y : o, w = A({
|
|
58
|
+
prices: C(d.prices),
|
|
59
|
+
salePrices: D(d.sale_prices),
|
|
60
|
+
fallbackPrice: d.price,
|
|
61
|
+
fallbackSalePrice: d.sale_price,
|
|
62
|
+
saleStartAt: d.sale_start_at,
|
|
63
|
+
saleEndAt: d.sale_end_at,
|
|
64
|
+
scheduledPrice: d.scheduled_price,
|
|
65
|
+
scheduledPrices: C(d.scheduled_prices),
|
|
66
|
+
scheduledPriceAt: d.scheduled_price_at,
|
|
67
|
+
currencyCode: r.code,
|
|
68
|
+
currencies: s
|
|
69
|
+
}), b = l === "freemius" ? 1 : Math.max(1, Number(n.quantity) || 1), P = Math.max(0, w.sale_price ?? w.price);
|
|
70
|
+
h.push({
|
|
71
|
+
key: q(n),
|
|
72
|
+
product_id: o.id,
|
|
73
|
+
variant_id: y?.id ?? null,
|
|
74
|
+
title: o.title,
|
|
75
|
+
quantity: b,
|
|
76
|
+
provider: l,
|
|
77
|
+
subtotal: P * b,
|
|
78
|
+
freemius_product_id: o.freemius_product_id ?? null,
|
|
79
|
+
freemius_plan_id: o.freemius_plan_id ?? null
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
return {
|
|
83
|
+
lines: h,
|
|
84
|
+
currencies: s,
|
|
85
|
+
selectedCurrency: r
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
async function R(e, t) {
|
|
89
|
+
const { data: c, error: s } = await e.from("coupon_products").select("product_id").eq("coupon_id", t);
|
|
90
|
+
if (s)
|
|
91
|
+
throw new Error(s.message);
|
|
92
|
+
return new Set((c || []).map((r) => r.product_id));
|
|
93
|
+
}
|
|
94
|
+
function z(e, t) {
|
|
95
|
+
const c = e.reduce((i, p) => i + p.subtotal, 0), s = Math.min(t, c);
|
|
96
|
+
if (c <= 0 || s <= 0)
|
|
97
|
+
return /* @__PURE__ */ new Map();
|
|
98
|
+
const r = /* @__PURE__ */ new Map();
|
|
99
|
+
let u = 0;
|
|
100
|
+
return e.forEach((i, p) => {
|
|
101
|
+
const a = p === e.length - 1 ? s - u : Math.min(
|
|
102
|
+
i.subtotal,
|
|
103
|
+
Math.floor(s * i.subtotal / c)
|
|
104
|
+
);
|
|
105
|
+
u += a, r.set(i.key, a);
|
|
106
|
+
}), r;
|
|
107
|
+
}
|
|
108
|
+
async function B(e) {
|
|
109
|
+
const t = S(e.code);
|
|
110
|
+
if (!t)
|
|
111
|
+
return {
|
|
112
|
+
success: !1,
|
|
113
|
+
error: "Enter a coupon code.",
|
|
114
|
+
errorKey: "ecommerce.coupon_code_required"
|
|
115
|
+
};
|
|
116
|
+
if (!e.items.length)
|
|
117
|
+
return {
|
|
118
|
+
success: !1,
|
|
119
|
+
error: "Add an item to your cart before applying a coupon.",
|
|
120
|
+
errorKey: "ecommerce.coupon_cart_empty"
|
|
121
|
+
};
|
|
122
|
+
const { data: c, error: s } = await e.client.from("coupons").select(
|
|
123
|
+
"id, code, name, provider_scope, discount_type, discount_amount, is_active, starts_at, ends_at, redemption_limit, redemptions_count"
|
|
124
|
+
).ilike("code", t).maybeSingle();
|
|
125
|
+
if (s)
|
|
126
|
+
throw new Error(s.message);
|
|
127
|
+
if (!c)
|
|
128
|
+
return {
|
|
129
|
+
success: !1,
|
|
130
|
+
error: "Coupon code not found.",
|
|
131
|
+
errorKey: "ecommerce.coupon_not_found"
|
|
132
|
+
};
|
|
133
|
+
const r = c, u = /* @__PURE__ */ new Date(), i = M(r.starts_at), p = M(r.ends_at);
|
|
134
|
+
if (!r.is_active)
|
|
135
|
+
return {
|
|
136
|
+
success: !1,
|
|
137
|
+
error: "This coupon is not active.",
|
|
138
|
+
errorKey: "ecommerce.coupon_inactive"
|
|
139
|
+
};
|
|
140
|
+
if (i && i > u)
|
|
141
|
+
return {
|
|
142
|
+
success: !1,
|
|
143
|
+
error: "This coupon is not active yet.",
|
|
144
|
+
errorKey: "ecommerce.coupon_not_started"
|
|
145
|
+
};
|
|
146
|
+
if (p && p <= u)
|
|
147
|
+
return {
|
|
148
|
+
success: !1,
|
|
149
|
+
error: "This coupon has expired.",
|
|
150
|
+
errorKey: "ecommerce.coupon_expired"
|
|
151
|
+
};
|
|
152
|
+
if (r.redemption_limit !== null && (r.redemptions_count ?? 0) >= r.redemption_limit)
|
|
153
|
+
return {
|
|
154
|
+
success: !1,
|
|
155
|
+
error: "This coupon has reached its redemption limit.",
|
|
156
|
+
errorKey: "ecommerce.coupon_limit_reached"
|
|
157
|
+
};
|
|
158
|
+
const [{ lines: f }, a] = await Promise.all([
|
|
159
|
+
L({
|
|
160
|
+
client: e.client,
|
|
161
|
+
items: e.items,
|
|
162
|
+
currencyCode: e.currencyCode
|
|
163
|
+
}),
|
|
164
|
+
R(e.client, r.id)
|
|
165
|
+
]), g = a.size > 0, _ = f.filter(
|
|
166
|
+
(o) => o.subtotal > 0 && k(r.provider_scope, o.provider) && (!g || a.has(o.product_id))
|
|
167
|
+
);
|
|
168
|
+
if (_.length === 0)
|
|
169
|
+
return {
|
|
170
|
+
success: !1,
|
|
171
|
+
error: "This coupon does not apply to the items in your cart.",
|
|
172
|
+
errorKey: "ecommerce.coupon_not_applicable"
|
|
173
|
+
};
|
|
174
|
+
const v = r.discount_type === "fixed" ? z(_, r.discount_amount) : null, m = E(), h = _.map((o) => {
|
|
175
|
+
const l = r.discount_type === "percent" ? Math.min(o.subtotal, Math.round(o.subtotal * r.discount_amount / 100)) : v?.get(o.key) ?? 0;
|
|
176
|
+
return m[o.provider] += l, {
|
|
177
|
+
key: o.key,
|
|
178
|
+
product_id: o.product_id,
|
|
179
|
+
variant_id: o.variant_id,
|
|
180
|
+
provider: o.provider,
|
|
181
|
+
title: o.title,
|
|
182
|
+
quantity: o.quantity,
|
|
183
|
+
subtotal: o.subtotal,
|
|
184
|
+
discount: l
|
|
185
|
+
};
|
|
186
|
+
});
|
|
187
|
+
return {
|
|
188
|
+
success: !0,
|
|
189
|
+
quote: {
|
|
190
|
+
couponId: r.id,
|
|
191
|
+
code: r.code,
|
|
192
|
+
name: r.name,
|
|
193
|
+
discountType: r.discount_type,
|
|
194
|
+
discountAmount: r.discount_amount,
|
|
195
|
+
providerScope: r.provider_scope,
|
|
196
|
+
eligibleSubtotal: _.reduce((o, l) => o + l.subtotal, 0),
|
|
197
|
+
discountTotal: m.stripe + m.freemius,
|
|
198
|
+
providerDiscounts: m,
|
|
199
|
+
lineDiscounts: h
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
function Q(e) {
|
|
204
|
+
const t = /* @__PURE__ */ new Map();
|
|
205
|
+
for (const c of e?.lineDiscounts ?? [])
|
|
206
|
+
t.set(c.key, c.discount);
|
|
207
|
+
return t;
|
|
208
|
+
}
|
|
209
|
+
async function O(e) {
|
|
210
|
+
const { error: t } = await e.client.from("coupon_redemptions").insert({
|
|
211
|
+
coupon_id: e.quote.couponId,
|
|
212
|
+
order_id: e.orderId,
|
|
213
|
+
coupon_code: e.quote.code,
|
|
214
|
+
provider: e.provider,
|
|
215
|
+
discount_total: Math.max(0, e.discountTotal),
|
|
216
|
+
user_id: e.userId || null,
|
|
217
|
+
customer_email: e.customerEmail || null,
|
|
218
|
+
metadata: e.metadata || {}
|
|
219
|
+
});
|
|
220
|
+
if (t) {
|
|
221
|
+
console.error("Failed to record coupon redemption:", t);
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
const { data: c } = await e.client.from("coupons").select("redemptions_count").eq("id", e.quote.couponId).single();
|
|
225
|
+
await e.client.from("coupons").update({
|
|
226
|
+
redemptions_count: (c?.redemptions_count ?? 0) + 1,
|
|
227
|
+
updated_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
228
|
+
}).eq("id", e.quote.couponId);
|
|
229
|
+
}
|
|
230
|
+
export {
|
|
231
|
+
B as getCouponQuote,
|
|
232
|
+
Q as getQuoteLineDiscountMap,
|
|
233
|
+
O as recordCouponRedemption
|
|
234
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function o(e){return(e||"").trim().replace(/\s+/g,"").toUpperCase()}function r(e){return`${e.product_id}:${e.variant_id||"base"}`}function t(){return{stripe:0,freemius:0}}exports.emptyProviderDiscounts=t;exports.getCartLineCouponKey=r;exports.normalizeCouponCode=o;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
function r(e) {
|
|
2
|
+
return (e || "").trim().replace(/\s+/g, "").toUpperCase();
|
|
3
|
+
}
|
|
4
|
+
function n(e) {
|
|
5
|
+
return `${e.product_id}:${e.variant_id || "base"}`;
|
|
6
|
+
}
|
|
7
|
+
function t() {
|
|
8
|
+
return {
|
|
9
|
+
stripe: 0,
|
|
10
|
+
freemius: 0
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
export {
|
|
14
|
+
t as emptyProviderDiscounts,
|
|
15
|
+
n as getCartLineCouponKey,
|
|
16
|
+
r as normalizeCouponCode
|
|
17
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const C="NEXTBLOCK_CURRENCY";exports.CURRENCY_COOKIE_NAME=C;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const a=require("zustand"),t=require("zustand/middleware"),c=require("@nextblock-cms/utils"),o=a.create()(t.persist(e=>({activeCurrencyCode:null,hasHydrated:!1,setActiveCurrencyCode:r=>e({activeCurrencyCode:c.normalizeCurrencyCode(r)}),setHasHydrated:r=>e({hasHydrated:r})}),{name:"currency-preference-storage",storage:t.createJSONStorage(()=>localStorage),skipHydration:!0,partialize:e=>({activeCurrencyCode:e.activeCurrencyCode}),onRehydrateStorage:()=>e=>{e?.setHasHydrated(!0)}}));exports.useCurrencyPreferenceStore=o;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { create as t } from "zustand";
|
|
2
|
+
import { persist as a, createJSONStorage as o } from "zustand/middleware";
|
|
3
|
+
import { normalizeCurrencyCode as c } from "@nextblock-cms/utils";
|
|
4
|
+
const s = t()(
|
|
5
|
+
a(
|
|
6
|
+
(e) => ({
|
|
7
|
+
activeCurrencyCode: null,
|
|
8
|
+
hasHydrated: !1,
|
|
9
|
+
setActiveCurrencyCode: (r) => e({ activeCurrencyCode: c(r) }),
|
|
10
|
+
setHasHydrated: (r) => e({ hasHydrated: r })
|
|
11
|
+
}),
|
|
12
|
+
{
|
|
13
|
+
name: "currency-preference-storage",
|
|
14
|
+
storage: o(() => localStorage),
|
|
15
|
+
skipHydration: !0,
|
|
16
|
+
partialize: (e) => ({
|
|
17
|
+
activeCurrencyCode: e.activeCurrencyCode
|
|
18
|
+
}),
|
|
19
|
+
onRehydrateStorage: () => (e) => {
|
|
20
|
+
e?.setHasHydrated(!0);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
)
|
|
24
|
+
);
|
|
25
|
+
export {
|
|
26
|
+
s as useCurrencyPreferenceStore
|
|
27
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});require("server-only");const R=require("@nextblock-cms/db/server"),a=require("@nextblock-cms/utils"),S=require("./currency.cjs.js"),v="https://api.frankfurter.dev",z="id, code, symbol, exchange_rate, is_default, is_active, auto_update_exchange_rate, auto_sync_product_prices, exchange_rate_source, exchange_rate_updated_at, updated_at, rounding_mode, rounding_increment, rounding_charm_amount";function w(r){if(!r.id)throw new Error(`Currency ${a.normalizeCurrencyCode(r.code)} is missing its ID.`);return r.id}function F(){return process.env.FX_API_BASE_URL?.trim().replace(/\/+$/,"")||v}function A(r){try{const e=new URL(r),n=e.pathname.replace(/\/+$/,"");return`${e.host}${n&&n!=="/"?n:""}`}catch{return r}}function $(r){if(!r||typeof r!="object")return!1;const e=r;return typeof e.base=="string"&&typeof e.date=="string"&&typeof e.quote=="string"&&typeof e.rate=="number"&&Number.isFinite(e.rate)&&e.rate>0}function q(r){return Array.isArray(r)&&r.every($)}async function E(){const r=R.getServiceRoleSupabaseClient(),{data:e,error:n}=await r.from("currencies").select(z).order("code",{ascending:!0});if(n)throw new Error(n.message);return{supabase:r,currencies:e||[]}}async function D(r){const e=a.normalizeCurrencyCode(r.newDefaultCurrencyCode),n=Number(r.previousBaseRate);if(!Number.isFinite(n)||n<=0)throw new Error("Cannot rebase exchange rates without a valid previous base rate.");const{supabase:f,currencies:c}=await E();if(!c.find(o=>a.normalizeCurrencyCode(o.code)===e))throw new Error(`Currency ${e} was not found for rebasing.`);const i=new Date().toISOString(),d=[],p=c.map(o=>{const C=w(o),u=a.normalizeCurrencyCode(o.code),y=u===e?1:Number((o.exchange_rate/n).toFixed(10));return d.push(u),{...o,id:C,exchange_rate:y,auto_update_exchange_rate:u===e?!1:o.auto_update_exchange_rate,exchange_rate_source:u===e?"store-default":o.exchange_rate_source||"rebased-default",exchange_rate_updated_at:i,updated_at:i}}),{error:s}=await f.from("currencies").upsert(p,{onConflict:"id"});if(s)throw new Error(s.message);return{rebasedAt:i,newDefaultCurrencyCode:e,updatedCurrencies:d}}async function N(){const{supabase:r,currencies:e}=await E();if(!e.length)throw new Error("No currencies are configured yet.");const n=e.map(t=>S.normalizeCurrencyRecord(t)),f=S.getDefaultCurrency(n),c=a.normalizeCurrencyCode(f.code),_=e.find(t=>a.normalizeCurrencyCode(t.code)===c);if(!_)throw new Error(`Default currency ${c} could not be found for syncing.`);const i=e.filter(t=>a.normalizeCurrencyCode(t.code)!==c&&t.auto_update_exchange_rate!==!1),d=F(),p=A(d),s=new Date().toISOString(),{error:o}=await r.from("currencies").upsert({..._,id:w(_),exchange_rate:1,auto_update_exchange_rate:!1,exchange_rate_source:"store-default",exchange_rate_updated_at:s,updated_at:s},{onConflict:"id"});if(o)throw new Error(o.message);if(!i.length)return{baseCurrencyCode:c,fetchedAt:s,provider:p,providerUrl:d,skippedCurrencies:[],updatedCurrencies:[]};const C=new URL(`${d}/v2/rates`);C.searchParams.set("base",c),C.searchParams.set("quotes",[...new Set(i.map(t=>a.normalizeCurrencyCode(t.code)))].join(","));const u=await fetch(C.toString(),{cache:"no-store",headers:{Accept:"application/json"}});if(!u.ok){const t=await u.text();throw new Error(`FX provider request failed (${u.status}): ${t||"Unexpected response body."}`)}const y=await u.json();if(!q(y))throw new Error("FX provider returned an unexpected response payload.");const m=new Map;for(const t of y)m.set(a.normalizeCurrencyCode(t.quote),t.rate);const x=[],b=[],g=[];for(const t of i){const h=a.normalizeCurrencyCode(t.code),l=m.get(h);if(!l||!Number.isFinite(l)||l<=0){b.push(h);continue}g.push({...t,id:w(t),exchange_rate:Number(l.toFixed(10)),exchange_rate_source:p,exchange_rate_updated_at:s,updated_at:s}),x.push(h)}if(g.length){const{error:t}=await r.from("currencies").upsert(g,{onConflict:"id"});if(t)throw new Error(t.message)}return{baseCurrencyCode:c,fetchedAt:s,provider:p,providerUrl:d,skippedCurrencies:b,updatedCurrencies:x}}async function P(r){const e=a.normalizeCurrencyCode(r),n=R.getServiceRoleSupabaseClient(),{error:f}=await n.rpc("clear_currency_price_overrides",{target_currency:e});if(f)throw new Error(f.message);return{currencyCode:e}}exports.clearAutoSyncedCurrencyPriceOverrides=P;exports.rebaseStoreCurrencyExchangeRates=D;exports.syncStoreCurrencyRates=N;
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import "server-only";
|
|
2
|
+
import { getServiceRoleSupabaseClient as E } from "@nextblock-cms/db/server";
|
|
3
|
+
import { normalizeCurrencyCode as a } from "@nextblock-cms/utils";
|
|
4
|
+
import { normalizeCurrencyRecord as S, getDefaultCurrency as v } from "./currency.es.js";
|
|
5
|
+
const F = "https://api.frankfurter.dev", A = "id, code, symbol, exchange_rate, is_default, is_active, auto_update_exchange_rate, auto_sync_product_prices, exchange_rate_source, exchange_rate_updated_at, updated_at, rounding_mode, rounding_increment, rounding_charm_amount";
|
|
6
|
+
function C(r) {
|
|
7
|
+
if (!r.id)
|
|
8
|
+
throw new Error(`Currency ${a(r.code)} is missing its ID.`);
|
|
9
|
+
return r.id;
|
|
10
|
+
}
|
|
11
|
+
function $() {
|
|
12
|
+
return process.env.FX_API_BASE_URL?.trim().replace(/\/+$/, "") || F;
|
|
13
|
+
}
|
|
14
|
+
function D(r) {
|
|
15
|
+
try {
|
|
16
|
+
const e = new URL(r), n = e.pathname.replace(/\/+$/, "");
|
|
17
|
+
return `${e.host}${n && n !== "/" ? n : ""}`;
|
|
18
|
+
} catch {
|
|
19
|
+
return r;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
function N(r) {
|
|
23
|
+
if (!r || typeof r != "object")
|
|
24
|
+
return !1;
|
|
25
|
+
const e = r;
|
|
26
|
+
return typeof e.base == "string" && typeof e.date == "string" && typeof e.quote == "string" && typeof e.rate == "number" && Number.isFinite(e.rate) && e.rate > 0;
|
|
27
|
+
}
|
|
28
|
+
function U(r) {
|
|
29
|
+
return Array.isArray(r) && r.every(N);
|
|
30
|
+
}
|
|
31
|
+
async function R() {
|
|
32
|
+
const r = E(), { data: e, error: n } = await r.from("currencies").select(A).order("code", { ascending: !0 });
|
|
33
|
+
if (n)
|
|
34
|
+
throw new Error(n.message);
|
|
35
|
+
return {
|
|
36
|
+
supabase: r,
|
|
37
|
+
currencies: e || []
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
async function L(r) {
|
|
41
|
+
const e = a(r.newDefaultCurrencyCode), n = Number(r.previousBaseRate);
|
|
42
|
+
if (!Number.isFinite(n) || n <= 0)
|
|
43
|
+
throw new Error("Cannot rebase exchange rates without a valid previous base rate.");
|
|
44
|
+
const { supabase: f, currencies: c } = await R();
|
|
45
|
+
if (!c.find(
|
|
46
|
+
(o) => a(o.code) === e
|
|
47
|
+
))
|
|
48
|
+
throw new Error(`Currency ${e} was not found for rebasing.`);
|
|
49
|
+
const i = (/* @__PURE__ */ new Date()).toISOString(), d = [], p = c.map((o) => {
|
|
50
|
+
const _ = C(o), u = a(o.code), g = u === e ? 1 : Number((o.exchange_rate / n).toFixed(10));
|
|
51
|
+
return d.push(u), {
|
|
52
|
+
...o,
|
|
53
|
+
id: _,
|
|
54
|
+
exchange_rate: g,
|
|
55
|
+
auto_update_exchange_rate: u === e ? !1 : o.auto_update_exchange_rate,
|
|
56
|
+
exchange_rate_source: u === e ? "store-default" : o.exchange_rate_source || "rebased-default",
|
|
57
|
+
exchange_rate_updated_at: i,
|
|
58
|
+
updated_at: i
|
|
59
|
+
};
|
|
60
|
+
}), { error: s } = await f.from("currencies").upsert(p, {
|
|
61
|
+
onConflict: "id"
|
|
62
|
+
});
|
|
63
|
+
if (s)
|
|
64
|
+
throw new Error(s.message);
|
|
65
|
+
return {
|
|
66
|
+
rebasedAt: i,
|
|
67
|
+
newDefaultCurrencyCode: e,
|
|
68
|
+
updatedCurrencies: d
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
async function q() {
|
|
72
|
+
const { supabase: r, currencies: e } = await R();
|
|
73
|
+
if (!e.length)
|
|
74
|
+
throw new Error("No currencies are configured yet.");
|
|
75
|
+
const n = e.map(
|
|
76
|
+
(t) => S(t)
|
|
77
|
+
), f = v(n), c = a(f.code), h = e.find(
|
|
78
|
+
(t) => a(t.code) === c
|
|
79
|
+
);
|
|
80
|
+
if (!h)
|
|
81
|
+
throw new Error(`Default currency ${c} could not be found for syncing.`);
|
|
82
|
+
const i = e.filter(
|
|
83
|
+
(t) => a(t.code) !== c && t.auto_update_exchange_rate !== !1
|
|
84
|
+
), d = $(), p = D(d), s = (/* @__PURE__ */ new Date()).toISOString(), { error: o } = await r.from("currencies").upsert(
|
|
85
|
+
{
|
|
86
|
+
...h,
|
|
87
|
+
id: C(h),
|
|
88
|
+
exchange_rate: 1,
|
|
89
|
+
auto_update_exchange_rate: !1,
|
|
90
|
+
exchange_rate_source: "store-default",
|
|
91
|
+
exchange_rate_updated_at: s,
|
|
92
|
+
updated_at: s
|
|
93
|
+
},
|
|
94
|
+
{
|
|
95
|
+
onConflict: "id"
|
|
96
|
+
}
|
|
97
|
+
);
|
|
98
|
+
if (o)
|
|
99
|
+
throw new Error(o.message);
|
|
100
|
+
if (!i.length)
|
|
101
|
+
return {
|
|
102
|
+
baseCurrencyCode: c,
|
|
103
|
+
fetchedAt: s,
|
|
104
|
+
provider: p,
|
|
105
|
+
providerUrl: d,
|
|
106
|
+
skippedCurrencies: [],
|
|
107
|
+
updatedCurrencies: []
|
|
108
|
+
};
|
|
109
|
+
const _ = new URL(`${d}/v2/rates`);
|
|
110
|
+
_.searchParams.set("base", c), _.searchParams.set(
|
|
111
|
+
"quotes",
|
|
112
|
+
[...new Set(i.map((t) => a(t.code)))].join(
|
|
113
|
+
","
|
|
114
|
+
)
|
|
115
|
+
);
|
|
116
|
+
const u = await fetch(_.toString(), {
|
|
117
|
+
cache: "no-store",
|
|
118
|
+
headers: {
|
|
119
|
+
Accept: "application/json"
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
if (!u.ok) {
|
|
123
|
+
const t = await u.text();
|
|
124
|
+
throw new Error(
|
|
125
|
+
`FX provider request failed (${u.status}): ${t || "Unexpected response body."}`
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
const g = await u.json();
|
|
129
|
+
if (!U(g))
|
|
130
|
+
throw new Error("FX provider returned an unexpected response payload.");
|
|
131
|
+
const m = /* @__PURE__ */ new Map();
|
|
132
|
+
for (const t of g)
|
|
133
|
+
m.set(a(t.quote), t.rate);
|
|
134
|
+
const x = [], b = [], y = [];
|
|
135
|
+
for (const t of i) {
|
|
136
|
+
const l = a(t.code), w = m.get(l);
|
|
137
|
+
if (!w || !Number.isFinite(w) || w <= 0) {
|
|
138
|
+
b.push(l);
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
y.push({
|
|
142
|
+
...t,
|
|
143
|
+
id: C(t),
|
|
144
|
+
exchange_rate: Number(w.toFixed(10)),
|
|
145
|
+
exchange_rate_source: p,
|
|
146
|
+
exchange_rate_updated_at: s,
|
|
147
|
+
updated_at: s
|
|
148
|
+
}), x.push(l);
|
|
149
|
+
}
|
|
150
|
+
if (y.length) {
|
|
151
|
+
const { error: t } = await r.from("currencies").upsert(y, {
|
|
152
|
+
onConflict: "id"
|
|
153
|
+
});
|
|
154
|
+
if (t)
|
|
155
|
+
throw new Error(t.message);
|
|
156
|
+
}
|
|
157
|
+
return {
|
|
158
|
+
baseCurrencyCode: c,
|
|
159
|
+
fetchedAt: s,
|
|
160
|
+
provider: p,
|
|
161
|
+
providerUrl: d,
|
|
162
|
+
skippedCurrencies: b,
|
|
163
|
+
updatedCurrencies: x
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
async function z(r) {
|
|
167
|
+
const e = a(r), n = E(), { error: f } = await n.rpc("clear_currency_price_overrides", {
|
|
168
|
+
target_currency: e
|
|
169
|
+
});
|
|
170
|
+
if (f)
|
|
171
|
+
throw new Error(f.message);
|
|
172
|
+
return {
|
|
173
|
+
currencyCode: e
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
export {
|
|
177
|
+
z as clearAutoSyncedCurrencyPriceOverrides,
|
|
178
|
+
L as rebaseStoreCurrencyExchangeRates,
|
|
179
|
+
q as syncStoreCurrencyRates
|
|
180
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const u=require("@nextblock-cms/utils"),A=["none","nearest","up","down","charm"],S={AU:"AUD",CA:"CAD",CH:"CHF",CZ:"CZK",DE:"EUR",DK:"DKK",ES:"EUR",EU:"EUR",FI:"EUR",FR:"EUR",GB:"GBP",IE:"EUR",IN:"INR",IT:"EUR",JP:"JPY",MX:"MXN",NL:"EUR",NO:"NOK",NZ:"NZD",PL:"PLN",PT:"EUR",SE:"SEK",SG:"SGD",US:"USD"};function g(r){return typeof r=="number"&&Number.isFinite(r)}function P(r,e=1){const n=typeof r=="string"?Number.parseInt(r,10):r;return typeof n=="number"&&Number.isFinite(n)&&n>0?Math.max(1,Math.round(n)):e}function U(r){const e=typeof r=="string"?Number.parseInt(r,10):r;return typeof e=="number"&&Number.isFinite(e)&&e>=0?Math.round(e):null}function b(r){if(typeof r=="string"){const e=r.trim().toLowerCase();if(A.includes(e))return e}return"none"}function s(r){const e=r||{},n=u.normalizeCurrencyCode(typeof e.code=="string"?e.code:null),o=e.is_default===!0;return{code:n,symbol:typeof e.symbol=="string"&&e.symbol.trim()?e.symbol.trim():n,exchange_rate:o?1:g(e.exchange_rate)&&e.exchange_rate>0?e.exchange_rate:1,is_default:o,is_active:e.is_active!==!1,auto_sync_product_prices:o?!1:e.auto_sync_product_prices===!0,rounding_mode:b(e.rounding_mode),rounding_increment:P(e.rounding_increment,1),rounding_charm_amount:U(e.rounding_charm_amount),auto_update_exchange_rate:o?!1:e.auto_update_exchange_rate!==!1,exchange_rate_source:typeof e.exchange_rate_source=="string"&&e.exchange_rate_source.trim()?e.exchange_rate_source.trim():null,exchange_rate_updated_at:typeof e.exchange_rate_updated_at=="string"&&e.exchange_rate_updated_at.trim()?e.exchange_rate_updated_at:null}}function C(r){return!r||typeof r!="object"||Array.isArray(r)?{}:Object.entries(r).reduce((e,[n,o])=>{const c=u.normalizeCurrencyCode(n),t=typeof o=="string"?Number.parseFloat(o):o;return g(t)&&t>=0&&(e[c]=Math.round(t)),e},{})}function M(r){return!r||typeof r!="object"||Array.isArray(r)?{}:Object.entries(r).reduce((e,[n,o])=>{const c=u.normalizeCurrencyCode(n),t=typeof o=="string"?Number.parseFloat(o):o;return t===null?(e[c]=null,e):(g(t)&&t>=0&&(e[c]=Math.round(t)),e)},{})}function f(r){return r.map(s).find(e=>e.is_default)??s(r[0])??s({code:"USD",symbol:"$",exchange_rate:1,is_default:!0,is_active:!0})}function _(r){return r.reduce((e,n)=>{const o=s(n);return e[o.code]=o,e},{})}function D(r,e){const n=u.normalizeCurrencyCode(e),o=f(r);return _(r)[n]?.exchange_rate??(n===o.code,1)}function y(r){const{amount:e,currencies:n}=r,o=u.normalizeCurrencyCode(r.fromCurrencyCode),c=u.normalizeCurrencyCode(r.toCurrencyCode);if(!Number.isFinite(e))return 0;if(o===c)return Math.round(e);const t=_(n),i=f(n),a=t[o]?.exchange_rate??(o===i.code?1:i.exchange_rate),d=t[c]?.exchange_rate??(c===i.code?1:i.exchange_rate),x=e/u.getCurrencyMinorUnitFactor(o)/a*d,h=u.majorUnitAmountToMinor(x,c);return r.applyRounding?R({amount:h,currencyCode:c,currencies:n}):h}function R(r){const e=Math.max(0,Math.round(r.amount)),n=u.normalizeCurrencyCode(r.currencyCode),c=_(r.currencies)[n]??s(f(r.currencies)),t=P(c.rounding_increment,1);if(c.rounding_mode==="nearest")return Math.round(e/t)*t;if(c.rounding_mode==="up")return Math.ceil(e/t)*t;if(c.rounding_mode==="down")return Math.floor(e/t)*t;if(c.rounding_mode==="charm"){const i=u.getCurrencyMinorUnitFactor(c.code),a=Math.min(Math.max(c.rounding_charm_amount??0,0),Math.max(i-1,0));let l=Math.floor(e/i)*i+a;return l<e&&(l+=i),l}return e}function k(r){const e=s(r);if(e.rounding_mode==="nearest")return`Nearest ${u.formatPrice(e.rounding_increment??1,e.code)}`;if(e.rounding_mode==="up")return`Round up to ${u.formatPrice(e.rounding_increment??1,e.code)}`;if(e.rounding_mode==="down")return`Round down to ${u.formatPrice(e.rounding_increment??1,e.code)}`;if(e.rounding_mode==="charm"){const n=e.rounding_charm_amount??0;return`Charm ending ${u.formatPrice(n,e.code)}`}return`Exact conversion (${u.minorUnitAmountToMajor(1,e.code).toFixed(u.getCurrencyMinorUnitFactor(e.code)===1?0:2)} step)`}function w(r,e){const n=f(e);return r[n.code]!==void 0?n.code:Object.keys(r)[0]??n.code}function p(r){const{amountMap:e,fallbackAmount:n,currencies:o}=r,c=u.normalizeCurrencyCode(r.currencyCode),t=C(e);if(t[c]!==void 0)return t[c];if(Object.keys(t).length>0){const i=w(t,o),a=t[i];if(a!==void 0)return y({amount:a,fromCurrencyCode:i,toCurrencyCode:c,currencies:o,applyRounding:!0})}if(typeof n=="number"&&Number.isFinite(n)){const i=f(o);return y({amount:n,fromCurrencyCode:i.code,toCurrencyCode:c,currencies:o,applyRounding:!0})}return 0}function z(r){const{prices:e={},salePrices:n={},fallbackPrice:o,fallbackSalePrice:c,currencies:t}=r,i=u.normalizeCurrencyCode(r.currencyCode),a=C(e),d=M(n),l=p({amountMap:a,fallbackAmount:o,currencyCode:i,currencies:t}),m=p({amountMap:C(d),fallbackAmount:c,currencyCode:i,currencies:t});return{currencyCode:i,price:l,sale_price:m>0&&m<=l?m:null}}function F(r){const e=(r.now??new Date).getTime();if(r.saleStartAt){const n=new Date(r.saleStartAt).getTime();if(Number.isFinite(n)&&e<n)return!1}if(r.saleEndAt){const n=new Date(r.saleEndAt).getTime();if(Number.isFinite(n)&&e>=n)return!1}return!0}function N(r){if(!r.scheduledPriceAt)return!1;const e=new Date(r.scheduledPriceAt).getTime();return Number.isFinite(e)?(r.now??new Date).getTime()>=e:!1}function E(r){const e=r.now??new Date,n=N({scheduledPriceAt:r.scheduledPriceAt,now:e}),o=n?r.scheduledPrices:r.prices,c=n?r.scheduledPrice:r.fallbackPrice,t=F({saleStartAt:r.saleStartAt,saleEndAt:r.saleEndAt,now:e});return z({prices:o,salePrices:t?r.salePrices:null,fallbackPrice:c,fallbackSalePrice:t?r.fallbackSalePrice:null,currencyCode:r.currencyCode,currencies:r.currencies})}function v(r){const e=r.entries.map(n=>E({prices:n.prices,salePrices:n.sale_prices,fallbackPrice:n.price,fallbackSalePrice:n.sale_price,saleStartAt:n.sale_start_at,saleEndAt:n.sale_end_at,scheduledPrice:n.scheduled_price,scheduledPrices:n.scheduled_prices,scheduledPriceAt:n.scheduled_price_at,currencyCode:r.currencyCode,currencies:r.currencies,now:r.now})).map(n=>n.sale_price??n.price).filter(n=>Number.isFinite(n));return e.length?{min:Math.min(...e),max:Math.max(...e)}:null}function j(r,e){const n=f(e),o=r?.trim();if(!o)return n.code;const t=o.replace("_","-").split("-").map(d=>d.toUpperCase())[1];if(!t)return n.code;const i=S[t];return i&&new Set(e.map(d=>u.normalizeCurrencyCode(d.code))).has(i)?i:n.code}function O(r){return r.map(s).sort((e,n)=>e.is_default!==n.is_default?e.is_default?-1:1:u.normalizeCurrencyCode(e.code).localeCompare(u.normalizeCurrencyCode(n.code)))}exports.CURRENCY_ROUNDING_MODES=A;exports.applyCurrencyRounding=R;exports.convertMinorUnitAmount=y;exports.describeCurrencyRoundingRule=k;exports.getCurrencyLookup=_;exports.getDefaultCurrency=f;exports.getExchangeRateForCurrency=D;exports.inferCurrencyCodeFromLocale=j;exports.isSaleWindowActive=F;exports.isScheduledPriceDue=N;exports.normalizeCurrencyRecord=s;exports.normalizeCurrencyRoundingMode=b;exports.normalizePriceMap=C;exports.normalizeSalePriceMap=M;exports.resolveEffectivePriceForCurrency=E;exports.resolvePriceForCurrency=z;exports.resolvePriceRangeForCurrency=v;exports.sortCurrencies=O;
|