// Metro module 13939 (f13940). import { useMemo, useState, useCallback } from "react"; import FamilyCenterStore from "./FamilyCenterStore"; import { ParentalControlledSpendingLimit } from "../user_settings/family_center/ParentalControlledUserSettings"; import { useStateFromStores } from "../../_bundle/Module589"; import { CurrencyExponents } from "../../_bundle/convertToMajorCurrencyUnits"; import { getCurrencySymbol, getNextRenewalDateLabel, sanitizeAmountInput } from "./SpendingLimitUtils"; export function useChangeSpendingLimitFormState(a0) { let currency, c20801_3, c20801_5, c20801_6, amount; let c20801_0 = a0; const controlledSetting = ParentalControlledSpendingLimit.useControlledSetting(a0); const stateFromStores = useStateFromStores([FamilyCenterStore], () => FamilyCenterStore.getSpendingLimit()); const stateFromStores2 = useStateFromStores([FamilyCenterStore], () => { let r6 = FamilyCenterStore.getMonthlyPurchases()?.total_amount; if (r6 == null) { r6 = null; } return r6; }); let r37 = controlledSetting?.currency; if (r37 == null) { r37 = stateFromStores?.currency; } if (r37 == null) { r37 = "usd"; } currency = r37.toLowerCase(); let exponent = CurrencyExponents[currency]; if (exponent == null) { exponent = 2; } c20801_3 = exponent; const memo = useMemo(() => getCurrencySymbol(currency), [currency]); const memo2 = useMemo(getNextRenewalDateLabel, []); let [amountInput, setState] = useState(() => controlledSetting == null ? "" : String(controlledSetting.amount / 10 ** c20801_3)); const callback = useCallback((a0) => setState(sanitizeAmountInput(a0, c20801_3)), [exponent]); const isClearingCap = amountInput === "" && controlledSetting != null; c20801_5 = isClearingCap; const r22 = parseFloat(amountInput); const isNaN2 = Number.isNaN(r22); const r3 = isNaN2 ? !isNaN2 : r22 >= 0; const canSave = isClearingCap || r3; c20801_6 = canSave; amount = r3 ? Math.round(r22 * 10 ** exponent) : null; const [isSubmitting, setState2] = useState(false); return { amountInput, handleAmountChange: callback, currency, currencySymbol: memo, exponent, isClearingCap, isOverspending: stateFromStores2 != null && stateFromStores2 > 0 && amount != null && amount <= stateFromStores2, canSave, isSubmitting, renewalDate: memo2, monthlySpend: stateFromStores2, save: useCallback(async () => { let c27591_2; let c27591_3 = 0; L18: { try { if (!c20801_6) { break L18; } setState2(true); c27591_3 = 1; if (c20801_5) { await ParentalControlledSpendingLimit.updateControlledSetting(c20801_0, null); } else { if (amount == null) { c27591_3 = 0; (0, setState2)(false); break L18; } await ParentalControlledSpendingLimit.updateControlledSetting(c20801_0, { amount, currency }); } c27591_3 = 0; (0, setState2)(false); } catch ($e) { const r29 = $e; let r7 = r29; c27591_2 = r29; if (c27591_3 !== 0) { while (true) { try { const r102 = c27591_2; c27591_3 = 0; (0, setState2)(false); throw r102; } catch (r99) { r7 = r99; c27591_2 = r99; if (c27591_3 === 0) { break; } } } } throw r7; } return void 0; } }, [canSave, isClearingCap, amount, a0, currency]) }; }