@cimplify/sdk 0.3.6 → 0.5.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,105 @@
1
+ import { en as AdSlot, eo as AdPosition, es as AdContextValue, r as CimplifyElements, C as CimplifyClient, v as ElementsOptions, eh as AuthenticatedData, ef as AddressInfo, eg as PaymentMethodInfo, ej as ElementsCheckoutResult } from './ads-BOUIrN73.mjs';
2
+ export { eq as AdConfig } from './ads-BOUIrN73.mjs';
3
+ import React, { ReactNode } from 'react';
4
+
5
+ interface UserIdentity {
6
+ uid: string;
7
+ isAuthenticated: boolean;
8
+ fingerprint: string;
9
+ signals: DeviceSignals;
10
+ }
11
+ interface DeviceSignals {
12
+ userAgent: string;
13
+ language: string;
14
+ languages: string[];
15
+ platform: string;
16
+ screenWidth: number;
17
+ screenHeight: number;
18
+ colorDepth: number;
19
+ pixelRatio: number;
20
+ timezone: string;
21
+ timezoneOffset: number;
22
+ touchSupport: boolean;
23
+ cookiesEnabled: boolean;
24
+ doNotTrack: boolean;
25
+ }
26
+
27
+ interface AdContextValueExtended extends AdContextValue {
28
+ identity: UserIdentity | null;
29
+ apiBase: string;
30
+ }
31
+ declare function useAds(): AdContextValueExtended;
32
+ interface AdProviderProps {
33
+ siteId: string;
34
+ apiBase?: string;
35
+ authenticatedAccountId?: string;
36
+ children: ReactNode;
37
+ }
38
+ declare function AdProvider({ siteId, apiBase, authenticatedAccountId, children, }: AdProviderProps): React.ReactElement;
39
+ interface AdProps {
40
+ slot: AdSlot;
41
+ position?: AdPosition;
42
+ className?: string;
43
+ style?: React.CSSProperties;
44
+ fallback?: ReactNode;
45
+ onImpression?: (adId: string) => void;
46
+ onClick?: (adId: string) => void;
47
+ }
48
+ declare function Ad({ slot, position, className, style, fallback, onImpression, onClick, }: AdProps): React.ReactElement | null;
49
+
50
+ declare function useElements(): CimplifyElements | null;
51
+ declare function useElementsReady(): boolean;
52
+ interface ElementsProviderProps {
53
+ client: CimplifyClient;
54
+ businessId: string;
55
+ options?: ElementsOptions;
56
+ children: ReactNode;
57
+ }
58
+ declare function ElementsProvider({ client, businessId, options, children, }: ElementsProviderProps): React.ReactElement;
59
+ interface BaseElementProps {
60
+ className?: string;
61
+ style?: React.CSSProperties;
62
+ onReady?: () => void;
63
+ onError?: (error: {
64
+ code: string;
65
+ message: string;
66
+ }) => void;
67
+ }
68
+ interface AuthElementProps extends BaseElementProps {
69
+ prefillEmail?: string;
70
+ onAuthenticated?: (data: AuthenticatedData) => void;
71
+ onRequiresOtp?: (data: {
72
+ contactMasked: string;
73
+ }) => void;
74
+ }
75
+ declare function AuthElement({ className, style, prefillEmail, onReady, onAuthenticated, onRequiresOtp, onError, }: AuthElementProps): React.ReactElement | null;
76
+ interface AddressElementProps extends BaseElementProps {
77
+ mode?: "shipping" | "billing";
78
+ onChange?: (data: {
79
+ address: AddressInfo;
80
+ saveToLink: boolean;
81
+ }) => void;
82
+ }
83
+ declare function AddressElement({ className, style, mode, onReady, onChange, onError, }: AddressElementProps): React.ReactElement | null;
84
+ interface PaymentElementProps extends BaseElementProps {
85
+ amount?: number;
86
+ currency?: string;
87
+ onChange?: (data: {
88
+ paymentMethod: PaymentMethodInfo;
89
+ saveToLink: boolean;
90
+ }) => void;
91
+ }
92
+ declare function PaymentElement({ className, style, amount, currency, onReady, onChange, onError, }: PaymentElementProps): React.ReactElement | null;
93
+ declare function useCheckout(): {
94
+ submit: (data: {
95
+ cart_id: string;
96
+ order_type?: "delivery" | "pickup" | "dine_in";
97
+ location_id?: string;
98
+ scheduled_time?: string;
99
+ tip_amount?: number;
100
+ notes?: string;
101
+ }) => Promise<ElementsCheckoutResult>;
102
+ isLoading: boolean;
103
+ };
104
+
105
+ export { Ad, AdPosition, type AdProps, AdProvider, AdSlot, AddressElement, AuthElement, ElementsProvider, PaymentElement, useAds, useCheckout, useElements, useElementsReady };
@@ -0,0 +1,105 @@
1
+ import { en as AdSlot, eo as AdPosition, es as AdContextValue, r as CimplifyElements, C as CimplifyClient, v as ElementsOptions, eh as AuthenticatedData, ef as AddressInfo, eg as PaymentMethodInfo, ej as ElementsCheckoutResult } from './ads-BOUIrN73.js';
2
+ export { eq as AdConfig } from './ads-BOUIrN73.js';
3
+ import React, { ReactNode } from 'react';
4
+
5
+ interface UserIdentity {
6
+ uid: string;
7
+ isAuthenticated: boolean;
8
+ fingerprint: string;
9
+ signals: DeviceSignals;
10
+ }
11
+ interface DeviceSignals {
12
+ userAgent: string;
13
+ language: string;
14
+ languages: string[];
15
+ platform: string;
16
+ screenWidth: number;
17
+ screenHeight: number;
18
+ colorDepth: number;
19
+ pixelRatio: number;
20
+ timezone: string;
21
+ timezoneOffset: number;
22
+ touchSupport: boolean;
23
+ cookiesEnabled: boolean;
24
+ doNotTrack: boolean;
25
+ }
26
+
27
+ interface AdContextValueExtended extends AdContextValue {
28
+ identity: UserIdentity | null;
29
+ apiBase: string;
30
+ }
31
+ declare function useAds(): AdContextValueExtended;
32
+ interface AdProviderProps {
33
+ siteId: string;
34
+ apiBase?: string;
35
+ authenticatedAccountId?: string;
36
+ children: ReactNode;
37
+ }
38
+ declare function AdProvider({ siteId, apiBase, authenticatedAccountId, children, }: AdProviderProps): React.ReactElement;
39
+ interface AdProps {
40
+ slot: AdSlot;
41
+ position?: AdPosition;
42
+ className?: string;
43
+ style?: React.CSSProperties;
44
+ fallback?: ReactNode;
45
+ onImpression?: (adId: string) => void;
46
+ onClick?: (adId: string) => void;
47
+ }
48
+ declare function Ad({ slot, position, className, style, fallback, onImpression, onClick, }: AdProps): React.ReactElement | null;
49
+
50
+ declare function useElements(): CimplifyElements | null;
51
+ declare function useElementsReady(): boolean;
52
+ interface ElementsProviderProps {
53
+ client: CimplifyClient;
54
+ businessId: string;
55
+ options?: ElementsOptions;
56
+ children: ReactNode;
57
+ }
58
+ declare function ElementsProvider({ client, businessId, options, children, }: ElementsProviderProps): React.ReactElement;
59
+ interface BaseElementProps {
60
+ className?: string;
61
+ style?: React.CSSProperties;
62
+ onReady?: () => void;
63
+ onError?: (error: {
64
+ code: string;
65
+ message: string;
66
+ }) => void;
67
+ }
68
+ interface AuthElementProps extends BaseElementProps {
69
+ prefillEmail?: string;
70
+ onAuthenticated?: (data: AuthenticatedData) => void;
71
+ onRequiresOtp?: (data: {
72
+ contactMasked: string;
73
+ }) => void;
74
+ }
75
+ declare function AuthElement({ className, style, prefillEmail, onReady, onAuthenticated, onRequiresOtp, onError, }: AuthElementProps): React.ReactElement | null;
76
+ interface AddressElementProps extends BaseElementProps {
77
+ mode?: "shipping" | "billing";
78
+ onChange?: (data: {
79
+ address: AddressInfo;
80
+ saveToLink: boolean;
81
+ }) => void;
82
+ }
83
+ declare function AddressElement({ className, style, mode, onReady, onChange, onError, }: AddressElementProps): React.ReactElement | null;
84
+ interface PaymentElementProps extends BaseElementProps {
85
+ amount?: number;
86
+ currency?: string;
87
+ onChange?: (data: {
88
+ paymentMethod: PaymentMethodInfo;
89
+ saveToLink: boolean;
90
+ }) => void;
91
+ }
92
+ declare function PaymentElement({ className, style, amount, currency, onReady, onChange, onError, }: PaymentElementProps): React.ReactElement | null;
93
+ declare function useCheckout(): {
94
+ submit: (data: {
95
+ cart_id: string;
96
+ order_type?: "delivery" | "pickup" | "dine_in";
97
+ location_id?: string;
98
+ scheduled_time?: string;
99
+ tip_amount?: number;
100
+ notes?: string;
101
+ }) => Promise<ElementsCheckoutResult>;
102
+ isLoading: boolean;
103
+ };
104
+
105
+ export { Ad, AdPosition, type AdProps, AdProvider, AdSlot, AddressElement, AuthElement, ElementsProvider, PaymentElement, useAds, useCheckout, useElements, useElementsReady };
package/dist/react.js ADDED
@@ -0,0 +1,447 @@
1
+ 'use strict';
2
+
3
+ var react = require('react');
4
+ var jsxRuntime = require('react/jsx-runtime');
5
+
6
+ // src/react/index.tsx
7
+
8
+ // src/types/elements.ts
9
+ var ELEMENT_TYPES = {
10
+ AUTH: "auth",
11
+ ADDRESS: "address",
12
+ PAYMENT: "payment"
13
+ };
14
+ var EVENT_TYPES = {
15
+ READY: "ready",
16
+ AUTHENTICATED: "authenticated",
17
+ REQUIRES_OTP: "requires_otp",
18
+ ERROR: "error",
19
+ CHANGE: "change"};
20
+
21
+ // src/ads/identity.ts
22
+ var COOKIE_NAME = "_cimplify_uid";
23
+ var COOKIE_MAX_AGE = 60 * 60 * 24 * 365;
24
+ function getOrCreateUserId() {
25
+ if (typeof document === "undefined") return generateId();
26
+ const existing = getCookie(COOKIE_NAME);
27
+ if (existing) return existing;
28
+ const newId = generateId();
29
+ setCookie(COOKIE_NAME, newId, COOKIE_MAX_AGE);
30
+ return newId;
31
+ }
32
+ function collectDeviceSignals() {
33
+ if (typeof window === "undefined") {
34
+ return getEmptySignals();
35
+ }
36
+ return {
37
+ userAgent: navigator.userAgent,
38
+ language: navigator.language,
39
+ languages: Array.from(navigator.languages || []),
40
+ platform: navigator.platform,
41
+ screenWidth: screen.width,
42
+ screenHeight: screen.height,
43
+ colorDepth: screen.colorDepth,
44
+ pixelRatio: window.devicePixelRatio || 1,
45
+ timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
46
+ timezoneOffset: (/* @__PURE__ */ new Date()).getTimezoneOffset(),
47
+ touchSupport: "ontouchstart" in window || navigator.maxTouchPoints > 0,
48
+ cookiesEnabled: navigator.cookieEnabled,
49
+ doNotTrack: navigator.doNotTrack === "1"
50
+ };
51
+ }
52
+ function generateFingerprint(signals) {
53
+ const components = [
54
+ signals.userAgent,
55
+ signals.language,
56
+ signals.platform,
57
+ `${signals.screenWidth}x${signals.screenHeight}`,
58
+ signals.colorDepth.toString(),
59
+ signals.pixelRatio.toString(),
60
+ signals.timezone,
61
+ signals.touchSupport.toString()
62
+ ];
63
+ return hashString(components.join("|"));
64
+ }
65
+ function getUserIdentity(authenticatedAccountId) {
66
+ const signals = collectDeviceSignals();
67
+ const fingerprint = generateFingerprint(signals);
68
+ const cookieId = getOrCreateUserId();
69
+ return {
70
+ uid: authenticatedAccountId || cookieId,
71
+ isAuthenticated: !!authenticatedAccountId,
72
+ fingerprint,
73
+ signals
74
+ };
75
+ }
76
+ function getDeviceType(signals) {
77
+ const ua = signals.userAgent.toLowerCase();
78
+ const width = signals.screenWidth;
79
+ if (/mobile|android|iphone|ipod/.test(ua) || width < 768) {
80
+ return "mobile";
81
+ }
82
+ if (/tablet|ipad/.test(ua) || width >= 768 && width < 1024) {
83
+ return "tablet";
84
+ }
85
+ return "desktop";
86
+ }
87
+ function getBrowserInfo(signals) {
88
+ const ua = signals.userAgent;
89
+ const browsers = [
90
+ { name: "Chrome", regex: /Chrome\/(\d+)/ },
91
+ { name: "Firefox", regex: /Firefox\/(\d+)/ },
92
+ { name: "Safari", regex: /Version\/(\d+).*Safari/ },
93
+ { name: "Edge", regex: /Edg\/(\d+)/ },
94
+ { name: "Opera", regex: /OPR\/(\d+)/ }
95
+ ];
96
+ for (const browser of browsers) {
97
+ const match = ua.match(browser.regex);
98
+ if (match) {
99
+ return { name: browser.name, version: match[1] };
100
+ }
101
+ }
102
+ return { name: "Unknown", version: "0" };
103
+ }
104
+ function getOSInfo(signals) {
105
+ const ua = signals.userAgent;
106
+ if (/Windows NT 10/.test(ua)) return { name: "Windows", version: "10" };
107
+ if (/Windows NT 6.3/.test(ua)) return { name: "Windows", version: "8.1" };
108
+ if (/Mac OS X (\d+[._]\d+)/.test(ua)) {
109
+ const match = ua.match(/Mac OS X (\d+[._]\d+)/);
110
+ return { name: "macOS", version: match?.[1].replace("_", ".") || "" };
111
+ }
112
+ if (/Android (\d+)/.test(ua)) {
113
+ const match = ua.match(/Android (\d+)/);
114
+ return { name: "Android", version: match?.[1] || "" };
115
+ }
116
+ if (/iPhone OS (\d+)/.test(ua)) {
117
+ const match = ua.match(/iPhone OS (\d+)/);
118
+ return { name: "iOS", version: match?.[1] || "" };
119
+ }
120
+ if (/Linux/.test(ua)) return { name: "Linux", version: "" };
121
+ return { name: "Unknown", version: "" };
122
+ }
123
+ function generateId() {
124
+ const timestamp = Date.now().toString(36);
125
+ const random = Math.random().toString(36).substring(2, 10);
126
+ return `${timestamp}-${random}`;
127
+ }
128
+ function hashString(str) {
129
+ let hash = 0;
130
+ for (let i = 0; i < str.length; i++) {
131
+ const char = str.charCodeAt(i);
132
+ hash = (hash << 5) - hash + char;
133
+ hash = hash & hash;
134
+ }
135
+ return Math.abs(hash).toString(36);
136
+ }
137
+ function getCookie(name) {
138
+ if (typeof document === "undefined") return null;
139
+ const match = document.cookie.match(new RegExp(`(^| )${name}=([^;]+)`));
140
+ return match ? match[2] : null;
141
+ }
142
+ function setCookie(name, value, maxAge) {
143
+ if (typeof document === "undefined") return;
144
+ document.cookie = `${name}=${value};max-age=${maxAge};path=/;SameSite=Lax`;
145
+ }
146
+ function getEmptySignals() {
147
+ return {
148
+ userAgent: "",
149
+ language: "",
150
+ languages: [],
151
+ platform: "",
152
+ screenWidth: 0,
153
+ screenHeight: 0,
154
+ colorDepth: 0,
155
+ pixelRatio: 1,
156
+ timezone: "",
157
+ timezoneOffset: 0,
158
+ touchSupport: false,
159
+ cookiesEnabled: false,
160
+ doNotTrack: false
161
+ };
162
+ }
163
+ function deriveAdsApiUrl() {
164
+ if (typeof window === "undefined") return "https://api.cimplify.io";
165
+ const hostname = window.location.hostname;
166
+ if (hostname === "localhost" || hostname === "127.0.0.1") {
167
+ return `${window.location.protocol}//${hostname}:8080`;
168
+ }
169
+ return "https://api.cimplify.io";
170
+ }
171
+ var AdContext = react.createContext({
172
+ siteId: null,
173
+ config: null,
174
+ isLoading: true,
175
+ identity: null,
176
+ apiBase: "https://api.cimplify.io"
177
+ });
178
+ function useAds() {
179
+ return react.useContext(AdContext);
180
+ }
181
+ function AdProvider({
182
+ siteId,
183
+ apiBase,
184
+ authenticatedAccountId,
185
+ children
186
+ }) {
187
+ const resolvedApiBase = apiBase || deriveAdsApiUrl();
188
+ const [config, setConfig] = react.useState(null);
189
+ const [isLoading, setIsLoading] = react.useState(true);
190
+ const [identity, setIdentity] = react.useState(null);
191
+ react.useEffect(() => {
192
+ const userIdentity = getUserIdentity(authenticatedAccountId);
193
+ setIdentity(userIdentity);
194
+ fetch(`${resolvedApiBase}/ads/config/${siteId}`).then((r) => r.json()).then((data) => {
195
+ setConfig(data);
196
+ setIsLoading(false);
197
+ }).catch(() => {
198
+ setConfig({ enabled: false, theme: "auto" });
199
+ setIsLoading(false);
200
+ });
201
+ }, [siteId, resolvedApiBase, authenticatedAccountId]);
202
+ return /* @__PURE__ */ jsxRuntime.jsx(AdContext.Provider, { value: { siteId, config, isLoading, identity, apiBase: resolvedApiBase }, children });
203
+ }
204
+ function Ad({
205
+ slot,
206
+ position = "static",
207
+ className,
208
+ style,
209
+ fallback,
210
+ onImpression,
211
+ onClick
212
+ }) {
213
+ const { siteId, config, isLoading, identity, apiBase } = useAds();
214
+ const [ad, setAd] = react.useState(null);
215
+ const [error, setError] = react.useState(false);
216
+ const impressionTracked = react.useRef(false);
217
+ const containerRef = react.useRef(null);
218
+ react.useEffect(() => {
219
+ if (isLoading || !config?.enabled || !siteId || !identity) return;
220
+ const path = typeof window !== "undefined" ? window.location.pathname : "/";
221
+ const referrer = typeof document !== "undefined" ? document.referrer : "";
222
+ if (config.excludePaths?.some((p) => new RegExp(p.replace("*", ".*")).test(path))) {
223
+ return;
224
+ }
225
+ const deviceType = getDeviceType(identity.signals);
226
+ const browser = getBrowserInfo(identity.signals);
227
+ const os = getOSInfo(identity.signals);
228
+ fetch(`${apiBase}/ads/get`, {
229
+ method: "POST",
230
+ headers: { "Content-Type": "application/json" },
231
+ body: JSON.stringify({
232
+ siteId,
233
+ slot,
234
+ path,
235
+ referrer,
236
+ user: {
237
+ uid: identity.uid,
238
+ fingerprint: identity.fingerprint,
239
+ isAuthenticated: identity.isAuthenticated
240
+ },
241
+ device: {
242
+ type: deviceType,
243
+ browser: browser.name,
244
+ browserVersion: browser.version,
245
+ os: os.name,
246
+ osVersion: os.version,
247
+ screenWidth: identity.signals.screenWidth,
248
+ screenHeight: identity.signals.screenHeight,
249
+ language: identity.signals.language,
250
+ timezone: identity.signals.timezone
251
+ }
252
+ })
253
+ }).then((r) => r.json()).then((data) => {
254
+ if (data?.html) {
255
+ setAd(data);
256
+ } else {
257
+ setError(true);
258
+ }
259
+ }).catch(() => setError(true));
260
+ }, [siteId, config, isLoading, slot, identity, apiBase]);
261
+ react.useEffect(() => {
262
+ if (!ad || impressionTracked.current || typeof window === "undefined" || !identity) return;
263
+ const observer = new IntersectionObserver(
264
+ ([entry]) => {
265
+ if (entry.isIntersecting && !impressionTracked.current) {
266
+ impressionTracked.current = true;
267
+ const impressionData = {
268
+ adId: ad.id,
269
+ siteId,
270
+ slot,
271
+ uid: identity.uid,
272
+ fingerprint: identity.fingerprint,
273
+ timestamp: Date.now()
274
+ };
275
+ if (navigator.sendBeacon) {
276
+ navigator.sendBeacon(
277
+ `${apiBase}/ads/impression`,
278
+ JSON.stringify(impressionData)
279
+ );
280
+ }
281
+ onImpression?.(ad.id);
282
+ }
283
+ },
284
+ { threshold: 0.5 }
285
+ );
286
+ if (containerRef.current) {
287
+ observer.observe(containerRef.current);
288
+ }
289
+ return () => observer.disconnect();
290
+ }, [ad, siteId, slot, onImpression, identity, apiBase]);
291
+ if (isLoading) {
292
+ return /* @__PURE__ */ jsxRuntime.jsx(
293
+ "div",
294
+ {
295
+ className: `cimplify-ad cimplify-ad--loading ${className || ""}`,
296
+ style,
297
+ "aria-hidden": "true"
298
+ }
299
+ );
300
+ }
301
+ if (!config?.enabled) return null;
302
+ if (error) return fallback ? /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: fallback }) : null;
303
+ if (!ad) return null;
304
+ const handleClick = () => {
305
+ onClick?.(ad.id);
306
+ };
307
+ const positionStyles = position === "fixed" ? { position: "fixed", bottom: 0, left: 0, right: 0, zIndex: 9999 } : position === "sticky" ? { position: "sticky", top: 20 } : {};
308
+ return /* @__PURE__ */ jsxRuntime.jsx(
309
+ "div",
310
+ {
311
+ ref: containerRef,
312
+ id: `cimplify-ad-${ad.id}`,
313
+ className: `cimplify-ad cimplify-ad--${slot} cimplify-ad--${position} cimplify-ad--${config.theme} ${className || ""}`,
314
+ style: { ...positionStyles, ...style },
315
+ onClick: handleClick,
316
+ dangerouslySetInnerHTML: { __html: ad.html }
317
+ }
318
+ );
319
+ }
320
+ var ElementsContext = react.createContext({
321
+ elements: null,
322
+ isReady: false
323
+ });
324
+ function useElements() {
325
+ return react.useContext(ElementsContext).elements;
326
+ }
327
+ function useElementsReady() {
328
+ return react.useContext(ElementsContext).isReady;
329
+ }
330
+ function ElementsProvider({
331
+ client,
332
+ businessId,
333
+ options,
334
+ children
335
+ }) {
336
+ const [elements, setElements] = react.useState(null);
337
+ const [isReady, setIsReady] = react.useState(false);
338
+ react.useEffect(() => {
339
+ const instance = client.elements(businessId, options);
340
+ setElements(instance);
341
+ setIsReady(true);
342
+ return () => instance.destroy();
343
+ }, [client, businessId, options]);
344
+ return /* @__PURE__ */ jsxRuntime.jsx(ElementsContext.Provider, { value: { elements, isReady }, children });
345
+ }
346
+ function AuthElement({
347
+ className,
348
+ style,
349
+ prefillEmail,
350
+ onReady,
351
+ onAuthenticated,
352
+ onRequiresOtp,
353
+ onError
354
+ }) {
355
+ const containerRef = react.useRef(null);
356
+ const elementRef = react.useRef(null);
357
+ const elements = useElements();
358
+ react.useEffect(() => {
359
+ if (!elements || !containerRef.current) return;
360
+ const element = elements.create(ELEMENT_TYPES.AUTH, { prefillEmail });
361
+ elementRef.current = element;
362
+ element.on(EVENT_TYPES.READY, () => onReady?.());
363
+ element.on(EVENT_TYPES.AUTHENTICATED, (data) => onAuthenticated?.(data));
364
+ element.on(EVENT_TYPES.REQUIRES_OTP, (data) => onRequiresOtp?.(data));
365
+ element.on(EVENT_TYPES.ERROR, (data) => onError?.(data));
366
+ element.mount(containerRef.current);
367
+ return () => element.destroy();
368
+ }, [elements, prefillEmail, onReady, onAuthenticated, onRequiresOtp, onError]);
369
+ return /* @__PURE__ */ jsxRuntime.jsx("div", { ref: containerRef, className, style });
370
+ }
371
+ function AddressElement({
372
+ className,
373
+ style,
374
+ mode = "shipping",
375
+ onReady,
376
+ onChange,
377
+ onError
378
+ }) {
379
+ const containerRef = react.useRef(null);
380
+ const elementRef = react.useRef(null);
381
+ const elements = useElements();
382
+ react.useEffect(() => {
383
+ if (!elements || !containerRef.current) return;
384
+ const element = elements.create(ELEMENT_TYPES.ADDRESS, { mode });
385
+ elementRef.current = element;
386
+ element.on(EVENT_TYPES.READY, () => onReady?.());
387
+ element.on(EVENT_TYPES.CHANGE, (data) => onChange?.(data));
388
+ element.on(EVENT_TYPES.ERROR, (data) => onError?.(data));
389
+ element.mount(containerRef.current);
390
+ return () => element.destroy();
391
+ }, [elements, mode, onReady, onChange, onError]);
392
+ return /* @__PURE__ */ jsxRuntime.jsx("div", { ref: containerRef, className, style });
393
+ }
394
+ function PaymentElement({
395
+ className,
396
+ style,
397
+ amount,
398
+ currency,
399
+ onReady,
400
+ onChange,
401
+ onError
402
+ }) {
403
+ const containerRef = react.useRef(null);
404
+ const elementRef = react.useRef(null);
405
+ const elements = useElements();
406
+ react.useEffect(() => {
407
+ if (!elements || !containerRef.current) return;
408
+ const element = elements.create(ELEMENT_TYPES.PAYMENT, { amount, currency });
409
+ elementRef.current = element;
410
+ element.on(EVENT_TYPES.READY, () => onReady?.());
411
+ element.on(EVENT_TYPES.CHANGE, (data) => onChange?.(data));
412
+ element.on(EVENT_TYPES.ERROR, (data) => onError?.(data));
413
+ element.mount(containerRef.current);
414
+ return () => element.destroy();
415
+ }, [elements, amount, currency, onReady, onChange, onError]);
416
+ return /* @__PURE__ */ jsxRuntime.jsx("div", { ref: containerRef, className, style });
417
+ }
418
+ function useCheckout() {
419
+ const elements = useElements();
420
+ const [isLoading, setIsLoading] = react.useState(false);
421
+ const submit = react.useCallback(
422
+ async (data) => {
423
+ if (!elements) {
424
+ return { success: false, error: { code: "NO_ELEMENTS", message: "Elements not initialized" } };
425
+ }
426
+ setIsLoading(true);
427
+ try {
428
+ return await elements.submitCheckout(data);
429
+ } finally {
430
+ setIsLoading(false);
431
+ }
432
+ },
433
+ [elements]
434
+ );
435
+ return { submit, isLoading };
436
+ }
437
+
438
+ exports.Ad = Ad;
439
+ exports.AdProvider = AdProvider;
440
+ exports.AddressElement = AddressElement;
441
+ exports.AuthElement = AuthElement;
442
+ exports.ElementsProvider = ElementsProvider;
443
+ exports.PaymentElement = PaymentElement;
444
+ exports.useAds = useAds;
445
+ exports.useCheckout = useCheckout;
446
+ exports.useElements = useElements;
447
+ exports.useElementsReady = useElementsReady;