// Metro module 6547 (f6548). import _objectWithoutProperties from "../../_bundle/_objectWithoutProperties"; import _slicedToArray from "../../_bundle/_slicedToArray"; import { useState, useEffect, useRef, useCallback } from "react"; import InvoiceRecord from "../../records/InvoiceRecord"; import { Endpoints } from "../../Constants"; import { coerceExistingItemsToNewItemInterval } from "../../utils/PremiumUtils"; import { HTTP } from "../../_bundle/sendRequest"; import Dispatcher from "../../Dispatcher"; import { BillingError } from "../../errors/index"; import Module38 from "../../_bundle/Module38"; import { httpGetWithCountryCodeQuery } from "../../utils/StoreUtils"; import { HermesBuiltin, $createGenerator } from "../../_hermes/prelude"; function $mk78770($env) { return () => { let env25918 = { $parent: $env }; env25918[1] = function _loadPreview() { const r9 = (0, env25918.$parent.$parent[6])(function() { let c31427_0, c31427_1, c31427_2; let c31427_4 = 0; let c31427_5 = 0; let c31427_3 = 0; return $createGenerator(function() { let value2; const r0 = c31427_5; if (r0 === 2) { c31427_5 = 3; HermesBuiltin.throwTypeError("Generator functions may not be called on executing generators"); throw new Error("unreachable"); } const value = arguments[1]; const r3 = arguments[0]; const r11 = env25918; const r9 = env25918.$parent; if (r0 === 3) { if (r3 === 1) { throw value; } if (r3 === 2) { return { value, done: true }; } return { value: void 0, done: true }; } while (true) { L21: { L19: { L15: { L10: { try { c31427_5 = 2; const r19 = c31427_4; if (r19 === 0) { if (r3 === 1) { c31427_5 = 3; throw value; } if (r3 === 2) { c31427_5 = 3; break L21; } let env31967; c31427_1 = r11; const r39 = env31967 = {}; c31427_0 = r39; env31967[0] = void 0; c31427_3 = 1; (0, r9[3])(null); value2 = (0, r9[1])(); c31427_4 = 2; c31427_5 = 1; break L19; } if (r19 === 1) { const r29 = c31427_1; const r30 = c31427_2; c31427_3 = 0; env31967[1] = r30; if (!env25918[0]) { const r32 = r29.$parent; (0, r32[3])(r30); (0, r32[2])(null); } } else { if (r3 === 1) { c31427_5 = 3; throw value; } if (r3 === 2) { c31427_3 = 0; c31427_5 = 3; break L10; } const r14 = c31427_1; env31967[0] = value; if (!env25918[0]) { (0, r14.$parent[2])(env31967[0]); } c31427_3 = 0; } c31427_5 = 3; break L15; } catch (r49) { c31427_2 = r49; if (c31427_3 === 0) { c31427_5 = 3; throw r49; } c31427_4 = 1; continue; } } return { value, done: true }; } return { value: void 0, done: true }; } return { value: value2, done: false }; } return { value, done: true }; } }); }); env25918[1] = r9; return r9.apply(this, arguments); }; env25918[0] = false; if (!$env[0]) { (function loadPreview() { return env25918[1].apply(this, arguments); })(); } return () => { env25918[0] = true; }; }; } let updateSubscriptionInvoicePreview, createOneTimePurchaseInvoicePreview, getSubscriptionInvoice, _excluded; let env4209 = {}; export async function createSubscriptionInvoicePreview(c25914_0) { let r122; let c25914_1, c25914_2, c25914_3; let c25914_4 = 0; const r11 = env4209; try { let env29795; c25914_2 = r11; const r31 = env29795 = {}; c25914_1 = r31; env29795[0] = void 0; env29795[1] = void 0; env29795[2] = void 0; env29795[3] = void 0; env29795[4] = void 0; env29795[5] = void 0; env29795[6] = void 0; env29795[7] = void 0; env29795[8] = void 0; env29795[0] = c25914_0.items; env29795[1] = c25914_0.paymentSourceId; env29795[2] = c25914_0.trialId; env29795[3] = c25914_0.code; const applyEntitlements = c25914_0.applyEntitlements; if (applyEntitlements !== void 0) { env29795[4] = applyEntitlements; env29795[5] = c25914_0.currency; env29795[6] = c25914_0.renewal; env29795[7] = c25914_0.metadata; env29795[8] = c25914_0.loadId; env29795[9] = void 0; env29795[10] = void 0; env29795[11] = void 0; } else { env29795[4] = false; env29795[5] = c25914_0.currency; env29795[6] = c25914_0.renewal; env29795[7] = c25914_0.metadata; env29795[8] = c25914_0.loadId; env29795[9] = void 0; env29795[10] = void 0; env29795[11] = void 0; } await void 0; const r68 = coerceExistingItemsToNewItemInterval(env29795[0]); env29795[0] = r68; env29795[9] = { items: r68.map(({ planId, ...rest }) => ({ ...rest, plan_id: planId })), payment_source_id: env29795[1], trial_id: env29795[2], code: env29795[3], apply_entitlements: env29795[4], currency: env29795[5], renewal: env29795[6], metadata: env29795[7], load_id: env29795[8] }; c25914_4 = 1; env29795[10] = await HTTP.post({ url: Endpoints.BILLING_SUBSCRIPTIONS_PREVIEW, body: env29795[9], oldFormErrors: true, rejectWithError: false }); env29795[11] = InvoiceRecord.createInvoiceFromServer(env29795[10].body); if (env29795[11].checkoutContext?.payment_sources != null) { Dispatcher.dispatch({ type: "SUBSCRIPTION_PREVIEW_CHECKOUT_CONTEXT_UPDATE", checkoutContext: env29795[11].checkoutContext, paymentSourceId: env29795[1] }); } r122 = env29795[11]; c25914_4 = 0; } catch ($e) { const r29 = $e; let r8 = r29; c25914_3 = r29; if (c25914_4 !== 0) { while (true) { try { const r143 = c25914_3; c25914_4 = 0; env29795[12] = r143; throw new BillingError(env29795[12]); } catch (r139) { r8 = r139; c25914_3 = r139; if (c25914_4 === 0) { break; } } } } throw r8; } return r122; } updateSubscriptionInvoicePreview = async function updateSubscriptionInvoicePreview(c25915_0) { let r134; let c25915_1, c25915_2, c25915_3; let c25915_4 = 0; const r11 = env4209; try { let env29796; c25915_2 = r11; const r33 = env29796 = {}; c25915_1 = r33; env29796[0] = void 0; env29796[1] = void 0; env29796[2] = void 0; env29796[3] = void 0; env29796[4] = void 0; env29796[5] = void 0; env29796[6] = void 0; env29796[7] = void 0; env29796[8] = void 0; env29796[9] = void 0; env29796[0] = c25915_0.subscriptionId; env29796[1] = c25915_0.items; env29796[2] = c25915_0.paymentSourceId; env29796[3] = c25915_0.renewal; env29796[4] = c25915_0.currency; const applyEntitlements = c25915_0.applyEntitlements; if (applyEntitlements !== void 0) { env29796[5] = applyEntitlements; env29796[6] = c25915_0.analyticsLocations; env29796[7] = c25915_0.analyticsLocation; env29796[8] = c25915_0.userDiscountOfferId; env29796[9] = c25915_0.loadId; env29796[10] = void 0; env29796[11] = void 0; env29796[12] = void 0; } else { env29796[5] = false; env29796[6] = c25915_0.analyticsLocations; env29796[7] = c25915_0.analyticsLocation; env29796[8] = c25915_0.userDiscountOfferId; env29796[9] = c25915_0.loadId; env29796[10] = void 0; env29796[11] = void 0; env29796[12] = void 0; } await void 0; if (env29796[1] != null) { env29796[1] = coerceExistingItemsToNewItemInterval(env29796[1]); } const r18 = env29796[1]; env29796[10] = { items: r18 == null ? void 0 : r18.map(({ planId, ...rest }) => ({ ...rest, plan_id: planId })), payment_source_id: env29796[2], renewal: env29796[3], apply_entitlements: env29796[5], currency: env29796[4], user_discount_offer_id: env29796[8], load_id: env29796[9] }; c25915_4 = 1; env29796[11] = await HTTP.patch({ url: Endpoints.BILLING_SUBSCRIPTION_PREVIEW(env29796[0]), query: { location: env29796[7], location_stack: env29796[6] }, body: env29796[10], oldFormErrors: true, rejectWithError: false }); env29796[12] = InvoiceRecord.createInvoiceFromServer(env29796[11].body); if (env29796[2] != null && env29796[12].checkoutContext?.payment_sources != null) { Dispatcher.dispatch({ type: "SUBSCRIPTION_PREVIEW_CHECKOUT_CONTEXT_UPDATE", checkoutContext: env29796[12].checkoutContext, paymentSourceId: env29796[2] }); } r134 = env29796[12]; c25915_4 = 0; } catch ($e) { const r31 = $e; let r8 = r31; c25915_3 = r31; if (c25915_4 !== 0) { while (true) { try { const r155 = c25915_3; c25915_4 = 0; env29796[13] = r155; throw new BillingError(env29796[13]); } catch (r151) { r8 = r151; c25915_3 = r151; if (c25915_4 === 0) { break; } } } } throw r8; } return r134; }; createOneTimePurchaseInvoicePreview = async function createOneTimePurchaseInvoicePreview(c25916_0) { let invoiceFromServer; let c25916_3; let c25916_4 = 0; let c29797_0, c29797_1, c29797_2, c29797_3, c29797_4, c29797_5, c29797_6, c29797_7, c29797_8; try { c29797_0 = void 0; c29797_1 = void 0; c29797_2 = void 0; c29797_3 = void 0; c29797_4 = void 0; c29797_5 = void 0; c29797_0 = c25916_0.paymentSourceId; c29797_1 = c25916_0.skuId; c29797_2 = c25916_0.subscriptionPlanId; c29797_3 = c25916_0.currency; c29797_4 = c25916_0.loadId; c29797_5 = c25916_0.quantity; c29797_6 = void 0; c29797_7 = void 0; await void 0; Module38(c29797_1 != null && c29797_1 !== "", "SKU ID is missing for one time purchase gift invoice preview"); c29797_6 = { gift: true, payment_source_id: c29797_0, sku_subscription_plan_id: c29797_2, currency: c29797_3, load_id: c29797_4 }; if (c29797_5 != null) { c29797_6.quantity = c29797_5; } c25916_4 = 1; c29797_7 = await httpGetWithCountryCodeQuery({ url: Endpoints.STORE_SKU_PURCHASE(c29797_1), query: c29797_6, oldFormErrors: true, rejectWithError: false }); invoiceFromServer = InvoiceRecord.createInvoiceFromServer(c29797_7.body); c25916_4 = 0; } catch ($e) { const r33 = $e; let r8 = r33; c25916_3 = r33; if (c25916_4 !== 0) { while (true) { try { const r123 = c25916_3; c25916_4 = 0; c29797_8 = r123; throw new BillingError(c29797_8); } catch (r119) { r8 = r119; c25916_3 = r119; if (c25916_4 === 0) { break; } } } } throw r8; } return invoiceFromServer; }; getSubscriptionInvoice = async function getSubscriptionInvoice(c25917_0) { let c29798_0, c29798_1, c29798_2; L14: { c29798_0 = void 0; c29798_1 = void 0; c29798_0 = c25917_0.subscriptionId; c29798_1 = c25917_0.preventFetch; c29798_2 = void 0; await void 0; if (c29798_1) { break L14; } c29798_2 = await HTTP.get({ url: Endpoints.BILLING_SUBSCRIPTION_INVOICE(c29798_0), oldFormErrors: true, rejectWithError: false }); return InvoiceRecord.createInvoiceFromServer(c29798_2.body); } return null; }; _excluded = ["subscriptionId"]; export { updateSubscriptionInvoicePreview }; export { createOneTimePurchaseInvoicePreview }; export { getSubscriptionInvoice }; export function useFetchGenericInvoicePreview({ preventFetch }, a1, a2) { if (preventFetch === void 0) { preventFetch = false; } let env15778; const r11 = env15778 = { $parent: env4209 }; env15778[0] = preventFetch; env15778[1] = a1; env15778[2] = void 0; env15778[3] = void 0; const r10 = _slicedToArray(useState(null), 2); const r12 = r10[0]; env15778[2] = r10[1]; const r15 = _slicedToArray(useState(null), 2); const r16 = r15[0]; env15778[3] = r15[1]; (0, useEffect)($mk78770(r11), [preventFetch, a1, a2]); return [r12, r16]; } export function useFetchSubscriptionGiftInvoicePreview(a0, a1) { let env15780; const current = a0; let c15779_1 = useRef(a0); let c15779_2 = useRef(false); useEffect(() => { c15779_1.current = current; }); const r21 = (0, useCallback)(() => { const current4 = c15779_1.current; const r8 = c15779_2.current ? current4 : { ...current4, paymentSourceId: null }; c15779_2.current = true; return createOneTimePurchaseInvoicePreview(r8); }, [JSON.stringify(a0)]); const r22 = env15780 = { $parent: env4209 }; env15780[0] = void 0; env15780[1] = void 0; let preventFetch = a0.preventFetch; if (preventFetch === void 0) { preventFetch = false; } env15780[0] = preventFetch; env15780[1] = r21; env15780[2] = void 0; env15780[3] = void 0; const r11 = _slicedToArray(useState(null), 2); const r28 = r11[0]; env15780[2] = r11[1]; const r31 = _slicedToArray(useState(null), 2); const r32 = r31[0]; env15780[3] = r31[1]; useEffect($mk78770(r22), [preventFetch, r21, a1]); return [r28, r32]; } export function useFetchSubscriptionInvoicePreview(a0, a1) { let c15781_3, c15781_4, c15781_5; let current2 = a0; const r1 = "subscriptionId" in a0; let r15 = a0; if (r1) { const subscriptionId = a0.subscriptionId; r15 = a0; if (subscriptionId == null) { a0.subscriptionId; current2 = _objectWithoutProperties(a0, _excluded); r15 = current2; } } let env15783; let c15781_1 = useRef(r15); let c15781_2 = useRef(false); const ref = useRef(null); let c15782_0 = ref; useEffect(() => { c15782_0.current = null; }, [a1]); const callback = useCallback((a0, a1) => { const current4 = c15782_0.current; return current4 != null && a0.paymentSourceId != null && a0.paymentSourceId === current4.serverSelectedPaymentSourceId && a1 === current4.dedupeKey; }, []); const callback2 = useCallback((a0, a1, a2) => { const r1 = a2 == null; let current4 = null; if (r1) { const payment_sources = a0.checkoutContext?.payment_sources; const r15 = payment_sources == null; let serverSelectedPaymentSourceId = null; if (!r15) { let r19 = payment_sources.find((payment_source) => payment_source.enabled)?.id; if (r19 == null) { r19 = null; } serverSelectedPaymentSourceId = r19; } current4 = { record: a0, dedupeKey: a1, serverSelectedPaymentSourceId }; } c15782_0.current = current4; }, []); c15781_3 = ref; c15781_4 = callback; c15781_5 = callback2; useEffect(() => { c15781_1.current = current2; }); const callback3 = useCallback(() => { const current4 = c15781_1.current; const r2 = c15781_2.current ? current4 : { ...current4, paymentSourceId: null }; let c25919_1; let c25919_0 = r2; if ("subscriptionId" in r2) { c15781_2.current = true; return updateSubscriptionInvoicePreview(r2); } if ("items" in r2) { c15781_2.current = true; const r26 = JSON.stringify({ ...r2, paymentSourceId: "exclude_from_dedupe" }); c25919_1 = r26; const current5 = c15781_3.current; return current5 != null && c15781_4(r2, r26) ? Promise.resolve(current5.record) : createSubscriptionInvoicePreview(r2).then((result) => { c15781_5(result, c25919_1, c25919_0.paymentSourceId); return result; }); } return null; }, [JSON.stringify(r15), ref, callback, callback2]); const r43 = env15783 = { $parent: env4209 }; env15783[0] = void 0; env15783[1] = void 0; let preventFetch = r15.preventFetch; if (preventFetch === void 0) { preventFetch = false; } env15783[0] = preventFetch; env15783[1] = callback3; env15783[2] = void 0; env15783[3] = void 0; const r48 = _slicedToArray(useState(null), 2); const r49 = r48[0]; env15783[2] = r48[1]; const r52 = _slicedToArray(useState(null), 2); const r53 = r52[0]; env15783[3] = r52[1]; useEffect($mk78770(r43), [preventFetch, callback3, a1]); return [r49, r53]; } export function useGetSubscriptionInvoice(a0, a1) { let env15785; const current3 = a0; let c15784_1 = (0, useRef)(a0); useEffect(() => { c15784_1.current = current3; }); const r20 = (0, useCallback)(() => getSubscriptionInvoice(c15784_1.current), [JSON.stringify(a0)]); const r21 = env15785 = { $parent: env4209 }; env15785[0] = void 0; env15785[1] = void 0; let preventFetch = a0.preventFetch; if (preventFetch === void 0) { preventFetch = false; } env15785[0] = preventFetch; env15785[1] = r20; env15785[2] = void 0; env15785[3] = void 0; const r11 = _slicedToArray(useState(null), 2); const r26 = r11[0]; env15785[2] = r11[1]; const r29 = _slicedToArray(useState(null), 2); const r30 = r29[0]; env15785[3] = r29[1]; useEffect($mk78770(r21), [preventFetch, r20, a1]); return [r26, r30]; } export function getItemUnitPriceWithDiscount(a0) { let subscriptionPlanPrice = a0.subscriptionPlanPrice; a0.discounts.forEach((discount) => { subscriptionPlanPrice = subscriptionPlanPrice - discount.amount / a0.quantity; }); return subscriptionPlanPrice; }