@t402/react 2.0.0

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.
@@ -0,0 +1,737 @@
1
+ 'use strict';
2
+
3
+ var jsxRuntime = require('react/jsx-runtime');
4
+ var react = require('react');
5
+
6
+ // src/components/Spinner.tsx
7
+ var sizeClasses = {
8
+ sm: { width: 16, height: 16, borderWidth: 2 },
9
+ md: { width: 24, height: 24, borderWidth: 3 },
10
+ lg: { width: 32, height: 32, borderWidth: 4 }
11
+ };
12
+ function Spinner({ size = "md", className = "" }) {
13
+ const { width, height, borderWidth } = sizeClasses[size];
14
+ const spinnerStyle = {
15
+ width,
16
+ height,
17
+ border: `${borderWidth}px solid #e5e7eb`,
18
+ borderTopColor: "#3b82f6",
19
+ borderRadius: "50%",
20
+ animation: "t402-spin 0.8s linear infinite",
21
+ display: "inline-block"
22
+ };
23
+ return /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
24
+ /* @__PURE__ */ jsxRuntime.jsx("style", { children: `@keyframes t402-spin { to { transform: rotate(360deg); } }` }),
25
+ /* @__PURE__ */ jsxRuntime.jsx(
26
+ "span",
27
+ {
28
+ style: spinnerStyle,
29
+ className,
30
+ role: "status",
31
+ "aria-label": "Loading"
32
+ }
33
+ )
34
+ ] });
35
+ }
36
+ var baseStyles = {
37
+ display: "inline-flex",
38
+ alignItems: "center",
39
+ justifyContent: "center",
40
+ gap: "8px",
41
+ fontWeight: 600,
42
+ borderRadius: "8px",
43
+ cursor: "pointer",
44
+ transition: "all 0.15s ease",
45
+ border: "none",
46
+ fontFamily: "inherit"
47
+ };
48
+ var variantStyles = {
49
+ primary: {
50
+ backgroundColor: "#2563eb",
51
+ color: "#ffffff"
52
+ },
53
+ secondary: {
54
+ backgroundColor: "#6b7280",
55
+ color: "#ffffff"
56
+ },
57
+ outline: {
58
+ backgroundColor: "transparent",
59
+ color: "#2563eb",
60
+ border: "2px solid #2563eb"
61
+ }
62
+ };
63
+ var sizeStyles = {
64
+ sm: {
65
+ padding: "8px 16px",
66
+ fontSize: "14px"
67
+ },
68
+ md: {
69
+ padding: "12px 24px",
70
+ fontSize: "16px"
71
+ },
72
+ lg: {
73
+ padding: "16px 32px",
74
+ fontSize: "18px"
75
+ }
76
+ };
77
+ var disabledStyles = {
78
+ opacity: 0.6,
79
+ cursor: "not-allowed"
80
+ };
81
+ function PaymentButton({
82
+ onClick,
83
+ disabled = false,
84
+ loading = false,
85
+ children = "Pay Now",
86
+ className = "",
87
+ variant = "primary",
88
+ size = "md"
89
+ }) {
90
+ const [isHovered, setIsHovered] = react.useState(false);
91
+ const handleClick = react.useCallback(async () => {
92
+ if (disabled || loading || !onClick) return;
93
+ await onClick();
94
+ }, [disabled, loading, onClick]);
95
+ const isDisabled = disabled || loading;
96
+ const combinedStyles = {
97
+ ...baseStyles,
98
+ ...variantStyles[variant],
99
+ ...sizeStyles[size],
100
+ ...isDisabled ? disabledStyles : {},
101
+ ...isHovered && !isDisabled ? { filter: "brightness(1.1)", transform: "translateY(-1px)" } : {}
102
+ };
103
+ return /* @__PURE__ */ jsxRuntime.jsxs(
104
+ "button",
105
+ {
106
+ type: "button",
107
+ onClick: handleClick,
108
+ disabled: isDisabled,
109
+ className,
110
+ style: combinedStyles,
111
+ onMouseEnter: () => setIsHovered(true),
112
+ onMouseLeave: () => setIsHovered(false),
113
+ children: [
114
+ loading && /* @__PURE__ */ jsxRuntime.jsx(Spinner, { size: "sm" }),
115
+ children
116
+ ]
117
+ }
118
+ );
119
+ }
120
+ var statusStyles = {
121
+ idle: {
122
+ backgroundColor: "#f3f4f6",
123
+ color: "#374151",
124
+ borderColor: "#d1d5db"
125
+ },
126
+ loading: {
127
+ backgroundColor: "#eff6ff",
128
+ color: "#1d4ed8",
129
+ borderColor: "#93c5fd"
130
+ },
131
+ success: {
132
+ backgroundColor: "#f0fdf4",
133
+ color: "#166534",
134
+ borderColor: "#86efac"
135
+ },
136
+ error: {
137
+ backgroundColor: "#fef2f2",
138
+ color: "#dc2626",
139
+ borderColor: "#fca5a5"
140
+ }
141
+ };
142
+ var baseStyles2 = {
143
+ display: "flex",
144
+ alignItems: "center",
145
+ gap: "8px",
146
+ padding: "12px 16px",
147
+ borderRadius: "8px",
148
+ border: "1px solid",
149
+ fontSize: "14px",
150
+ fontWeight: 500
151
+ };
152
+ var icons = {
153
+ idle: "\u25CB",
154
+ loading: "",
155
+ success: "\u2713",
156
+ error: "\u2715"
157
+ };
158
+ function PaymentStatus({ status, message, className = "" }) {
159
+ const statusStyle = statusStyles[status] || statusStyles.idle;
160
+ const defaultMessages = {
161
+ idle: "Ready to pay",
162
+ loading: "Processing...",
163
+ success: "Payment successful",
164
+ error: "Payment failed"
165
+ };
166
+ const displayMessage = message || defaultMessages[status];
167
+ return /* @__PURE__ */ jsxRuntime.jsxs(
168
+ "div",
169
+ {
170
+ style: { ...baseStyles2, ...statusStyle },
171
+ className,
172
+ role: "status",
173
+ "aria-live": "polite",
174
+ children: [
175
+ status === "loading" ? /* @__PURE__ */ jsxRuntime.jsx(Spinner, { size: "sm" }) : /* @__PURE__ */ jsxRuntime.jsx("span", { style: { fontSize: "16px" }, children: icons[status] }),
176
+ /* @__PURE__ */ jsxRuntime.jsx("span", { children: displayMessage })
177
+ ]
178
+ }
179
+ );
180
+ }
181
+
182
+ // src/utils/index.ts
183
+ var EVM_CHAIN_IDS = {
184
+ ETHEREUM_MAINNET: "1",
185
+ BASE_MAINNET: "8453",
186
+ BASE_SEPOLIA: "84532",
187
+ ARBITRUM_MAINNET: "42161",
188
+ ARBITRUM_SEPOLIA: "421614"
189
+ };
190
+ var SOLANA_NETWORK_REFS = {
191
+ MAINNET: "5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp",
192
+ DEVNET: "EtWTRABZaYq6iMfeYKouRu166VU2xqa1"
193
+ };
194
+ var TON_NETWORK_REFS = {
195
+ MAINNET: "-239",
196
+ TESTNET: "-3"
197
+ };
198
+ var TRON_NETWORK_REFS = {
199
+ MAINNET: "mainnet",
200
+ NILE: "nile",
201
+ SHASTA: "shasta"
202
+ };
203
+ function normalizePaymentRequirements(paymentRequirements) {
204
+ if (Array.isArray(paymentRequirements)) {
205
+ return paymentRequirements;
206
+ }
207
+ return [paymentRequirements];
208
+ }
209
+ function getPreferredNetworks(testnet) {
210
+ if (testnet) {
211
+ return [
212
+ `eip155:${EVM_CHAIN_IDS.BASE_SEPOLIA}`,
213
+ `eip155:${EVM_CHAIN_IDS.ARBITRUM_SEPOLIA}`,
214
+ `solana:${SOLANA_NETWORK_REFS.DEVNET}`
215
+ ];
216
+ }
217
+ return [
218
+ `eip155:${EVM_CHAIN_IDS.BASE_MAINNET}`,
219
+ `eip155:${EVM_CHAIN_IDS.ARBITRUM_MAINNET}`,
220
+ `solana:${SOLANA_NETWORK_REFS.MAINNET}`
221
+ ];
222
+ }
223
+ function choosePaymentRequirement(paymentRequirements, testnet) {
224
+ const normalized = normalizePaymentRequirements(paymentRequirements);
225
+ const preferredNetworks = getPreferredNetworks(testnet);
226
+ for (const preferredNetwork of preferredNetworks) {
227
+ const match = normalized.find((req) => req.network === preferredNetwork);
228
+ if (match) {
229
+ return match;
230
+ }
231
+ }
232
+ return normalized[0];
233
+ }
234
+ function isEvmNetwork(network) {
235
+ return network.startsWith("eip155:");
236
+ }
237
+ function isSvmNetwork(network) {
238
+ return network.startsWith("solana:");
239
+ }
240
+ function isTonNetwork(network) {
241
+ return network.startsWith("ton:");
242
+ }
243
+ function isTronNetwork(network) {
244
+ return network.startsWith("tron:");
245
+ }
246
+ var EVM_CHAIN_NAMES = {
247
+ "1": "Ethereum",
248
+ "10": "Optimism",
249
+ "137": "Polygon",
250
+ "8453": "Base",
251
+ "42161": "Arbitrum One",
252
+ "84532": "Base Sepolia",
253
+ "421614": "Arbitrum Sepolia",
254
+ "11155111": "Sepolia"
255
+ };
256
+ function getNetworkDisplayName(network) {
257
+ if (network.startsWith("eip155:")) {
258
+ const chainId = network.split(":")[1];
259
+ return EVM_CHAIN_NAMES[chainId] ?? `Chain ${chainId}`;
260
+ }
261
+ if (network.startsWith("solana:")) {
262
+ const ref = network.split(":")[1];
263
+ return ref === SOLANA_NETWORK_REFS.DEVNET ? "Solana Devnet" : "Solana";
264
+ }
265
+ if (network.startsWith("ton:")) {
266
+ const ref = network.split(":")[1];
267
+ return ref === TON_NETWORK_REFS.TESTNET ? "TON Testnet" : "TON";
268
+ }
269
+ if (network.startsWith("tron:")) {
270
+ const ref = network.split(":")[1];
271
+ if (ref === TRON_NETWORK_REFS.NILE) return "TRON Nile";
272
+ if (ref === TRON_NETWORK_REFS.SHASTA) return "TRON Shasta";
273
+ return "TRON";
274
+ }
275
+ return network;
276
+ }
277
+ var TESTNET_CHAIN_IDS = /* @__PURE__ */ new Set(["84532", "421614", "11155111", "80001", "97"]);
278
+ function isTestnetNetwork(network) {
279
+ if (network.startsWith("eip155:")) {
280
+ const chainId = network.split(":")[1];
281
+ return TESTNET_CHAIN_IDS.has(chainId);
282
+ }
283
+ if (network.startsWith("solana:")) {
284
+ const ref = network.split(":")[1];
285
+ return ref === SOLANA_NETWORK_REFS.DEVNET;
286
+ }
287
+ if (network.startsWith("ton:")) {
288
+ const ref = network.split(":")[1];
289
+ return ref === TON_NETWORK_REFS.TESTNET;
290
+ }
291
+ if (network.startsWith("tron:")) {
292
+ const ref = network.split(":")[1];
293
+ return ref === TRON_NETWORK_REFS.NILE || ref === TRON_NETWORK_REFS.SHASTA;
294
+ }
295
+ return false;
296
+ }
297
+ function truncateAddress(address, startChars = 6, endChars = 4) {
298
+ if (address.length <= startChars + endChars + 3) {
299
+ return address;
300
+ }
301
+ return `${address.slice(0, startChars)}...${address.slice(-endChars)}`;
302
+ }
303
+ function formatTokenAmount(amount, decimals = 6, maxDecimals = 2) {
304
+ const value = BigInt(amount);
305
+ const divisor = BigInt(10 ** decimals);
306
+ const integerPart = value / divisor;
307
+ const fractionalPart = value % divisor;
308
+ if (fractionalPart === 0n) {
309
+ return integerPart.toString();
310
+ }
311
+ const fractionalStr = fractionalPart.toString().padStart(decimals, "0");
312
+ const trimmed = fractionalStr.slice(0, maxDecimals).replace(/0+$/, "");
313
+ if (trimmed === "") {
314
+ return integerPart.toString();
315
+ }
316
+ return `${integerPart}.${trimmed}`;
317
+ }
318
+ function getAssetDisplayName(asset) {
319
+ const assetLower = asset.toLowerCase();
320
+ switch (assetLower) {
321
+ case "usdt":
322
+ return "USDT";
323
+ case "usdt0":
324
+ return "USDT0";
325
+ case "usdc":
326
+ return "USDC";
327
+ default:
328
+ return asset.toUpperCase();
329
+ }
330
+ }
331
+ var containerStyles = {
332
+ backgroundColor: "#f9fafb",
333
+ borderRadius: "12px",
334
+ padding: "16px",
335
+ border: "1px solid #e5e7eb"
336
+ };
337
+ var rowStyles = {
338
+ display: "flex",
339
+ justifyContent: "space-between",
340
+ alignItems: "center",
341
+ padding: "8px 0",
342
+ borderBottom: "1px solid #e5e7eb"
343
+ };
344
+ var lastRowStyles = {
345
+ ...rowStyles,
346
+ borderBottom: "none"
347
+ };
348
+ var labelStyles = {
349
+ color: "#6b7280",
350
+ fontSize: "14px"
351
+ };
352
+ var valueStyles = {
353
+ color: "#111827",
354
+ fontSize: "14px",
355
+ fontWeight: 500
356
+ };
357
+ var amountStyles = {
358
+ color: "#111827",
359
+ fontSize: "20px",
360
+ fontWeight: 700
361
+ };
362
+ function PaymentDetails({
363
+ requirement,
364
+ showNetwork = true,
365
+ showAsset = true,
366
+ showRecipient = false,
367
+ className = ""
368
+ }) {
369
+ const formattedAmount = formatTokenAmount(requirement.amount);
370
+ const assetName = getAssetDisplayName(requirement.asset);
371
+ const networkName = getNetworkDisplayName(requirement.network);
372
+ const truncatedAddress = truncateAddress(requirement.payTo);
373
+ return /* @__PURE__ */ jsxRuntime.jsxs("div", { style: containerStyles, className, children: [
374
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { style: rowStyles, children: [
375
+ /* @__PURE__ */ jsxRuntime.jsx("span", { style: labelStyles, children: "Amount" }),
376
+ /* @__PURE__ */ jsxRuntime.jsxs("span", { style: amountStyles, children: [
377
+ formattedAmount,
378
+ " ",
379
+ assetName
380
+ ] })
381
+ ] }),
382
+ showNetwork && /* @__PURE__ */ jsxRuntime.jsxs("div", { style: rowStyles, children: [
383
+ /* @__PURE__ */ jsxRuntime.jsx("span", { style: labelStyles, children: "Network" }),
384
+ /* @__PURE__ */ jsxRuntime.jsx("span", { style: valueStyles, children: networkName })
385
+ ] }),
386
+ showAsset && /* @__PURE__ */ jsxRuntime.jsxs("div", { style: rowStyles, children: [
387
+ /* @__PURE__ */ jsxRuntime.jsx("span", { style: labelStyles, children: "Asset" }),
388
+ /* @__PURE__ */ jsxRuntime.jsx("span", { style: valueStyles, children: assetName })
389
+ ] }),
390
+ showRecipient && /* @__PURE__ */ jsxRuntime.jsxs("div", { style: lastRowStyles, children: [
391
+ /* @__PURE__ */ jsxRuntime.jsx("span", { style: labelStyles, children: "Recipient" }),
392
+ /* @__PURE__ */ jsxRuntime.jsx("span", { style: { ...valueStyles, fontFamily: "monospace" }, title: requirement.payTo, children: truncatedAddress })
393
+ ] })
394
+ ] });
395
+ }
396
+ var containerStyles2 = {
397
+ display: "inline-flex",
398
+ alignItems: "center",
399
+ gap: "8px",
400
+ fontFamily: "monospace",
401
+ fontSize: "14px"
402
+ };
403
+ var addressStyles = {
404
+ color: "#374151"
405
+ };
406
+ var buttonStyles = {
407
+ background: "none",
408
+ border: "none",
409
+ cursor: "pointer",
410
+ padding: "4px",
411
+ borderRadius: "4px",
412
+ color: "#6b7280",
413
+ transition: "all 0.15s ease",
414
+ display: "inline-flex",
415
+ alignItems: "center",
416
+ justifyContent: "center"
417
+ };
418
+ var copiedStyles = {
419
+ ...buttonStyles,
420
+ color: "#22c55e"
421
+ };
422
+ function AddressDisplay({
423
+ address,
424
+ startChars = 6,
425
+ endChars = 4,
426
+ copyable = false,
427
+ className = ""
428
+ }) {
429
+ const [copied, setCopied] = react.useState(false);
430
+ const displayAddress = truncateAddress(address, startChars, endChars);
431
+ const handleCopy = react.useCallback(async () => {
432
+ try {
433
+ await navigator.clipboard.writeText(address);
434
+ setCopied(true);
435
+ setTimeout(() => setCopied(false), 2e3);
436
+ } catch {
437
+ const textArea = document.createElement("textarea");
438
+ textArea.value = address;
439
+ document.body.appendChild(textArea);
440
+ textArea.select();
441
+ document.execCommand("copy");
442
+ document.body.removeChild(textArea);
443
+ setCopied(true);
444
+ setTimeout(() => setCopied(false), 2e3);
445
+ }
446
+ }, [address]);
447
+ return /* @__PURE__ */ jsxRuntime.jsxs("span", { style: containerStyles2, className, title: address, children: [
448
+ /* @__PURE__ */ jsxRuntime.jsx("span", { style: addressStyles, children: displayAddress }),
449
+ copyable && /* @__PURE__ */ jsxRuntime.jsx(
450
+ "button",
451
+ {
452
+ type: "button",
453
+ onClick: handleCopy,
454
+ style: copied ? copiedStyles : buttonStyles,
455
+ "aria-label": copied ? "Copied" : "Copy address",
456
+ children: copied ? /* @__PURE__ */ jsxRuntime.jsx("svg", { width: "16", height: "16", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "2", children: /* @__PURE__ */ jsxRuntime.jsx("polyline", { points: "20 6 9 17 4 12" }) }) : /* @__PURE__ */ jsxRuntime.jsxs("svg", { width: "16", height: "16", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "2", children: [
457
+ /* @__PURE__ */ jsxRuntime.jsx("rect", { x: "9", y: "9", width: "13", height: "13", rx: "2", ry: "2" }),
458
+ /* @__PURE__ */ jsxRuntime.jsx("path", { d: "M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1" })
459
+ ] })
460
+ }
461
+ )
462
+ ] });
463
+ }
464
+ function usePaymentRequired(options = {}) {
465
+ const { onSuccess, onError } = options;
466
+ const [paymentRequired, setPaymentRequired] = react.useState(null);
467
+ const [status, setStatus] = react.useState("idle");
468
+ const [error, setError] = react.useState(null);
469
+ const fetchResource = react.useCallback(
470
+ async (url, fetchOptions) => {
471
+ setStatus("loading");
472
+ setError(null);
473
+ setPaymentRequired(null);
474
+ try {
475
+ const response = await fetch(url, fetchOptions);
476
+ if (response.status === 402) {
477
+ const data = await response.json();
478
+ setPaymentRequired(data);
479
+ setStatus("idle");
480
+ return null;
481
+ }
482
+ if (response.ok) {
483
+ setStatus("success");
484
+ onSuccess?.(response);
485
+ return response;
486
+ }
487
+ throw new Error(`Request failed with status ${response.status}`);
488
+ } catch (err) {
489
+ const errorMessage = err instanceof Error ? err.message : "Unknown error";
490
+ setError(errorMessage);
491
+ setStatus("error");
492
+ onError?.(err instanceof Error ? err : new Error(errorMessage));
493
+ return null;
494
+ }
495
+ },
496
+ [onSuccess, onError]
497
+ );
498
+ const reset = react.useCallback(() => {
499
+ setPaymentRequired(null);
500
+ setStatus("idle");
501
+ setError(null);
502
+ }, []);
503
+ return {
504
+ paymentRequired,
505
+ status,
506
+ error,
507
+ fetchResource,
508
+ reset
509
+ };
510
+ }
511
+ function usePaymentStatus() {
512
+ const [status, setStatusState] = react.useState("idle");
513
+ const [message, setMessage] = react.useState(null);
514
+ const scheduleMessageClear = react.useCallback((timeout) => {
515
+ if (timeout && timeout > 0) {
516
+ setTimeout(() => {
517
+ setMessage(null);
518
+ }, timeout);
519
+ }
520
+ }, []);
521
+ const setStatus = react.useCallback(
522
+ (newStatus, messageText) => {
523
+ setStatusState(newStatus);
524
+ if (messageText) {
525
+ const type = newStatus === "error" ? "error" : newStatus === "success" ? "success" : "info";
526
+ setMessage({ text: messageText, type });
527
+ }
528
+ },
529
+ []
530
+ );
531
+ const setSuccess = react.useCallback(
532
+ (text, timeout) => {
533
+ setStatusState("success");
534
+ setMessage({ text, type: "success", timeout });
535
+ scheduleMessageClear(timeout);
536
+ },
537
+ [scheduleMessageClear]
538
+ );
539
+ const setError = react.useCallback((text) => {
540
+ setStatusState("error");
541
+ setMessage({ text, type: "error" });
542
+ }, []);
543
+ const setInfo = react.useCallback(
544
+ (text, timeout) => {
545
+ setMessage({ text, type: "info", timeout });
546
+ scheduleMessageClear(timeout);
547
+ },
548
+ [scheduleMessageClear]
549
+ );
550
+ const setWarning = react.useCallback(
551
+ (text, timeout) => {
552
+ setMessage({ text, type: "warning", timeout });
553
+ scheduleMessageClear(timeout);
554
+ },
555
+ [scheduleMessageClear]
556
+ );
557
+ const clearMessage = react.useCallback(() => {
558
+ setMessage(null);
559
+ }, []);
560
+ const reset = react.useCallback(() => {
561
+ setStatusState("idle");
562
+ setMessage(null);
563
+ }, []);
564
+ return {
565
+ status,
566
+ message,
567
+ setStatus,
568
+ setSuccess,
569
+ setError,
570
+ setInfo,
571
+ setWarning,
572
+ clearMessage,
573
+ reset
574
+ };
575
+ }
576
+ function useAsyncPayment(options) {
577
+ const { paymentFn, onSuccess, onError, onStart } = options;
578
+ const [status, setStatus] = react.useState("idle");
579
+ const [result, setResult] = react.useState(null);
580
+ const [error, setError] = react.useState(null);
581
+ const isMountedRef = react.useRef(true);
582
+ const execute = react.useCallback(async () => {
583
+ setStatus("loading");
584
+ setError(null);
585
+ onStart?.();
586
+ try {
587
+ const paymentResult = await paymentFn();
588
+ if (isMountedRef.current) {
589
+ setResult(paymentResult);
590
+ setStatus("success");
591
+ onSuccess?.(paymentResult);
592
+ }
593
+ return paymentResult;
594
+ } catch (err) {
595
+ const errorMessage = err instanceof Error ? err.message : "Payment failed";
596
+ if (isMountedRef.current) {
597
+ setError(errorMessage);
598
+ setStatus("error");
599
+ }
600
+ onError?.(err instanceof Error ? err : new Error(errorMessage));
601
+ return null;
602
+ }
603
+ }, [paymentFn, onSuccess, onError, onStart]);
604
+ const reset = react.useCallback(() => {
605
+ setStatus("idle");
606
+ setResult(null);
607
+ setError(null);
608
+ }, []);
609
+ return {
610
+ execute,
611
+ status,
612
+ result,
613
+ error,
614
+ isLoading: status === "loading",
615
+ isSuccess: status === "success",
616
+ isError: status === "error",
617
+ reset
618
+ };
619
+ }
620
+ var initialState = {
621
+ status: "idle",
622
+ error: null,
623
+ paymentRequired: null,
624
+ selectedRequirement: null,
625
+ isTestnet: false
626
+ };
627
+ var PaymentContext = react.createContext(null);
628
+ function PaymentProvider({
629
+ children,
630
+ initialPaymentRequired,
631
+ testnet = false
632
+ }) {
633
+ const [state, setState] = react.useState(() => {
634
+ if (initialPaymentRequired) {
635
+ const selectedRequirement = choosePaymentRequirement(
636
+ initialPaymentRequired.accepts,
637
+ testnet
638
+ );
639
+ const detectedTestnet = isTestnetNetwork(selectedRequirement.network);
640
+ return {
641
+ status: "idle",
642
+ error: null,
643
+ paymentRequired: initialPaymentRequired,
644
+ selectedRequirement,
645
+ isTestnet: testnet || detectedTestnet
646
+ };
647
+ }
648
+ return { ...initialState, isTestnet: testnet };
649
+ });
650
+ const setPaymentRequired = react.useCallback(
651
+ (data) => {
652
+ const selectedRequirement = choosePaymentRequirement(data.accepts, state.isTestnet);
653
+ const detectedTestnet = isTestnetNetwork(selectedRequirement.network);
654
+ setState((prev) => ({
655
+ ...prev,
656
+ paymentRequired: data,
657
+ selectedRequirement,
658
+ isTestnet: prev.isTestnet || detectedTestnet,
659
+ error: null
660
+ }));
661
+ },
662
+ [state.isTestnet]
663
+ );
664
+ const selectRequirement = react.useCallback((requirement) => {
665
+ setState((prev) => ({
666
+ ...prev,
667
+ selectedRequirement: requirement,
668
+ isTestnet: isTestnetNetwork(requirement.network)
669
+ }));
670
+ }, []);
671
+ const setStatus = react.useCallback((status) => {
672
+ setState((prev) => ({
673
+ ...prev,
674
+ status,
675
+ error: status === "error" ? prev.error : null
676
+ }));
677
+ }, []);
678
+ const setError = react.useCallback((error) => {
679
+ setState((prev) => ({
680
+ ...prev,
681
+ error,
682
+ status: error ? "error" : prev.status
683
+ }));
684
+ }, []);
685
+ const reset = react.useCallback(() => {
686
+ setState({ ...initialState, isTestnet: testnet });
687
+ }, [testnet]);
688
+ const value = react.useMemo(
689
+ () => ({
690
+ ...state,
691
+ setPaymentRequired,
692
+ selectRequirement,
693
+ setStatus,
694
+ setError,
695
+ reset
696
+ }),
697
+ [state, setPaymentRequired, selectRequirement, setStatus, setError, reset]
698
+ );
699
+ return /* @__PURE__ */ jsxRuntime.jsx(PaymentContext.Provider, { value, children });
700
+ }
701
+ function usePaymentContext() {
702
+ const context = react.useContext(PaymentContext);
703
+ if (!context) {
704
+ throw new Error("usePaymentContext must be used within a PaymentProvider");
705
+ }
706
+ return context;
707
+ }
708
+
709
+ exports.AddressDisplay = AddressDisplay;
710
+ exports.EVM_CHAIN_IDS = EVM_CHAIN_IDS;
711
+ exports.PaymentButton = PaymentButton;
712
+ exports.PaymentContext = PaymentContext;
713
+ exports.PaymentDetails = PaymentDetails;
714
+ exports.PaymentProvider = PaymentProvider;
715
+ exports.PaymentStatusDisplay = PaymentStatus;
716
+ exports.SOLANA_NETWORK_REFS = SOLANA_NETWORK_REFS;
717
+ exports.Spinner = Spinner;
718
+ exports.TON_NETWORK_REFS = TON_NETWORK_REFS;
719
+ exports.TRON_NETWORK_REFS = TRON_NETWORK_REFS;
720
+ exports.choosePaymentRequirement = choosePaymentRequirement;
721
+ exports.formatTokenAmount = formatTokenAmount;
722
+ exports.getAssetDisplayName = getAssetDisplayName;
723
+ exports.getNetworkDisplayName = getNetworkDisplayName;
724
+ exports.getPreferredNetworks = getPreferredNetworks;
725
+ exports.isEvmNetwork = isEvmNetwork;
726
+ exports.isSvmNetwork = isSvmNetwork;
727
+ exports.isTestnetNetwork = isTestnetNetwork;
728
+ exports.isTonNetwork = isTonNetwork;
729
+ exports.isTronNetwork = isTronNetwork;
730
+ exports.normalizePaymentRequirements = normalizePaymentRequirements;
731
+ exports.truncateAddress = truncateAddress;
732
+ exports.useAsyncPayment = useAsyncPayment;
733
+ exports.usePaymentContext = usePaymentContext;
734
+ exports.usePaymentRequired = usePaymentRequired;
735
+ exports.usePaymentStatus = usePaymentStatus;
736
+ //# sourceMappingURL=index.cjs.map
737
+ //# sourceMappingURL=index.cjs.map