@nairaramp/react 0.0.0-stage → 0.2.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.
package/dist/index.cjs ADDED
@@ -0,0 +1,1370 @@
1
+ "use client";
2
+ "use strict";
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+
21
+ // src/index.ts
22
+ var index_exports = {};
23
+ __export(index_exports, {
24
+ CashOutToNaira: () => CashOutToNaira,
25
+ ERROR_MESSAGES: () => ERROR_MESSAGES,
26
+ HAUSA_LABELS: () => HAUSA_LABELS,
27
+ IGBO_LABELS: () => IGBO_LABELS,
28
+ NairaRampProvider: () => NairaRampProvider,
29
+ PIDGIN_LABELS: () => PIDGIN_LABELS,
30
+ PayWithNaira: () => PayWithNaira,
31
+ STATUS_MAP: () => STATUS_MAP,
32
+ StatusTimeline: () => StatusTimeline,
33
+ TIMELINE_STEPS: () => TIMELINE_STEPS,
34
+ VERSION: () => VERSION,
35
+ YORUBA_LABELS: () => YORUBA_LABELS,
36
+ getErrorMessage: () => getErrorMessage,
37
+ getStellarExplorerUrl: () => getStellarExplorerUrl,
38
+ useAnchors: () => useAnchors,
39
+ useCashOutToNaira: () => useCashOutToNaira,
40
+ useNairaRamp: () => useNairaRamp,
41
+ usePayWithNaira: () => usePayWithNaira,
42
+ useRampQuote: () => useRampQuote
43
+ });
44
+ module.exports = __toCommonJS(index_exports);
45
+
46
+ // src/NairaRampContext.tsx
47
+ var import_react = require("react");
48
+ var import_jsx_runtime = require("react/jsx-runtime");
49
+ var NairaRampContext = (0, import_react.createContext)(void 0);
50
+ function NairaRampProvider({
51
+ ramp,
52
+ children
53
+ }) {
54
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(NairaRampContext.Provider, { value: ramp, children });
55
+ }
56
+ function useNairaRamp(overrideRamp) {
57
+ const contextRamp = (0, import_react.useContext)(NairaRampContext);
58
+ const resolved = overrideRamp ?? contextRamp;
59
+ if (!resolved) {
60
+ throw new Error(
61
+ "No Ramp instance found. Pass a `ramp` prop/option or wrap your component tree in <NairaRampProvider>."
62
+ );
63
+ }
64
+ return resolved;
65
+ }
66
+
67
+ // src/usePayWithNaira.ts
68
+ var import_react2 = require("react");
69
+ var import_core2 = require("@nairaramp/core");
70
+
71
+ // src/utils.ts
72
+ var import_core = require("@nairaramp/core");
73
+ function ensureNairaRampError(err) {
74
+ if ((0, import_core.isNairaRampError)(err)) {
75
+ return err;
76
+ }
77
+ const message = err instanceof Error ? err.message : String(err);
78
+ return new import_core.NairaRampError("TRANSACTION_FAILED", message, { cause: err });
79
+ }
80
+ function isSafeHttpUrl(rawUrl) {
81
+ try {
82
+ const parsed = new URL(rawUrl);
83
+ return parsed.protocol === "https:" || parsed.protocol === "http:";
84
+ } catch {
85
+ return false;
86
+ }
87
+ }
88
+ function getStellarExplorerUrl(txHash, network = "testnet") {
89
+ if (!txHash || !/^[0-9a-fA-F]{64}$/.test(txHash)) {
90
+ return void 0;
91
+ }
92
+ const segment = network === "mainnet" ? "public" : "testnet";
93
+ return `https://stellar.expert/explorer/${segment}/tx/${txHash}`;
94
+ }
95
+
96
+ // src/usePayWithNaira.ts
97
+ function usePayWithNaira({
98
+ ramp: rampOption,
99
+ assetCode
100
+ }) {
101
+ const ramp = useNairaRamp(rampOption);
102
+ const [phase, setPhase] = (0, import_react2.useState)("idle");
103
+ const [status, setStatus] = (0, import_react2.useState)(void 0);
104
+ const [transactionId, setTransactionId] = (0, import_react2.useState)(void 0);
105
+ const [transaction, setTransaction] = (0, import_react2.useState)(void 0);
106
+ const [stellarTransactionId, setStellarTransactionId] = (0, import_react2.useState)(void 0);
107
+ const [url, setUrl] = (0, import_react2.useState)(void 0);
108
+ const [error, setError] = (0, import_react2.useState)(void 0);
109
+ const phaseRef = (0, import_react2.useRef)("idle");
110
+ const subscriptionRef = (0, import_react2.useRef)(void 0);
111
+ const isMountedRef = (0, import_react2.useRef)(true);
112
+ const attemptIdRef = (0, import_react2.useRef)(0);
113
+ (0, import_react2.useEffect)(() => {
114
+ phaseRef.current = phase;
115
+ }, [phase]);
116
+ const stopWatcher = (0, import_react2.useCallback)(() => {
117
+ if (subscriptionRef.current) {
118
+ subscriptionRef.current.stop();
119
+ subscriptionRef.current = void 0;
120
+ }
121
+ }, []);
122
+ const reset = (0, import_react2.useCallback)(() => {
123
+ attemptIdRef.current += 1;
124
+ stopWatcher();
125
+ phaseRef.current = "idle";
126
+ if (isMountedRef.current) {
127
+ setPhase("idle");
128
+ setStatus(void 0);
129
+ setTransactionId(void 0);
130
+ setTransaction(void 0);
131
+ setStellarTransactionId(void 0);
132
+ setUrl(void 0);
133
+ setError(void 0);
134
+ }
135
+ }, [stopWatcher]);
136
+ const start = (0, import_react2.useCallback)(
137
+ async (amount) => {
138
+ if (phaseRef.current !== "idle") {
139
+ return void 0;
140
+ }
141
+ const attemptId = ++attemptIdRef.current;
142
+ phaseRef.current = "starting";
143
+ if (isMountedRef.current) {
144
+ setPhase("starting");
145
+ setTransaction(void 0);
146
+ setStellarTransactionId(void 0);
147
+ setError(void 0);
148
+ }
149
+ let depositResult;
150
+ try {
151
+ depositResult = await ramp.deposit({
152
+ assetCode,
153
+ ...amount !== void 0 ? { amount } : {}
154
+ });
155
+ } catch (err) {
156
+ if (!isMountedRef.current || attemptId !== attemptIdRef.current) {
157
+ return void 0;
158
+ }
159
+ const wrapped = ensureNairaRampError(err);
160
+ phaseRef.current = "failed";
161
+ setError(wrapped);
162
+ setPhase("failed");
163
+ return void 0;
164
+ }
165
+ if (!isMountedRef.current || attemptId !== attemptIdRef.current) {
166
+ return void 0;
167
+ }
168
+ const { id, url: hostedUrl } = depositResult;
169
+ setTransactionId(id);
170
+ setUrl(hostedUrl);
171
+ phaseRef.current = "awaiting_user";
172
+ setPhase("awaiting_user");
173
+ stopWatcher();
174
+ const subscription = ramp.watch(id, {
175
+ onStatus: (nextStatus, tx) => {
176
+ if (!isMountedRef.current || attemptId !== attemptIdRef.current) return;
177
+ setStatus(nextStatus);
178
+ if (tx) {
179
+ setTransaction(tx);
180
+ if (tx.stellar_transaction_id) {
181
+ setStellarTransactionId(tx.stellar_transaction_id);
182
+ }
183
+ }
184
+ if (nextStatus === "completed") {
185
+ stopWatcher();
186
+ phaseRef.current = "completed";
187
+ setPhase("completed");
188
+ } else if (import_core2.TERMINAL_STATUSES.includes(nextStatus)) {
189
+ stopWatcher();
190
+ const detail = tx?.message ? `Transaction ${nextStatus}: ${tx.message}` : `Transaction ${nextStatus}`;
191
+ setError(new import_core2.NairaRampError("TRANSACTION_FAILED", detail));
192
+ phaseRef.current = "failed";
193
+ setPhase("failed");
194
+ } else if (nextStatus === "pending_user_transfer_start" || nextStatus === "incomplete") {
195
+ phaseRef.current = "awaiting_user";
196
+ setPhase("awaiting_user");
197
+ } else {
198
+ phaseRef.current = "processing";
199
+ setPhase("processing");
200
+ }
201
+ },
202
+ onComplete: (tx) => {
203
+ if (!isMountedRef.current || attemptId !== attemptIdRef.current) return;
204
+ stopWatcher();
205
+ if (tx) {
206
+ setTransaction(tx);
207
+ if (tx.stellar_transaction_id) {
208
+ setStellarTransactionId(tx.stellar_transaction_id);
209
+ }
210
+ }
211
+ setStatus("completed");
212
+ phaseRef.current = "completed";
213
+ setPhase("completed");
214
+ },
215
+ onError: (err) => {
216
+ if (!isMountedRef.current || attemptId !== attemptIdRef.current) return;
217
+ stopWatcher();
218
+ const wrapped = ensureNairaRampError(err);
219
+ setError(wrapped);
220
+ phaseRef.current = "failed";
221
+ setPhase("failed");
222
+ }
223
+ });
224
+ subscriptionRef.current = subscription;
225
+ return depositResult;
226
+ },
227
+ [ramp, assetCode, stopWatcher]
228
+ );
229
+ (0, import_react2.useEffect)(() => {
230
+ isMountedRef.current = true;
231
+ return () => {
232
+ isMountedRef.current = false;
233
+ attemptIdRef.current += 1;
234
+ stopWatcher();
235
+ };
236
+ }, [stopWatcher]);
237
+ return {
238
+ start,
239
+ phase,
240
+ status,
241
+ transactionId,
242
+ transaction,
243
+ stellarTransactionId,
244
+ url,
245
+ error,
246
+ reset
247
+ };
248
+ }
249
+
250
+ // src/PayWithNaira.tsx
251
+ var import_react5 = require("react");
252
+ var import_core3 = require("@nairaramp/core");
253
+
254
+ // src/theme.ts
255
+ var import_react3 = require("react");
256
+ var LIGHT_THEME = {
257
+ text: "#111827",
258
+ secondaryText: "#1f2937",
259
+ mutedText: "#4b5563",
260
+ pendingText: "#6b7280",
261
+ doneText: "#059669",
262
+ primaryBtnBg: "#059669",
263
+ primaryBtnText: "#ffffff",
264
+ disabledBtnBg: "#6b7280",
265
+ disabledBtnText: "#ffffff",
266
+ retryBtnBg: "#f3f4f6",
267
+ retryBtnText: "#1f2937",
268
+ retryBtnBorder: "#d1d5db",
269
+ errorText: "#b91c1c",
270
+ linkText: "#2563eb",
271
+ failureBg: "#fef2f2",
272
+ failureBorder: "#fee2e2",
273
+ failureTitle: "#991b1b",
274
+ activeBadgeBg: "#2563eb",
275
+ doneBadgeBg: "#059669",
276
+ activeOrDoneBadgeText: "#ffffff",
277
+ pendingBadgeBg: "#e5e7eb",
278
+ pendingBadgeText: "#374151",
279
+ focusPrimary: "#047857",
280
+ focusAccent: "#2563eb"
281
+ };
282
+ var DARK_THEME = {
283
+ text: "#f9fafb",
284
+ secondaryText: "#e5e7eb",
285
+ mutedText: "#d1d5db",
286
+ pendingText: "#9ca3af",
287
+ doneText: "#34d399",
288
+ primaryBtnBg: "#059669",
289
+ primaryBtnText: "#ffffff",
290
+ disabledBtnBg: "#4b5563",
291
+ disabledBtnText: "#ffffff",
292
+ retryBtnBg: "#1f2937",
293
+ retryBtnText: "#f9fafb",
294
+ retryBtnBorder: "#374151",
295
+ errorText: "#fca5a5",
296
+ linkText: "#60a5fa",
297
+ failureBg: "#450a0a",
298
+ failureBorder: "#7f1d1d",
299
+ failureTitle: "#fecaca",
300
+ activeBadgeBg: "#3b82f6",
301
+ doneBadgeBg: "#059669",
302
+ activeOrDoneBadgeText: "#ffffff",
303
+ pendingBadgeBg: "#374151",
304
+ pendingBadgeText: "#e5e7eb",
305
+ focusPrimary: "#34d399",
306
+ focusAccent: "#60a5fa"
307
+ };
308
+ function getSystemDarkPreference() {
309
+ if (typeof window === "undefined" || typeof window.matchMedia !== "function") {
310
+ return false;
311
+ }
312
+ return window.matchMedia("(prefers-color-scheme: dark)").matches;
313
+ }
314
+ function useResolvedTheme(theme = "system") {
315
+ const [systemDark, setSystemDark] = (0, import_react3.useState)(
316
+ () => theme === "system" ? getSystemDarkPreference() : false
317
+ );
318
+ (0, import_react3.useEffect)(() => {
319
+ if (theme !== "system" || typeof window === "undefined" || typeof window.matchMedia !== "function") {
320
+ return;
321
+ }
322
+ const mediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
323
+ setSystemDark(mediaQuery.matches);
324
+ const handleChange = (event) => {
325
+ setSystemDark(event.matches);
326
+ };
327
+ mediaQuery.addEventListener("change", handleChange);
328
+ return () => {
329
+ mediaQuery.removeEventListener("change", handleChange);
330
+ };
331
+ }, [theme]);
332
+ if (theme === "dark") return DARK_THEME;
333
+ if (theme === "light") return LIGHT_THEME;
334
+ return systemDark ? DARK_THEME : LIGHT_THEME;
335
+ }
336
+
337
+ // src/useAnchorPopup.ts
338
+ var import_react4 = require("react");
339
+ var rampContainerStyle = {
340
+ width: "100%",
341
+ maxWidth: "420px",
342
+ boxSizing: "border-box",
343
+ fontFamily: "system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif",
344
+ fontSize: "16px",
345
+ lineHeight: 1.5,
346
+ color: "#111827",
347
+ textAlign: "center"
348
+ };
349
+ var baseButtonStyle = {
350
+ width: "100%",
351
+ boxSizing: "border-box",
352
+ padding: "12px 16px",
353
+ fontSize: "16px",
354
+ fontWeight: 600,
355
+ borderRadius: "8px",
356
+ border: "1px solid transparent",
357
+ cursor: "pointer",
358
+ textAlign: "center",
359
+ display: "inline-block",
360
+ transition: "outline 0.15s ease"
361
+ };
362
+ var rampPrimaryButtonStyle = {
363
+ ...baseButtonStyle,
364
+ color: "#ffffff",
365
+ backgroundColor: "#059669"
366
+ };
367
+ var rampDisabledButtonStyle = {
368
+ ...baseButtonStyle,
369
+ color: "#ffffff",
370
+ backgroundColor: "#9ca3af",
371
+ cursor: "not-allowed",
372
+ opacity: 0.8
373
+ };
374
+ var rampRetryButtonStyle = {
375
+ ...baseButtonStyle,
376
+ marginTop: "12px",
377
+ padding: "10px 16px",
378
+ fontSize: "15px",
379
+ color: "#1f2937",
380
+ backgroundColor: "#f3f4f6",
381
+ border: "1px solid #d1d5db"
382
+ };
383
+ var rampStatusTextStyle = {
384
+ margin: "8px 0",
385
+ fontSize: "16px",
386
+ fontWeight: 500,
387
+ color: "#1f2937"
388
+ };
389
+ var rampErrorTextStyle = {
390
+ margin: "8px 0",
391
+ fontSize: "15px",
392
+ fontWeight: 500,
393
+ color: "#b91c1c",
394
+ wordBreak: "break-word"
395
+ };
396
+ var rampLinkStyle = {
397
+ display: "inline-block",
398
+ fontSize: "15px",
399
+ fontWeight: 600,
400
+ color: "#2563eb",
401
+ textDecoration: "underline",
402
+ wordBreak: "break-all",
403
+ cursor: "pointer"
404
+ };
405
+ function useAnchorPopup() {
406
+ const [popupBlocked, setPopupBlocked] = (0, import_react4.useState)(false);
407
+ const popupRef = (0, import_react4.useRef)(null);
408
+ const closePopup = (0, import_react4.useCallback)(() => {
409
+ if (popupRef.current && !popupRef.current.closed) {
410
+ try {
411
+ popupRef.current.close();
412
+ } catch {
413
+ }
414
+ popupRef.current = null;
415
+ }
416
+ }, []);
417
+ const openPopup = (0, import_react4.useCallback)(() => {
418
+ setPopupBlocked(false);
419
+ let popup = null;
420
+ if (typeof window !== "undefined" && typeof window.open === "function") {
421
+ try {
422
+ popup = window.open("", "nairaramp_anchor");
423
+ } catch {
424
+ popup = null;
425
+ }
426
+ }
427
+ popupRef.current = popup;
428
+ if (!popup) {
429
+ setPopupBlocked(true);
430
+ }
431
+ return popup;
432
+ }, []);
433
+ const navigatePopup = (0, import_react4.useCallback)(
434
+ (popup, targetUrl) => {
435
+ if (targetUrl && isSafeHttpUrl(targetUrl)) {
436
+ if (popup && !popup.closed) {
437
+ popup.location.href = targetUrl;
438
+ }
439
+ } else {
440
+ closePopup();
441
+ }
442
+ },
443
+ [closePopup]
444
+ );
445
+ const clearPopupBlocked = (0, import_react4.useCallback)(() => {
446
+ setPopupBlocked(false);
447
+ }, []);
448
+ (0, import_react4.useEffect)(() => {
449
+ return () => {
450
+ closePopup();
451
+ };
452
+ }, [closePopup]);
453
+ return {
454
+ popupBlocked,
455
+ openPopup,
456
+ navigatePopup,
457
+ closePopup,
458
+ clearPopupBlocked
459
+ };
460
+ }
461
+
462
+ // src/PayWithNaira.tsx
463
+ var import_jsx_runtime2 = require("react/jsx-runtime");
464
+ function PayWithNaira({
465
+ ramp,
466
+ assetCode,
467
+ amount,
468
+ onComplete,
469
+ onError,
470
+ label = "Pay with Naira",
471
+ theme = "system",
472
+ className,
473
+ style
474
+ }) {
475
+ const tokens = useResolvedTheme(theme);
476
+ const { start, phase, error, url, reset } = usePayWithNaira({
477
+ ramp,
478
+ assetCode
479
+ });
480
+ const {
481
+ popupBlocked,
482
+ openPopup,
483
+ navigatePopup,
484
+ closePopup,
485
+ clearPopupBlocked
486
+ } = useAnchorPopup();
487
+ const [isPrimaryFocused, setIsPrimaryFocused] = (0, import_react5.useState)(false);
488
+ const [isRetryFocused, setIsRetryFocused] = (0, import_react5.useState)(false);
489
+ const [isLinkFocused, setIsLinkFocused] = (0, import_react5.useState)(false);
490
+ const onCompleteRef = (0, import_react5.useRef)(onComplete);
491
+ const onErrorRef = (0, import_react5.useRef)(onError);
492
+ const completedFiredRef = (0, import_react5.useRef)(false);
493
+ const errorFiredRef = (0, import_react5.useRef)(false);
494
+ (0, import_react5.useEffect)(() => {
495
+ onCompleteRef.current = onComplete;
496
+ }, [onComplete]);
497
+ (0, import_react5.useEffect)(() => {
498
+ onErrorRef.current = onError;
499
+ }, [onError]);
500
+ const handleClick = (0, import_react5.useCallback)(async () => {
501
+ const popup = openPopup();
502
+ const result = await start(amount);
503
+ navigatePopup(popup, result?.url);
504
+ }, [openPopup, start, amount, navigatePopup]);
505
+ const handleRetry = (0, import_react5.useCallback)(() => {
506
+ clearPopupBlocked();
507
+ closePopup();
508
+ completedFiredRef.current = false;
509
+ errorFiredRef.current = false;
510
+ reset();
511
+ }, [clearPopupBlocked, closePopup, reset]);
512
+ (0, import_react5.useEffect)(() => {
513
+ if (phase === "completed") {
514
+ if (!completedFiredRef.current) {
515
+ completedFiredRef.current = true;
516
+ onCompleteRef.current?.();
517
+ }
518
+ } else if (phase === "failed") {
519
+ closePopup();
520
+ if (!errorFiredRef.current) {
521
+ errorFiredRef.current = true;
522
+ const finalError = error ?? new import_core3.NairaRampError("TRANSACTION_FAILED", "Payment failed");
523
+ onErrorRef.current?.(finalError);
524
+ }
525
+ } else if (phase === "idle") {
526
+ completedFiredRef.current = false;
527
+ errorFiredRef.current = false;
528
+ }
529
+ }, [phase, error, closePopup]);
530
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className, style: { ...rampContainerStyle, color: tokens.text, ...style }, children: [
531
+ phase === "idle" && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
532
+ "button",
533
+ {
534
+ type: "button",
535
+ onClick: handleClick,
536
+ onFocus: () => setIsPrimaryFocused(true),
537
+ onBlur: () => setIsPrimaryFocused(false),
538
+ style: {
539
+ ...rampPrimaryButtonStyle,
540
+ color: tokens.primaryBtnText,
541
+ backgroundColor: tokens.primaryBtnBg,
542
+ outline: isPrimaryFocused ? `3px solid ${tokens.focusPrimary}` : "2px solid transparent",
543
+ outlineOffset: "2px"
544
+ },
545
+ children: label
546
+ }
547
+ ),
548
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { "aria-live": "polite", "aria-atomic": "true", children: [
549
+ phase === "starting" && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
550
+ "button",
551
+ {
552
+ type: "button",
553
+ disabled: true,
554
+ style: {
555
+ ...rampDisabledButtonStyle,
556
+ color: tokens.disabledBtnText,
557
+ backgroundColor: tokens.disabledBtnBg
558
+ },
559
+ children: "Connecting..."
560
+ }
561
+ ),
562
+ phase === "awaiting_user" && /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { children: [
563
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("p", { style: { ...rampStatusTextStyle, color: tokens.secondaryText }, children: "Complete the steps in the anchor window" }),
564
+ popupBlocked && url && isSafeHttpUrl(url) && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("p", { style: { margin: "8px 0 0 0" }, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
565
+ "a",
566
+ {
567
+ href: url,
568
+ target: "_blank",
569
+ rel: "noopener noreferrer",
570
+ onFocus: () => setIsLinkFocused(true),
571
+ onBlur: () => setIsLinkFocused(false),
572
+ style: {
573
+ ...rampLinkStyle,
574
+ color: tokens.linkText,
575
+ outline: isLinkFocused ? `2px solid ${tokens.focusAccent}` : "2px solid transparent",
576
+ outlineOffset: "2px"
577
+ },
578
+ children: "Continue to the anchor"
579
+ }
580
+ ) })
581
+ ] }),
582
+ phase === "processing" && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("p", { style: { ...rampStatusTextStyle, color: tokens.secondaryText }, children: "Processing your payment" }),
583
+ phase === "completed" && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("p", { style: { ...rampStatusTextStyle, color: tokens.secondaryText }, children: "Payment completed" }),
584
+ phase === "failed" && /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { children: [
585
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("p", { style: { ...rampErrorTextStyle, color: tokens.errorText }, children: error?.message ?? "Payment failed" }),
586
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
587
+ "button",
588
+ {
589
+ type: "button",
590
+ onClick: handleRetry,
591
+ onFocus: () => setIsRetryFocused(true),
592
+ onBlur: () => setIsRetryFocused(false),
593
+ style: {
594
+ ...rampRetryButtonStyle,
595
+ color: tokens.retryBtnText,
596
+ backgroundColor: tokens.retryBtnBg,
597
+ border: `1px solid ${tokens.retryBtnBorder}`,
598
+ outline: isRetryFocused ? `2px solid ${tokens.focusAccent}` : "2px solid transparent",
599
+ outlineOffset: "2px"
600
+ },
601
+ children: "Try again"
602
+ }
603
+ )
604
+ ] })
605
+ ] })
606
+ ] });
607
+ }
608
+
609
+ // src/useCashOutToNaira.ts
610
+ var import_react6 = require("react");
611
+ var import_core4 = require("@nairaramp/core");
612
+ function useCashOutToNaira({
613
+ ramp: rampOption,
614
+ assetCode
615
+ }) {
616
+ const ramp = useNairaRamp(rampOption);
617
+ const [phase, setPhase] = (0, import_react6.useState)("idle");
618
+ const [status, setStatus] = (0, import_react6.useState)(void 0);
619
+ const [transactionId, setTransactionId] = (0, import_react6.useState)(void 0);
620
+ const [transaction, setTransaction] = (0, import_react6.useState)(void 0);
621
+ const [url, setUrl] = (0, import_react6.useState)(void 0);
622
+ const [txHash, setTxHash] = (0, import_react6.useState)(void 0);
623
+ const [error, setError] = (0, import_react6.useState)(void 0);
624
+ const phaseRef = (0, import_react6.useRef)("idle");
625
+ const subscriptionRef = (0, import_react6.useRef)(void 0);
626
+ const isMountedRef = (0, import_react6.useRef)(true);
627
+ const completionStartedRef = (0, import_react6.useRef)(false);
628
+ const attemptIdRef = (0, import_react6.useRef)(0);
629
+ (0, import_react6.useEffect)(() => {
630
+ phaseRef.current = phase;
631
+ }, [phase]);
632
+ const stopWatcher = (0, import_react6.useCallback)(() => {
633
+ if (subscriptionRef.current) {
634
+ subscriptionRef.current.stop();
635
+ subscriptionRef.current = void 0;
636
+ }
637
+ }, []);
638
+ const reset = (0, import_react6.useCallback)(() => {
639
+ attemptIdRef.current += 1;
640
+ stopWatcher();
641
+ phaseRef.current = "idle";
642
+ completionStartedRef.current = false;
643
+ if (isMountedRef.current) {
644
+ setPhase("idle");
645
+ setStatus(void 0);
646
+ setTransactionId(void 0);
647
+ setTransaction(void 0);
648
+ setUrl(void 0);
649
+ setTxHash(void 0);
650
+ setError(void 0);
651
+ }
652
+ }, [stopWatcher]);
653
+ const start = (0, import_react6.useCallback)(
654
+ async (amount) => {
655
+ if (phaseRef.current !== "idle") {
656
+ return void 0;
657
+ }
658
+ const attemptId = ++attemptIdRef.current;
659
+ completionStartedRef.current = false;
660
+ phaseRef.current = "starting";
661
+ if (isMountedRef.current) {
662
+ setPhase("starting");
663
+ setTransaction(void 0);
664
+ setError(void 0);
665
+ setTxHash(void 0);
666
+ }
667
+ let withdrawResult;
668
+ try {
669
+ withdrawResult = await ramp.withdraw({
670
+ assetCode,
671
+ ...amount !== void 0 ? { amount } : {}
672
+ });
673
+ } catch (err) {
674
+ if (!isMountedRef.current || attemptId !== attemptIdRef.current) {
675
+ return void 0;
676
+ }
677
+ const wrapped = ensureNairaRampError(err);
678
+ phaseRef.current = "failed";
679
+ setError(wrapped);
680
+ setPhase("failed");
681
+ return void 0;
682
+ }
683
+ if (!isMountedRef.current || attemptId !== attemptIdRef.current) {
684
+ return void 0;
685
+ }
686
+ const { id, url: hostedUrl } = withdrawResult;
687
+ setTransactionId(id);
688
+ setUrl(hostedUrl);
689
+ phaseRef.current = "awaiting_user";
690
+ setPhase("awaiting_user");
691
+ stopWatcher();
692
+ const subscription = ramp.watch(id, {
693
+ onStatus: (nextStatus, tx) => {
694
+ if (!isMountedRef.current || attemptId !== attemptIdRef.current) return;
695
+ setStatus(nextStatus);
696
+ if (tx) {
697
+ setTransaction(tx);
698
+ if (tx.stellar_transaction_id) {
699
+ setTxHash((prev) => prev ?? tx.stellar_transaction_id);
700
+ }
701
+ }
702
+ if (nextStatus === "completed") {
703
+ stopWatcher();
704
+ phaseRef.current = "completed";
705
+ setPhase("completed");
706
+ return;
707
+ }
708
+ if (import_core4.TERMINAL_STATUSES.includes(nextStatus)) {
709
+ stopWatcher();
710
+ const detail = tx?.message ? `Transaction ${nextStatus}: ${tx.message}` : `Transaction ${nextStatus}`;
711
+ setError(new import_core4.NairaRampError("TRANSACTION_FAILED", detail));
712
+ phaseRef.current = "failed";
713
+ setPhase("failed");
714
+ return;
715
+ }
716
+ if (nextStatus === "pending_user_transfer_start") {
717
+ if (!completionStartedRef.current) {
718
+ completionStartedRef.current = true;
719
+ phaseRef.current = "completing";
720
+ setPhase("completing");
721
+ ramp.completeWithdrawal(id).then(({ hash }) => {
722
+ if (!isMountedRef.current || attemptId !== attemptIdRef.current) return;
723
+ setTxHash(hash);
724
+ if (phaseRef.current === "completing") {
725
+ phaseRef.current = "processing";
726
+ setPhase("processing");
727
+ }
728
+ }).catch((err) => {
729
+ if (!isMountedRef.current || attemptId !== attemptIdRef.current) return;
730
+ stopWatcher();
731
+ setError(ensureNairaRampError(err));
732
+ phaseRef.current = "failed";
733
+ setPhase("failed");
734
+ });
735
+ }
736
+ return;
737
+ }
738
+ if (nextStatus === "incomplete" || nextStatus === "pending_user") {
739
+ phaseRef.current = "awaiting_user";
740
+ setPhase("awaiting_user");
741
+ } else {
742
+ phaseRef.current = "processing";
743
+ setPhase("processing");
744
+ }
745
+ },
746
+ onComplete: (tx) => {
747
+ if (!isMountedRef.current || attemptId !== attemptIdRef.current) return;
748
+ stopWatcher();
749
+ if (tx) {
750
+ setTransaction(tx);
751
+ if (tx.stellar_transaction_id) {
752
+ setTxHash((prev) => prev ?? tx.stellar_transaction_id);
753
+ }
754
+ }
755
+ setStatus("completed");
756
+ phaseRef.current = "completed";
757
+ setPhase("completed");
758
+ },
759
+ onError: (err) => {
760
+ if (!isMountedRef.current || attemptId !== attemptIdRef.current) return;
761
+ stopWatcher();
762
+ setError(ensureNairaRampError(err));
763
+ phaseRef.current = "failed";
764
+ setPhase("failed");
765
+ }
766
+ });
767
+ subscriptionRef.current = subscription;
768
+ return withdrawResult;
769
+ },
770
+ [ramp, assetCode, stopWatcher]
771
+ );
772
+ (0, import_react6.useEffect)(() => {
773
+ isMountedRef.current = true;
774
+ return () => {
775
+ isMountedRef.current = false;
776
+ attemptIdRef.current += 1;
777
+ stopWatcher();
778
+ };
779
+ }, [stopWatcher]);
780
+ return {
781
+ start,
782
+ phase,
783
+ status,
784
+ transactionId,
785
+ transaction,
786
+ url,
787
+ txHash,
788
+ error,
789
+ reset
790
+ };
791
+ }
792
+
793
+ // src/CashOutToNaira.tsx
794
+ var import_react7 = require("react");
795
+ var import_core5 = require("@nairaramp/core");
796
+ var import_jsx_runtime3 = require("react/jsx-runtime");
797
+ function CashOutToNaira({
798
+ ramp,
799
+ assetCode,
800
+ amount,
801
+ onComplete,
802
+ onError,
803
+ label = "Cash out to Naira",
804
+ theme = "system",
805
+ className,
806
+ style
807
+ }) {
808
+ const tokens = useResolvedTheme(theme);
809
+ const { start, phase, error, url, txHash, reset } = useCashOutToNaira({
810
+ ramp,
811
+ assetCode
812
+ });
813
+ const {
814
+ popupBlocked,
815
+ openPopup,
816
+ navigatePopup,
817
+ closePopup,
818
+ clearPopupBlocked
819
+ } = useAnchorPopup();
820
+ const [isPrimaryFocused, setIsPrimaryFocused] = (0, import_react7.useState)(false);
821
+ const [isRetryFocused, setIsRetryFocused] = (0, import_react7.useState)(false);
822
+ const [isLinkFocused, setIsLinkFocused] = (0, import_react7.useState)(false);
823
+ const onCompleteRef = (0, import_react7.useRef)(onComplete);
824
+ const onErrorRef = (0, import_react7.useRef)(onError);
825
+ const completedFiredRef = (0, import_react7.useRef)(false);
826
+ const errorFiredRef = (0, import_react7.useRef)(false);
827
+ (0, import_react7.useEffect)(() => {
828
+ onCompleteRef.current = onComplete;
829
+ }, [onComplete]);
830
+ (0, import_react7.useEffect)(() => {
831
+ onErrorRef.current = onError;
832
+ }, [onError]);
833
+ const handleClick = (0, import_react7.useCallback)(async () => {
834
+ const popup = openPopup();
835
+ const result = await start(amount);
836
+ navigatePopup(popup, result?.url);
837
+ }, [openPopup, start, amount, navigatePopup]);
838
+ const handleRetry = (0, import_react7.useCallback)(() => {
839
+ clearPopupBlocked();
840
+ closePopup();
841
+ completedFiredRef.current = false;
842
+ errorFiredRef.current = false;
843
+ reset();
844
+ }, [clearPopupBlocked, closePopup, reset]);
845
+ (0, import_react7.useEffect)(() => {
846
+ if (phase === "completed") {
847
+ if (!completedFiredRef.current) {
848
+ completedFiredRef.current = true;
849
+ onCompleteRef.current?.(txHash);
850
+ }
851
+ } else if (phase === "failed") {
852
+ closePopup();
853
+ if (!errorFiredRef.current) {
854
+ errorFiredRef.current = true;
855
+ const finalError = error ?? new import_core5.NairaRampError("TRANSACTION_FAILED", "Cash out failed");
856
+ onErrorRef.current?.(finalError);
857
+ }
858
+ } else if (phase === "idle") {
859
+ completedFiredRef.current = false;
860
+ errorFiredRef.current = false;
861
+ }
862
+ }, [phase, error, txHash, closePopup]);
863
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { className, style: { ...rampContainerStyle, color: tokens.text, ...style }, children: [
864
+ phase === "idle" && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
865
+ "button",
866
+ {
867
+ type: "button",
868
+ onClick: handleClick,
869
+ onFocus: () => setIsPrimaryFocused(true),
870
+ onBlur: () => setIsPrimaryFocused(false),
871
+ style: {
872
+ ...rampPrimaryButtonStyle,
873
+ color: tokens.primaryBtnText,
874
+ backgroundColor: tokens.primaryBtnBg,
875
+ outline: isPrimaryFocused ? `3px solid ${tokens.focusPrimary}` : "2px solid transparent",
876
+ outlineOffset: "2px"
877
+ },
878
+ children: label
879
+ }
880
+ ),
881
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { "aria-live": "polite", "aria-atomic": "true", children: [
882
+ phase === "starting" && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
883
+ "button",
884
+ {
885
+ type: "button",
886
+ disabled: true,
887
+ style: {
888
+ ...rampDisabledButtonStyle,
889
+ color: tokens.disabledBtnText,
890
+ backgroundColor: tokens.disabledBtnBg
891
+ },
892
+ children: "Connecting..."
893
+ }
894
+ ),
895
+ phase === "awaiting_user" && /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { children: [
896
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("p", { style: { ...rampStatusTextStyle, color: tokens.secondaryText }, children: "Complete the steps in the anchor window" }),
897
+ popupBlocked && url && isSafeHttpUrl(url) && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("p", { style: { margin: "8px 0 0 0" }, children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
898
+ "a",
899
+ {
900
+ href: url,
901
+ target: "_blank",
902
+ rel: "noopener noreferrer",
903
+ onFocus: () => setIsLinkFocused(true),
904
+ onBlur: () => setIsLinkFocused(false),
905
+ style: {
906
+ ...rampLinkStyle,
907
+ color: tokens.linkText,
908
+ outline: isLinkFocused ? `2px solid ${tokens.focusAccent}` : "2px solid transparent",
909
+ outlineOffset: "2px"
910
+ },
911
+ children: "Continue to the anchor"
912
+ }
913
+ ) })
914
+ ] }),
915
+ phase === "completing" && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("p", { style: { ...rampStatusTextStyle, color: tokens.secondaryText }, children: "Submitting Stellar transfer..." }),
916
+ phase === "processing" && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("p", { style: { ...rampStatusTextStyle, color: tokens.secondaryText }, children: "Processing your cash out" }),
917
+ phase === "completed" && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("p", { style: { ...rampStatusTextStyle, color: tokens.secondaryText }, children: "Cash out completed" }),
918
+ phase === "failed" && /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { children: [
919
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("p", { style: { ...rampErrorTextStyle, color: tokens.errorText }, children: error?.message ?? "Cash out failed" }),
920
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
921
+ "button",
922
+ {
923
+ type: "button",
924
+ onClick: handleRetry,
925
+ onFocus: () => setIsRetryFocused(true),
926
+ onBlur: () => setIsRetryFocused(false),
927
+ style: {
928
+ ...rampRetryButtonStyle,
929
+ color: tokens.retryBtnText,
930
+ backgroundColor: tokens.retryBtnBg,
931
+ border: `1px solid ${tokens.retryBtnBorder}`,
932
+ outline: isRetryFocused ? `2px solid ${tokens.focusAccent}` : "2px solid transparent",
933
+ outlineOffset: "2px"
934
+ },
935
+ children: "Try again"
936
+ }
937
+ )
938
+ ] })
939
+ ] })
940
+ ] });
941
+ }
942
+
943
+ // src/StatusTimeline.tsx
944
+ var import_jsx_runtime4 = require("react/jsx-runtime");
945
+ var TIMELINE_STEPS = [
946
+ "Started",
947
+ "Waiting for your payment",
948
+ "Payment received",
949
+ "Anchor processing",
950
+ "Sending on Stellar",
951
+ "Completed"
952
+ ];
953
+ var YORUBA_LABELS = {
954
+ Started: "B\u1EB9r\u1EB9",
955
+ "Waiting for your payment": "Nduro de owo r\u1EB9",
956
+ "Payment received": "A ti gba owo",
957
+ "Anchor processing": "Anchor n \u1E63i\u1E63\u1EB9 lori r\u1EB9",
958
+ "Sending on Stellar": "Nfi ran\u1E63\u1EB9 lori Stellar",
959
+ Completed: "Pari"
960
+ };
961
+ var HAUSA_LABELS = {
962
+ Started: "An fara",
963
+ "Waiting for your payment": "Ana jiran biyan ku",
964
+ "Payment received": "An kar\u0253i biya",
965
+ "Anchor processing": "Anchor yana sarrafawa",
966
+ "Sending on Stellar": "Ana aikawa ta Stellar",
967
+ Completed: "An kammala"
968
+ };
969
+ var IGBO_LABELS = {
970
+ Started: "Amalitela",
971
+ "Waiting for your payment": "Na-eche ugwo gi",
972
+ "Payment received": "Anatala ugwo",
973
+ "Anchor processing": "Anchor na-ar\u1EE5 \u1ECDr\u1EE5",
974
+ "Sending on Stellar": "Na-eziga na Stellar",
975
+ Completed: "Emechala"
976
+ };
977
+ var PIDGIN_LABELS = {
978
+ Started: "Don start",
979
+ "Waiting for your payment": "Dey wait for your money",
980
+ "Payment received": "We don collect the money",
981
+ "Anchor processing": "Anchor dey process am",
982
+ "Sending on Stellar": "Dey send am on Stellar",
983
+ Completed: "Don finish"
984
+ };
985
+ var STATUS_MAP = {
986
+ incomplete: {
987
+ kind: "step",
988
+ stepIndex: 0,
989
+ label: "Started"
990
+ },
991
+ pending_user_transfer_start: {
992
+ kind: "step",
993
+ stepIndex: 1,
994
+ label: "Waiting for your payment"
995
+ },
996
+ pending_user_transfer_complete: {
997
+ kind: "step",
998
+ stepIndex: 2,
999
+ label: "Payment received"
1000
+ },
1001
+ pending_external: {
1002
+ kind: "step",
1003
+ stepIndex: 2,
1004
+ label: "Payment received"
1005
+ },
1006
+ pending_anchor: {
1007
+ kind: "step",
1008
+ stepIndex: 3,
1009
+ label: "Anchor processing"
1010
+ },
1011
+ pending_stellar: {
1012
+ kind: "step",
1013
+ stepIndex: 4,
1014
+ label: "Sending on Stellar"
1015
+ },
1016
+ pending_trust: {
1017
+ kind: "step",
1018
+ stepIndex: 4,
1019
+ label: "Sending on Stellar"
1020
+ },
1021
+ pending_user: {
1022
+ kind: "step",
1023
+ stepIndex: 1,
1024
+ label: "Waiting for your payment"
1025
+ },
1026
+ completed: {
1027
+ kind: "step",
1028
+ stepIndex: 5,
1029
+ label: "Completed"
1030
+ },
1031
+ refunded: {
1032
+ kind: "failure",
1033
+ label: "Refunded"
1034
+ },
1035
+ expired: {
1036
+ kind: "failure",
1037
+ label: "Expired"
1038
+ },
1039
+ no_market: {
1040
+ kind: "failure",
1041
+ label: "No market"
1042
+ },
1043
+ too_small: {
1044
+ kind: "failure",
1045
+ label: "Too small"
1046
+ },
1047
+ too_large: {
1048
+ kind: "failure",
1049
+ label: "Too large"
1050
+ },
1051
+ error: {
1052
+ kind: "failure",
1053
+ label: "Error"
1054
+ }
1055
+ };
1056
+ var containerStyle = {
1057
+ width: "100%",
1058
+ maxWidth: "420px",
1059
+ boxSizing: "border-box",
1060
+ fontFamily: "system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif",
1061
+ fontSize: "15px",
1062
+ lineHeight: 1.5,
1063
+ color: "#111827"
1064
+ };
1065
+ var listStyle = {
1066
+ listStyle: "none",
1067
+ padding: 0,
1068
+ margin: 0,
1069
+ display: "flex",
1070
+ flexDirection: "column",
1071
+ gap: "12px"
1072
+ };
1073
+ var failureContainerStyle = {
1074
+ ...containerStyle,
1075
+ padding: "16px",
1076
+ borderRadius: "8px",
1077
+ backgroundColor: "#fef2f2",
1078
+ border: "1px solid #fee2e2",
1079
+ textAlign: "center"
1080
+ };
1081
+ var failureTitleStyle = {
1082
+ margin: 0,
1083
+ fontSize: "16px",
1084
+ fontWeight: 600,
1085
+ color: "#991b1b"
1086
+ };
1087
+ var messageTextStyle = {
1088
+ margin: "6px 0 0 0",
1089
+ fontSize: "14px",
1090
+ color: "#4b5563"
1091
+ };
1092
+ function resolveStatusMapping(status) {
1093
+ if (status === void 0) {
1094
+ return {
1095
+ kind: "step",
1096
+ stepIndex: 0,
1097
+ label: "Started"
1098
+ };
1099
+ }
1100
+ if (Object.prototype.hasOwnProperty.call(STATUS_MAP, status)) {
1101
+ return STATUS_MAP[status];
1102
+ }
1103
+ return {
1104
+ kind: "failure",
1105
+ label: "Unknown status"
1106
+ };
1107
+ }
1108
+ function StatusTimeline({
1109
+ status,
1110
+ message,
1111
+ labels,
1112
+ failureLabels,
1113
+ theme = "system",
1114
+ className,
1115
+ style
1116
+ }) {
1117
+ const tokens = useResolvedTheme(theme);
1118
+ const mapping = resolveStatusMapping(status);
1119
+ if (mapping.kind === "failure") {
1120
+ const failureText = failureLabels?.[mapping.label] ?? mapping.label;
1121
+ return /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(
1122
+ "div",
1123
+ {
1124
+ className,
1125
+ style: {
1126
+ ...failureContainerStyle,
1127
+ color: tokens.text,
1128
+ backgroundColor: tokens.failureBg,
1129
+ border: `1px solid ${tokens.failureBorder}`,
1130
+ ...style
1131
+ },
1132
+ role: "alert",
1133
+ children: [
1134
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("p", { style: { ...failureTitleStyle, color: tokens.failureTitle }, children: failureText }),
1135
+ message && /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("p", { style: { ...messageTextStyle, color: tokens.mutedText }, children: message })
1136
+ ]
1137
+ }
1138
+ );
1139
+ }
1140
+ const activeIndex = mapping.stepIndex;
1141
+ return /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("div", { className, style: { ...containerStyle, color: tokens.text, ...style }, children: [
1142
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("ol", { style: listStyle, "aria-label": "Payment status timeline", children: TIMELINE_STEPS.map((step, index) => {
1143
+ const isDone = index < activeIndex;
1144
+ const isActive = index === activeIndex;
1145
+ const displayLabel = labels?.[step] ?? step;
1146
+ const itemStyle = {
1147
+ display: "flex",
1148
+ alignItems: "center",
1149
+ gap: "12px",
1150
+ color: isActive ? tokens.text : isDone ? tokens.doneText : tokens.pendingText,
1151
+ fontWeight: isActive ? 600 : isDone ? 500 : 400
1152
+ };
1153
+ const badgeStyle = {
1154
+ width: "24px",
1155
+ height: "24px",
1156
+ borderRadius: "50%",
1157
+ display: "inline-flex",
1158
+ alignItems: "center",
1159
+ justifyContent: "center",
1160
+ fontSize: "12px",
1161
+ fontWeight: 700,
1162
+ boxSizing: "border-box",
1163
+ backgroundColor: isActive ? tokens.activeBadgeBg : isDone ? tokens.doneBadgeBg : tokens.pendingBadgeBg,
1164
+ color: isActive || isDone ? tokens.activeOrDoneBadgeText : tokens.pendingBadgeText
1165
+ };
1166
+ return /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(
1167
+ "li",
1168
+ {
1169
+ "aria-current": isActive ? "step" : void 0,
1170
+ style: itemStyle,
1171
+ children: [
1172
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("span", { style: badgeStyle, "aria-hidden": "true", children: isDone ? "\u2713" : index + 1 }),
1173
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("span", { children: displayLabel })
1174
+ ]
1175
+ },
1176
+ step
1177
+ );
1178
+ }) }),
1179
+ message && /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("p", { style: { ...messageTextStyle, color: tokens.mutedText }, children: message })
1180
+ ] });
1181
+ }
1182
+
1183
+ // src/useRampQuote.ts
1184
+ var import_react8 = require("react");
1185
+ var import_core6 = require("@nairaramp/core");
1186
+ function toQuoteError(err) {
1187
+ if ((0, import_core6.isNairaRampError)(err)) {
1188
+ return err;
1189
+ }
1190
+ const message = err instanceof Error ? err.message : String(err);
1191
+ return new import_core6.NairaRampError("QUOTE_UNAVAILABLE", message, { cause: err });
1192
+ }
1193
+ function useRampQuote({
1194
+ ramp: rampOption,
1195
+ sellAsset,
1196
+ buyAsset,
1197
+ sellAmount,
1198
+ buyAmount,
1199
+ context,
1200
+ debounceMs = 300,
1201
+ pollIntervalMs,
1202
+ enabled = true
1203
+ }) {
1204
+ const ramp = useNairaRamp(rampOption);
1205
+ const [quote, setQuote] = (0, import_react8.useState)(void 0);
1206
+ const [loading, setLoading] = (0, import_react8.useState)(false);
1207
+ const [error, setError] = (0, import_react8.useState)(void 0);
1208
+ const isMountedRef = (0, import_react8.useRef)(true);
1209
+ const requestIdRef = (0, import_react8.useRef)(0);
1210
+ const hasAmount = sellAmount !== void 0 && sellAmount.trim() !== "" || buyAmount !== void 0 && buyAmount.trim() !== "";
1211
+ const fetchQuote = (0, import_react8.useCallback)(async () => {
1212
+ if (!enabled || !hasAmount) {
1213
+ return;
1214
+ }
1215
+ const reqId = ++requestIdRef.current;
1216
+ if (isMountedRef.current) {
1217
+ setLoading(true);
1218
+ setError(void 0);
1219
+ }
1220
+ const options = {
1221
+ sellAsset,
1222
+ buyAsset,
1223
+ ...sellAmount !== void 0 && sellAmount.trim() !== "" ? { sellAmount } : {},
1224
+ ...buyAmount !== void 0 && buyAmount.trim() !== "" ? { buyAmount } : {},
1225
+ ...context !== void 0 ? { context } : {}
1226
+ };
1227
+ try {
1228
+ const nextQuote = await ramp.getPrice(options);
1229
+ if (!isMountedRef.current || reqId !== requestIdRef.current) {
1230
+ return;
1231
+ }
1232
+ setQuote(nextQuote);
1233
+ setError(void 0);
1234
+ } catch (err) {
1235
+ if (!isMountedRef.current || reqId !== requestIdRef.current) {
1236
+ return;
1237
+ }
1238
+ setError(toQuoteError(err));
1239
+ } finally {
1240
+ if (isMountedRef.current && reqId === requestIdRef.current) {
1241
+ setLoading(false);
1242
+ }
1243
+ }
1244
+ }, [ramp, sellAsset, buyAsset, sellAmount, buyAmount, context, enabled, hasAmount]);
1245
+ (0, import_react8.useEffect)(() => {
1246
+ isMountedRef.current = true;
1247
+ return () => {
1248
+ isMountedRef.current = false;
1249
+ };
1250
+ }, []);
1251
+ (0, import_react8.useEffect)(() => {
1252
+ if (!enabled || !hasAmount) {
1253
+ requestIdRef.current++;
1254
+ setQuote(void 0);
1255
+ setError(void 0);
1256
+ setLoading(false);
1257
+ return;
1258
+ }
1259
+ const delay = Math.max(0, debounceMs);
1260
+ const debounceTimer = setTimeout(() => {
1261
+ void fetchQuote();
1262
+ }, delay);
1263
+ return () => {
1264
+ clearTimeout(debounceTimer);
1265
+ };
1266
+ }, [enabled, hasAmount, debounceMs, fetchQuote]);
1267
+ (0, import_react8.useEffect)(() => {
1268
+ if (!enabled || !hasAmount || !pollIntervalMs || pollIntervalMs <= 0) {
1269
+ return;
1270
+ }
1271
+ const interval = setInterval(() => {
1272
+ void fetchQuote();
1273
+ }, pollIntervalMs);
1274
+ return () => {
1275
+ clearInterval(interval);
1276
+ };
1277
+ }, [enabled, hasAmount, pollIntervalMs, fetchQuote]);
1278
+ return {
1279
+ quote,
1280
+ loading,
1281
+ error,
1282
+ refresh: fetchQuote
1283
+ };
1284
+ }
1285
+
1286
+ // src/useAnchors.ts
1287
+ var import_react9 = require("react");
1288
+ var import_core7 = require("@nairaramp/core");
1289
+ function useAnchors(options = {}) {
1290
+ const { network, country, assetCode, sep } = options;
1291
+ const anchors = (0, import_react9.useMemo)(() => {
1292
+ const coreFilter = network !== void 0 || country !== void 0 ? {
1293
+ ...network !== void 0 ? { network } : {},
1294
+ ...country !== void 0 ? { country } : {}
1295
+ } : void 0;
1296
+ const baseAnchors = (0, import_core7.listAnchors)(coreFilter);
1297
+ return baseAnchors.filter((anchor) => {
1298
+ if (assetCode !== void 0 && !anchor.assets.some(
1299
+ (asset) => asset.code.toUpperCase() === assetCode.toUpperCase()
1300
+ )) {
1301
+ return false;
1302
+ }
1303
+ if (sep !== void 0 && !anchor.seps.includes(sep)) {
1304
+ return false;
1305
+ }
1306
+ return true;
1307
+ });
1308
+ }, [network, country, assetCode, sep]);
1309
+ const getAnchorById = (0, import_react9.useCallback)(
1310
+ (id) => anchors.find((anchor) => anchor.id === id),
1311
+ [anchors]
1312
+ );
1313
+ return { anchors, getAnchorById };
1314
+ }
1315
+
1316
+ // src/errorMessages.ts
1317
+ var ERROR_MESSAGES = {
1318
+ AUTH_FAILED: "Authentication with the anchor failed. Please try signing in again.",
1319
+ ANCHOR_UNREACHABLE: "Could not reach the anchor server. Check your connection and try again.",
1320
+ ANCHOR_NOT_FOUND: "The selected Stellar anchor is not recognized.",
1321
+ KYC_REQUIRED: "Identity verification (KYC) is required by the anchor before continuing.",
1322
+ QUOTE_EXPIRED: "Your exchange rate quote expired. Request a fresh quote to continue.",
1323
+ QUOTE_UNAVAILABLE: "Exchange rate quote is currently unavailable for this pair or amount.",
1324
+ UNSUPPORTED_ASSET: "This Stellar asset is not supported by the selected anchor.",
1325
+ SIGNER_REJECTED: "Wallet signature was cancelled or rejected.",
1326
+ TRANSACTION_FAILED: "The transaction could not be completed. Please try again.",
1327
+ MAINNET_NOT_ALLOWED: "Mainnet transactions are disabled in the current configuration.",
1328
+ ANCHOR_NOT_VERIFIED: "This anchor's mainnet verification is outdated and cannot be used."
1329
+ };
1330
+ function getErrorMessage(error, overrides) {
1331
+ if (!error) {
1332
+ return void 0;
1333
+ }
1334
+ const code = error.code;
1335
+ if (overrides && Object.prototype.hasOwnProperty.call(overrides, code)) {
1336
+ const custom = overrides[code];
1337
+ if (custom !== void 0) {
1338
+ return custom;
1339
+ }
1340
+ }
1341
+ if (Object.prototype.hasOwnProperty.call(ERROR_MESSAGES, code)) {
1342
+ return ERROR_MESSAGES[code];
1343
+ }
1344
+ return error.message;
1345
+ }
1346
+
1347
+ // src/index.ts
1348
+ var VERSION = "0.2.0";
1349
+ // Annotate the CommonJS export names for ESM import in node:
1350
+ 0 && (module.exports = {
1351
+ CashOutToNaira,
1352
+ ERROR_MESSAGES,
1353
+ HAUSA_LABELS,
1354
+ IGBO_LABELS,
1355
+ NairaRampProvider,
1356
+ PIDGIN_LABELS,
1357
+ PayWithNaira,
1358
+ STATUS_MAP,
1359
+ StatusTimeline,
1360
+ TIMELINE_STEPS,
1361
+ VERSION,
1362
+ YORUBA_LABELS,
1363
+ getErrorMessage,
1364
+ getStellarExplorerUrl,
1365
+ useAnchors,
1366
+ useCashOutToNaira,
1367
+ useNairaRamp,
1368
+ usePayWithNaira,
1369
+ useRampQuote
1370
+ });