// Metro module 8804 (f8805). import _slicedToArray from "../../../_bundle/_slicedToArray"; import { useState, useMemo, useEffect, useRef, useCallback } from "react"; import BadgeDirectoryStore from "../../badges/BadgeDirectoryStore"; import PromotionsStore from "../../premium/promotions/PromotionsStore"; import GiftCodeRecord from "../../../records/GiftCodeRecord"; import UserStore from "../../../stores/UserStore"; import { AnalyticEvents } from "../../../Constants"; import { GPlayBillingResult } from "../../gplay/native/Constants"; import { PremiumTypes, SubscriptionIntervalTypes, SubscriptionPlanInfo } from "../../premium/PremiumConstants"; import { jsx } from "react/jsx-runtime"; import Logger from "../../debug/Logger"; import ContextUtils from "../../../utils/ContextUtils"; import { patchOrder, patchOrderLineItem, updateOrder, markOrderAsSigningInProgress } from "./OrderActionCreators"; import { captureBillingException } from "../../../utils/BillingUtils"; import NitroGiftPriceCopyExperiment from "../../premium/gifting/experiments/NitroGiftPriceCopyExperiment"; import { useGiftStyles } from "../../premium/gifting/hooks/useGiftStyles"; import { intl, t } from "../../../intl/index.native"; import { useOrderContext } from "./hooks/useOrderContext"; import { getPlanIdForPremiumType } from "../../../utils/PremiumUtils"; import { getProductIdForGift } from "../../premium/native/ProductIds.android"; import { useAndroidShopOrdersEnabled } from "../../billing/experiments/AndroidShopOrdersExperiment"; import { useHandlePremiumPurchase } from "../../premium/native/handlePremiumPurchase.android"; import { useStateFromStoresArray, useStateFromStores } from "../../../_bundle/Module589"; import { useFetchClaimableGiftingPromotionRewardSkuIds } from "../../premium/gifting/utils/promotions/GiftingPromotionUtils"; import GiftingBadgeExperiment from "../../premium/gifting/experiments/GiftingBadgeExperiment"; import { fetchBadge } from "../../badges/BadgeDirectoryActionCreators"; import { BadgeId } from "../../../_bundle/Module8106"; import AlertActionCreators from "../../../actions/native/AlertActionCreators"; import AnalyticsUtils from "../../../utils/AnalyticsUtils"; import AnalyticsLocation from "../../app_analytics/AnalyticsLocation"; import { logGiftIntentFlowPurchasedGift } from "../../premium/gifting/PremiumGiftingIntentActionCreators"; import Dispatcher from "../../../Dispatcher"; import { isAndroid } from "../../../utils/PlatformUtils"; import { PaymentFlowStep } from "../../premium/native/PremiumAnalyticsUtils"; let env5736 = {}; const c5736_16 = new Logger("NativeGiftContext"); const [c5736_17, r56, r57] = ContextUtils(); export function NativeGiftContextProvider(a0) { let env16787 = { $parent: env5736 }; const basePurchaseAnalytics = a0.basePurchaseAnalytics; env16787[0] = basePurchaseAnalytics; const recipientUserId2 = a0.recipientUserId; env16787[1] = recipientUserId2; let premiumType = a0.premiumType; let planInterval = a0.planInterval; const onClose = a0.onClose; env16787[2] = onClose; const setCurrentAnalyticsStep = a0.setCurrentAnalyticsStep; env16787[3] = setCurrentAnalyticsStep; const { initialOrder, children } = a0; env16787[4] = void 0; env16787[5] = void 0; env16787[6] = void 0; env16787[7] = void 0; env16787[8] = void 0; env16787[9] = void 0; env16787[10] = void 0; env16787[11] = void 0; env16787[12] = void 0; env16787[13] = void 0; env16787[14] = void 0; env16787[15] = void 0; env16787[16] = void 0; env16787[17] = void 0; env16787[18] = void 0; env16787[19] = void 0; env16787[20] = void 0; env16787[21] = void 0; env16787[22] = void 0; env16787[23] = void 0; env16787[24] = void 0; env16787[25] = void 0; env16787[26] = void 0; env16787[27] = void 0; env16787[28] = void 0; env16787[29] = void 0; env16787[30] = void 0; env16787[31] = void 0; env16787[32] = void 0; env16787[33] = void 0; env16787[34] = void 0; env16787[35] = void 0; env16787[36] = void 0; env16787[37] = void 0; env16787[38] = void 0; env16787[39] = void 0; env16787[40] = void 0; env16787[41] = void 0; if (premiumType == null) { premiumType = PremiumTypes.TIER_2; } const r53 = _slicedToArray(useState(premiumType), 2); const r32 = r53[0]; env16787[4] = r32; const r21 = r53[1]; env16787[5] = r21; const monthlyDefaultSelected = NitroGiftPriceCopyExperiment.useConfig({ location: "NativeGiftContext" }).monthlyDefaultSelected; if (planInterval == null) { planInterval = monthlyDefaultSelected ? SubscriptionIntervalTypes.MONTH : SubscriptionIntervalTypes.YEAR; } const r64 = _slicedToArray(useState(planInterval), 2); const r31 = r64[0]; env16787[6] = r31; const r20 = r64[1]; env16787[7] = r20; const r72 = _slicedToArray(useState(useGiftStyles()[0]), 2); const r33 = r72[0]; env16787[8] = r33; env16787[9] = r72[1]; const r85 = _slicedToArray(useState(intl.string(t.ZkOo1U)), 2); const r29 = r85[0]; env16787[10] = r29; env16787[11] = r85[1]; const r89 = _slicedToArray(useState(void 0), 2); const r28 = r89[0]; env16787[12] = r28; env16787[13] = r89[1]; const r93 = _slicedToArray(useState(void 0), 2); const r27 = r93[0]; env16787[14] = r27; env16787[15] = r93[1]; const r97 = _slicedToArray(useState(void 0), 2); const r26 = r97[0]; env16787[16] = r26; env16787[17] = r97[1]; const r101 = _slicedToArray(useState(false), 2); const r25 = r101[0]; env16787[18] = r25; env16787[19] = r101[1]; const r105 = _slicedToArray(useState(null), 2); const r106 = r105[0]; env16787[20] = r106; const r41 = r105[1]; env16787[21] = r41; const r109 = _slicedToArray(useState(), 2); const r110 = r109[0]; env16787[22] = r110; const r111 = r109[1]; env16787[23] = r111; const r114 = _slicedToArray(useState(null), 2); const r24 = r114[0]; env16787[24] = r24; env16787[25] = r114[1]; const orderContext = useOrderContext(initialOrder, "NativeGiftContext"); env16787[26] = orderContext; const planIdForPremiumType2 = getPlanIdForPremiumType(r32, r31); env16787[27] = planIdForPremiumType2; const productIdForGift = getProductIdForGift(planIdForPremiumType2); env16787[28] = productIdForGift; const r127 = SubscriptionPlanInfo[planIdForPremiumType2]; if (r127 == null) { throw new Error(`Invalid subscription plan for gift purchase: ${planIdForPremiumType2}`); } const skuId = r127.skuId; env16787[29] = skuId; const androidShopOrdersEnabled = useAndroidShopOrdersEnabled({ location: "NativeGiftContext" }); env16787[30] = androidShopOrdersEnabled; const memo3 = useMemo(() => env16787[30] ? { line_items: [{ external_product_id: env16787[28] }] } : void 0, [androidShopOrdersEnabled, productIdForGift]); const handlePremiumPurchase = useHandlePremiumPurchase(); env16787[31] = handlePremiumPurchase; const stateFromStoresArray = useStateFromStoresArray([PromotionsStore], () => PromotionsStore.getGiftPromotionRewardSkuIds()); env16787[32] = stateFromStoresArray; const fetchClaimableGiftingPromotionRewardSkuIds = useFetchClaimableGiftingPromotionRewardSkuIds(); env16787[33] = fetchClaimableGiftingPromotionRewardSkuIds; const r3 = fetchClaimableGiftingPromotionRewardSkuIds != null && fetchClaimableGiftingPromotionRewardSkuIds.length > 0; let state, recipientUserId, state2, state3, state4, state5, state6; env16787[34] = r3; const enabled = GiftingBadgeExperiment.useConfig({ location: "NativeGiftContext" }).enabled; env16787[35] = enabled; useEffect(() => { if (env16787[35]) { fetchBadge(BadgeId.GIFTING); } }, [enabled]); const stateFromStores = useStateFromStores([UserStore], () => { const r4 = env16787[1]; return r4 != null ? UserStore.getUser(r4) : void 0; }); env16787[36] = stateFromStores; state = r33; recipientUserId = recipientUserId2; state2 = r29; state3 = r27; state4 = r28; state5 = r110; state6 = r32; const r168 = [state, recipientUserId, state2, state3?.id, state3?.surrogates, , , ,]; const soundId = state4?.soundId; let order, revision, setRevision, setOrder, planIdForPremiumType, memo, memo2, state7, state8, state9, setState, setState2, setState3, ref, ref2, setState4; let c16790_0; const r174 = () => ({ gift_style: state, recipient_id: recipientUserId, custom_message: state2, emoji_id: state3?.id, emoji_name: state3?.surrogates, sound_id: state4?.soundId, reward_sku_ids: state5 != null && state6 === PremiumTypes.TIER_2 ? [state5] : [] }); r168[5] = soundId; r168[6] = state5; r168[7] = state6; const memo4 = useMemo(r174, r168); env16787[37] = memo4; useEffect(() => { if (env16787[20] != null) { AlertActionCreators.show({ title: intl.string(t.R0RpRX), body: intl.string(t.CKsXk3) }); (0, env16787[21])(null); } }, [r106]); useEffect(() => { if (env16787[34]) { AnalyticsUtils.track(AnalyticEvents.GIFT_PROMOTION_REWARD_SELECTED, { user_id: UserStore.getCurrentUser()?.id, reward_sku_id: env16787[22] }); } }, [state5, r3]); const { order: order2, revision: revision3, setRevision: setRevision2, setOrder: setOrder2 } = orderContext; order = order2; revision = revision3; setRevision = setRevision2; setOrder = setOrder2; planIdForPremiumType = planIdForPremiumType2; memo = memo3; memo2 = memo4; state7 = r25; state8 = state6; state9 = r31; setState = r21; setState2 = r20; setState3 = r41; ref = void 0; ref2 = void 0; setState4 = void 0; ref = useRef({ orderId: void 0, planId: null, planSelection: { premiumType: state6, planInterval: state9 }, giftInfo: null }); ref2 = useRef(false); let state10; [state10, setState4] = useState(0); useEffect(() => { const id4 = order?.id; let needsPlanSync, needsGiftSync, c29977_5, id3, revision2; const id = id4; const r16 = order == null; let r7 = void 0; if (!r16) { const order_line_items = order.order_line_items; r7 = order_line_items == null ? void 0 : order_line_items[0]; } let c29977_1 = r7; const id2 = r7?.id; const sku_id = r7?.sku_id; let planId = r7?.subscription_plan_id; if (planId == null) { planId = null; } if (order != null && ref.current.orderId !== order.id) { ref.current.orderId = order.id; ref.current.planId = planId; ref.current.planSelection = { premiumType: state8, planInterval: state9 }; } L34: { if (id != null && r7 != null && id2 != null && revision != null && !state7 && !ref2.current) { needsPlanSync = ref.current.planId !== planIdForPremiumType && (sku_id == null || SubscriptionPlanInfo[planIdForPremiumType]?.skuId === sku_id); needsGiftSync = ref.current.giftInfo !== memo2; if (!needsPlanSync && !needsGiftSync) { break L34; } c29977_5 = false; id3 = order?.id; ref2.current = true; revision2 = revision; (async () => { let r68; let c31473_2; let c31473_3 = 0; let c31980_0, c31980_1; L33: { L18: { L17: { L25: { L11: { try { c31980_0 = void 0; if (needsPlanSync) { c31473_3 = 1; if (memo != null) { c31980_0 = await patchOrder({ orderId: id, expectedRevision: revision2, orderLineItems: [{ sku_id: c29977_1.sku_id, quantity: c29977_1.quantity, purchase_type: c29977_1.purchase_type, subscription_plan_id: planIdForPremiumType }], externalGatewayFacet: memo }); if (ref.current.orderId !== id3) { c31473_3 = 0; break L33; } revision2 = c31980_0.revision; (0, setOrder)(c31980_0); } else { revision2 = await patchOrderLineItem({ orderId: id, orderLineItemId: id2, subscriptionPlanId: planIdForPremiumType, expectedRevision: revision2 }); if (ref.current.orderId !== id3) { c31473_3 = 0; break L25; } } ref.current.planId = planIdForPremiumType; ref.current.planSelection = { premiumType: state8, planInterval: state9 }; (0, setRevision)(revision2); c31473_3 = 0; if (!needsGiftSync) { break L17; } r68 = await updateOrder({ orderId: id, giftInfo: { recipient_id: memo2.recipient_id, gift_style: memo2.gift_style, emoji_id: memo2.emoji_id, emoji_name: memo2.emoji_name, sound_id: memo2.sound_id, reward_sku_ids: memo2.reward_sku_ids, custom_message_contents: memo2.custom_message }, expectedRevision: revision2 }); } else { if (!needsGiftSync) { break L11; } r68 = await updateOrder({ orderId: id, giftInfo: { recipient_id: memo2.recipient_id, gift_style: memo2.gift_style, emoji_id: memo2.emoji_id, emoji_name: memo2.emoji_name, sound_id: memo2.sound_id, reward_sku_ids: memo2.reward_sku_ids, custom_message_contents: memo2.custom_message }, expectedRevision: revision2 }); } revision2 = r68; if (ref.current.orderId !== id3) { break L18; } ref.current.giftInfo = memo2; (0, setRevision)(revision2); break L17; } catch ($e) { const r34 = $e; let r7 = r34; c31473_2 = r34; if (c31473_3 !== 0) { while (true) { try { const r245 = c31473_2; c31473_3 = 0; c31980_1 = r245; if (!c29977_5) { (0, setState)(ref.current.planSelection.premiumType); (0, setState2)(ref.current.planSelection.planInterval); } throw c31980_1; } catch (r241) { r7 = r241; c31473_2 = r241; if (c31473_3 === 0) { break; } } } } throw r7; } } return void 0; } return void 0; } return void 0; } return void 0; } })().catch((a0) => { captureBillingException(a0, { tags: { source: "NativeGiftContext_syncOrder" }, extra: { orderId: id, planId: planIdForPremiumType, needsPlanSync, needsGiftSync } }); c5736_16.error("Failed to sync order", { error: a0, orderId: id }); if (!c29977_5) { setState3(a0); } }).finally(() => { ref2.current = false; if (ref.current.orderId !== id3) { setState4((prevState4) => prevState4 + 1); } }); return () => { c29977_5 = true; }; } } }, [order, revision, planIdForPremiumType, memo, memo2, state7, state6, state9, setState, setState2, setRevision, setOrder, setState3, state10]); const callback = useCallback((c26281_0) => { const handleGiftCodeCreate2 = function handleGiftCodeCreate(a0) { const fromServer = GiftCodeRecord.createFromServer(a0.giftCode); if (fromServer.subscriptionPlanId === env16787[27]) { (0, env16787[17])(fromServer); const r18 = env16787[1]; let r19 = r18 != null; if (r19) { const location_stack = env16787[0].location_stack; r19 = location_stack == null ? void 0 : location_stack.includes(AnalyticsLocation.PREMIUM_GIFT_INTENT_CARD); } if (r19) { logGiftIntentFlowPurchasedGift(r18); } if (c26281_0 != null) { c26281_0(); } Dispatcher.unsubscribe("GIFT_CODE_CREATE", handleGiftCodeCreate2); } (0, env16787[19])(false); }; return handleGiftCodeCreate2; }, [planIdForPremiumType, recipientUserId, basePurchaseAnalytics]); env16787[38] = callback; c16790_0 = void 0; c16790_0 = async (c29978_0) => { let c29978_3; let c29978_4 = 0; let c31474_0; L14: { L13: { try { Dispatcher.subscribe("GIFT_CODE_CREATE", c29978_0); if (isAndroid()) { break L14; } if (env16787[26].orderId == null) { break L14; } c29978_4 = 1; c5736_16.info("Starting order signing with pre-created order", { orderId: env16787[26].orderId, skuId: env16787[29] }); await markOrderAsSigningInProgress(env16787[26].orderId); c29978_4 = 0; } catch ($e) { const r33 = $e; let r7 = r33; c29978_3 = r33; if (c29978_4 !== 0) { while (true) { try { const r92 = c29978_3; c29978_4 = 0; c31474_0 = r92; captureBillingException(c31474_0, { tags: { source: "NativeGiftContext_handlePurchaseComplete_sign" }, extra: { skuId: env16787[29], orderId: env16787[26].orderId } }); c5736_16.error("Failed to sign order in purchase completion", { error: c31474_0, skuId: env16787[29], orderId: env16787[26].orderId }); break L13; } catch (r88) { r7 = r88; c29978_3 = r88; if (c29978_4 === 0) { break; } } } } throw r7; } } return void 0; } }; const callback2 = useCallback(function() { return c16790_0.apply(this, arguments); }, [skuId, orderContext.orderId]); env16787[39] = callback2; const callback3 = useCallback((a0) => { Dispatcher.unsubscribe("GIFT_CODE_CREATE", a0); (0, env16787[3])(PaymentFlowStep.PLAN_SELECT); (0, env16787[19])(false); }, [setCurrentAnalyticsStep]); env16787[40] = callback3; const callback4 = useCallback((a0) => { if (BadgeDirectoryStore.getNextTier(BadgeId.GIFTING) != null) { let r24 = BadgeDirectoryStore.getSingleRequirementProgress(BadgeId.GIFTING)?.current; if (r24 == null) { r24 = null; } (0, env16787[25])(r24); } else { (0, env16787[25])(null); } let c26282_0, handleGPlayUpdatePurchaseAction2; (0, env16787[19])(true); let c26282_1 = (0, env16787[38])(a0); if (isAndroid()) { handleGPlayUpdatePurchaseAction2 = function handleGPlayUpdatePurchaseAction(a0) { if (a0.isActivePurchase) { Dispatcher.unsubscribe("GPLAY_UPDATE_PURCHASE_STATE", handleGPlayUpdatePurchaseAction2); if (a0.billingResult !== GPlayBillingResult.OK) { (0, env16787[40])(c26282_1); } } }; Dispatcher.subscribe("GPLAY_UPDATE_PURCHASE_STATE", handleGPlayUpdatePurchaseAction2); } (0, env16787[3])(PaymentFlowStep.REVIEW); const r49 = env16787[31]; const r52 = env16787[0]; c26282_0 = async () => await (0, env16787[39])(c26282_1); r49({ productId: env16787[28], isGift: true, analyticsLoadId: r52.load_id, analyticsLocation: r52.location, analyticsLocations: r52.location_stack, allowPlanChange: false, giftInfoOptions: env16787[37], onPurchaseComplete: function() { return c26282_0.apply(this, arguments); }, onPurchaseError: () => (0, env16787[40])(c26282_1), orderId: env16787[26].orderId, analyticsData: { load_id: r52.load_id, succeededOnlyFields: { is_custom_message_edited: env16787[10] !== intl.string(t.ZkOo1U), is_custom_emoji_sound_available: false } } }); }, [callback, setCurrentAnalyticsStep, memo2, handlePremiumPurchase, productIdForGift, basePurchaseAnalytics, callback2, callback3, orderContext.orderId, state2]); env16787[41] = callback4; return (0, jsx)(c5736_17.Provider, { value: useMemo(() => ({ ...env16787[26], recipientUserId: env16787[1], recipientUser: env16787[36], giftStyle: env16787[8], premiumType: env16787[4], planInterval: env16787[6], productId: env16787[28], customGiftMessage: env16787[10], soundEffect: env16787[12], emojiConfetti: env16787[14], giftCodeRecord: env16787[16], isPurchasing: env16787[18], prePurchaseGiftingBadgeProgress: env16787[24], basePurchaseAnalytics: env16787[0], setCurrentAnalyticsStep: env16787[3], setPremiumType: env16787[5], setPlanInterval: env16787[7], setGiftStyle: env16787[9], setCustomGiftMessage: env16787[11], setSoundEffect: env16787[13], setEmojiConfetti: env16787[15], onClose: env16787[2], onPurchase: env16787[41], allRewards: env16787[32], claimableRewards: env16787[33], selectedGiftingPromotionReward: env16787[22], setSelectedGiftingPromotionReward: env16787[23] }), [orderContext, recipientUserId, stateFromStores, state, state6, state9, productIdForGift, state2, state4, state3, r26, state7, r24, basePurchaseAnalytics, setCurrentAnalyticsStep, setState, setState2, onClose, callback4, stateFromStoresArray, fetchClaimableGiftingPromotionRewardSkuIds, state5, r111]), children }); } export { r56 as useNativeGiftContext }; export { r57 as useForwardedNativeGiftContext };