@aippy/runtime 0.4.2-dev.5 → 0.4.2-dev.8

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,993 @@
1
+ var ue = Object.defineProperty;
2
+ var de = (e, t, r) => t in e ? ue(e, t, { enumerable: !0, configurable: !0, writable: !0, value: r }) : e[t] = r;
3
+ var z = (e, t, r) => de(e, typeof t != "symbol" ? t + "" : t, r);
4
+ import { a as V, i as le, r as fe, b as he, c as me } from "./bridge-K3HqQwdj.js";
5
+ import { a as B, r as D } from "./app-version-checker-YVIFbytA.js";
6
+ import { E as x } from "./errors-B3bDbQbD.js";
7
+ import { a as pe, s as be } from "./container-message-D_fZlguH.js";
8
+ import { j as ye } from "./url-c26cuIpu.js";
9
+ const q = "wallet.showRecharge", we = 3e5;
10
+ class w extends Error {
11
+ constructor(t, r) {
12
+ super(r), this.type = t, this.name = "WalletRechargeError";
13
+ }
14
+ }
15
+ const R = /* @__PURE__ */ new WeakMap();
16
+ function H() {
17
+ if (typeof window > "u" || typeof document < "u" && document.visibilityState === "hidden")
18
+ return;
19
+ const e = window.webkit?.messageHandlers?.aippyListener;
20
+ if (typeof e?.postMessage == "function")
21
+ return { host: window, listener: e };
22
+ }
23
+ function ge(e) {
24
+ e.host.processRechargeResult === e.dispatch && (e.hadPrevious ? e.host.processRechargeResult = e.previous : delete e.host.processRechargeResult), R.delete(e.host);
25
+ }
26
+ function $(e) {
27
+ const t = {
28
+ host: e,
29
+ previous: e.processRechargeResult,
30
+ hadPrevious: Object.prototype.hasOwnProperty.call(
31
+ e,
32
+ "processRechargeResult"
33
+ ),
34
+ dispatch: (r) => {
35
+ const n = r && typeof r == "object" && !Array.isArray(r) ? r : void 0;
36
+ if (n?.status === "success" ? t.succeeded = !0 : n?.status === "closed" && typeof n.purchased == "boolean" ? (ge(t), t.succeeded && !n.purchased ? t.waiter?.reject(
37
+ new w("protocol", "Inconsistent recharge closure")
38
+ ) : t.waiter?.resolve(n.purchased)) : (t.quarantined = !0, t.waiter?.reject(
39
+ new w("protocol", "Invalid recharge result")
40
+ )), typeof t.previous == "function")
41
+ try {
42
+ t.previous.call(e, r);
43
+ } catch {
44
+ console.warn("Native recharge observer failed");
45
+ }
46
+ },
47
+ succeeded: !1,
48
+ quarantined: !1
49
+ };
50
+ return e.processRechargeResult = t.dispatch, R.set(e, t), t;
51
+ }
52
+ function ze() {
53
+ let e;
54
+ try {
55
+ if (typeof document < "u" && document.visibilityState === "hidden")
56
+ return;
57
+ const t = H();
58
+ if (t) {
59
+ if (R.has(t.host)) return;
60
+ e = $(t.host), t.listener.postMessage({ command: q });
61
+ return;
62
+ }
63
+ const r = V();
64
+ if (!r) return;
65
+ window.parent.postMessage({ type: q }, r);
66
+ } catch {
67
+ e && (e.quarantined = !0), console.warn("Failed to request native recharge");
68
+ }
69
+ }
70
+ function ve(e) {
71
+ return new Promise((t, r) => {
72
+ if (e?.aborted) {
73
+ r(new w("cancelled", "Recharge wait cancelled"));
74
+ return;
75
+ }
76
+ const n = H();
77
+ if (!n) {
78
+ r(
79
+ new w("unavailable", "Native recharge is unavailable")
80
+ );
81
+ return;
82
+ }
83
+ let o = R.get(n.host);
84
+ if (o?.waiter || o?.quarantined) {
85
+ r(
86
+ new w(
87
+ "busy",
88
+ "Another recharge continuation is active"
89
+ )
90
+ );
91
+ return;
92
+ }
93
+ if (o && n.host.processRechargeResult !== o.dispatch) {
94
+ o.quarantined = !0, r(
95
+ new w(
96
+ "protocol",
97
+ "Native recharge callback was replaced"
98
+ )
99
+ );
100
+ return;
101
+ }
102
+ const i = !o;
103
+ try {
104
+ o ?? (o = $(n.host));
105
+ } catch {
106
+ r(
107
+ new w(
108
+ "unavailable",
109
+ "Native recharge callback is unavailable"
110
+ )
111
+ );
112
+ return;
113
+ }
114
+ let u = !1;
115
+ const a = (f, g = !1) => {
116
+ f && (o.quarantined = !0), !u && (u = !0, clearTimeout(c), e?.removeEventListener("abort", s), f ? r(f) : t(g));
117
+ }, s = () => a(new w("cancelled", "Recharge wait cancelled")), c = setTimeout(
118
+ () => a(
119
+ new w(
120
+ "timeout",
121
+ "Recharge closure was not received"
122
+ )
123
+ ),
124
+ we
125
+ );
126
+ if (o.waiter = {
127
+ resolve: (f) => a(void 0, f),
128
+ reject: (f) => a(f)
129
+ }, e?.addEventListener("abort", s, { once: !0 }), e?.aborted) {
130
+ s();
131
+ return;
132
+ }
133
+ if (i)
134
+ try {
135
+ n.listener.postMessage({ command: q });
136
+ } catch {
137
+ a(
138
+ new w("bridge", "Native recharge request failed")
139
+ );
140
+ }
141
+ });
142
+ }
143
+ class d extends Error {
144
+ constructor(r, n, o = {}) {
145
+ super(
146
+ `${r === "validation" ? x.CODE : x.RUNTIME} ${n}`
147
+ );
148
+ z(this, "kind");
149
+ z(this, "httpStatus");
150
+ z(this, "code");
151
+ z(this, "type");
152
+ z(this, "reason");
153
+ z(this, "appMessage");
154
+ z(this, "traceId");
155
+ z(this, "retryAfterMs");
156
+ z(this, "bizOrderNo");
157
+ z(this, "settlementUnknown");
158
+ z(this, "authorizationUnknown");
159
+ z(this, "cause");
160
+ this.name = "MinistoreError", this.kind = r, this.httpStatus = o.httpStatus, this.code = o.code, this.type = o.type, this.reason = o.reason, this.appMessage = o.appMessage, this.traceId = o.traceId, this.retryAfterMs = o.retryAfterMs, this.bizOrderNo = o.bizOrderNo, this.settlementUnknown = o.settlementUnknown ?? !1, this.authorizationUnknown = o.authorizationUnknown ?? !1, this.cause = o.cause;
161
+ }
162
+ }
163
+ function h(e) {
164
+ throw new d("validation", e);
165
+ }
166
+ function y() {
167
+ throw new d("protocol", "Invalid ministore response");
168
+ }
169
+ function v(e) {
170
+ return (!e || typeof e != "object" || Array.isArray(e)) && y(), e;
171
+ }
172
+ function l(e) {
173
+ return typeof e != "string" && y(), e;
174
+ }
175
+ function b(e, t = !1) {
176
+ return (!Number.isSafeInteger(e) || !t && e < 0) && y(), e;
177
+ }
178
+ function O(e) {
179
+ return typeof e != "boolean" && y(), e;
180
+ }
181
+ function E(e, t) {
182
+ return Array.isArray(e) || y(), e.map(t);
183
+ }
184
+ function Ae(e) {
185
+ const t = v(e);
186
+ return {
187
+ title: l(t.title),
188
+ subtitle: l(t.subtitle),
189
+ iconUrl: l(t.iconUrl)
190
+ };
191
+ }
192
+ function F(e) {
193
+ const t = v(e);
194
+ return "value" in t || y(), {
195
+ benefitKey: l(t.benefitKey),
196
+ kind: l(t.kind),
197
+ value: t.value,
198
+ quantity: b(t.quantity),
199
+ used: b(t.used),
200
+ display: Ae(t.display)
201
+ };
202
+ }
203
+ function G(e) {
204
+ const t = v(e);
205
+ "value" in t || y();
206
+ const r = l(t.itemId);
207
+ _(r) || y();
208
+ const n = l(t.orderId);
209
+ return n !== "" && !N(n) && y(), {
210
+ itemId: r,
211
+ value: t.value,
212
+ orderId: n,
213
+ bizOrderNo: l(t.bizOrderNo),
214
+ createdAt: l(t.createdAt),
215
+ updatedAt: l(t.updatedAt)
216
+ };
217
+ }
218
+ function $e(e) {
219
+ if (e == null) return;
220
+ const t = v(e);
221
+ if (!(t.itemId === "" || Object.keys(t).length === 0))
222
+ return G(t);
223
+ }
224
+ function Oe(e) {
225
+ const t = v(e), r = F(t), n = r.quantity === 0 && r.used === 0 && (t.holdings === null || t.holdings === void 0) && (t.items === null || t.items === void 0), o = n || t.holdings === null || t.holdings === void 0 ? [] : E(t.holdings, (u) => {
226
+ const a = v(u), s = l(a.orderId), c = a.source === void 0 ? "" : l(a.source);
227
+ return (!N(s) && !(s === "" && c === "FREE") || !("value" in a)) && y(), {
228
+ orderId: s,
229
+ bizOrderNo: l(a.bizOrderNo),
230
+ value: a.value,
231
+ quantity: b(a.quantity),
232
+ createdAt: l(a.createdAt),
233
+ source: c
234
+ };
235
+ }), i = n || t.items === null || t.items === void 0 ? [] : E(t.items, G);
236
+ return { ...r, holdings: o, items: i };
237
+ }
238
+ function I(e, t) {
239
+ const r = v(e), n = l(r.reason), o = t(r);
240
+ return n === "" ? { ok: !0, data: o } : { ok: !1, reason: n, data: o };
241
+ }
242
+ function W(e) {
243
+ return {
244
+ benefitKey: l(e.benefitKey),
245
+ authMode: l(e.authMode),
246
+ authorized: O(e.authorized),
247
+ authorizedAt: l(e.authorizedAt),
248
+ bizOrderNo: l(e.bizOrderNo),
249
+ quantity: b(e.quantity),
250
+ amount: b(e.amount),
251
+ expireAt: l(e.expireAt),
252
+ used: O(e.used)
253
+ };
254
+ }
255
+ function P(e) {
256
+ (!e || typeof e != "object") && h("An action is required"), (typeof e.benefitKey != "string" || [...e.benefitKey].length < 1 || [...e.benefitKey].length > 64) && h("benefitKey must contain 1–64 Unicode code points"), e.bizOrderNo !== void 0 && k(e.bizOrderNo, "bizOrderNo", 64);
257
+ }
258
+ function Ne(e) {
259
+ (!Array.isArray(e) || e.length < 1 || e.length > 99) && h("itemIds must contain 1 to 99 item IDs");
260
+ const t = /* @__PURE__ */ new Set();
261
+ for (const r of e) {
262
+ (typeof r != "string" || !_(r)) && h("itemIds must contain positive uint64 decimal strings");
263
+ const n = BigInt(r).toString();
264
+ t.has(n) && h("itemIds must not contain duplicates"), t.add(n);
265
+ }
266
+ }
267
+ function Fe(e) {
268
+ if (P(e), e.autoPurchase !== void 0 && typeof e.autoPurchase != "boolean" && h("autoPurchase must be a boolean"), e.quantity !== void 0 && K(e.quantity), e.itemIds !== void 0 && (Ne(e.itemIds), e.quantity !== void 0 && e.quantity !== e.itemIds.length && h("quantity must equal itemIds length")), e.itemValues !== void 0) {
269
+ (!Array.isArray(e.itemValues) || e.itemValues.length < 1 || e.itemValues.length > 99) && h("itemValues must contain 1 to 99 item values"), e.itemValues.some((r) => r == null) && h("itemValues must contain JSON values");
270
+ const t = e.quantity ?? e.itemIds?.length ?? 1;
271
+ (e.itemValues.length > t || (e.autoPurchase !== !0 || e.itemIds !== void 0) && e.itemValues.length !== t) && h(
272
+ "itemValues must match consumed items or the automatic purchase deficit"
273
+ );
274
+ }
275
+ }
276
+ function Ge(e) {
277
+ P(e), e.quantity !== void 0 && K(e.quantity), e.itemValues !== void 0 && ((!Array.isArray(e.itemValues) || e.itemValues.length < 1 || e.itemValues.length > 99) && h("itemValues must contain 1 to 99 item values"), e.itemValues.some((t) => t == null) && h("itemValues must contain JSON values"), e.quantity !== void 0 && e.quantity !== e.itemValues.length && h("quantity must equal itemValues length"));
278
+ }
279
+ function We(e) {
280
+ P(e), (typeof e.itemId != "string" || !_(e.itemId)) && h("itemId must be a positive uint64 decimal string"), (e.value === null || e.value === void 0) && h("value is required");
281
+ }
282
+ function k(e, t, r = 128) {
283
+ (typeof e != "string" || [...e].length < 1 || [...e].length > r) && h(`${t} must contain a bounded Unicode string`);
284
+ }
285
+ function K(e) {
286
+ return (!Number.isSafeInteger(e) || e < 1 || e > 99) && h("quantity must be an integer from 1 to 99"), e;
287
+ }
288
+ function N(e) {
289
+ return /^[0-9]+$/.test(e) && BigInt(e) <= 18446744073709551615n;
290
+ }
291
+ function _(e) {
292
+ return N(e) && BigInt(e) > 0n;
293
+ }
294
+ const J = 3e4, L = /* @__PURE__ */ Symbol("authorization-pending"), ke = "wallet.authorize-result", Ee = "wallet.authorize-alive", Ie = "wallet.authorize-alive-ack";
295
+ let Me = 0;
296
+ function Q() {
297
+ return typeof globalThis.crypto?.randomUUID == "function" ? globalThis.crypto.randomUUID() : `ministore-${Date.now().toString(36)}-${++Me}-${Math.random().toString(36).slice(2)}`;
298
+ }
299
+ const U = /* @__PURE__ */ new WeakMap();
300
+ function Z(e) {
301
+ return v(typeof e == "string" ? JSON.parse(e) : e);
302
+ }
303
+ function Re(e, t, r) {
304
+ let n = U.get(e);
305
+ if (n) {
306
+ if (e.processAuthorizeResult !== n.dispatch)
307
+ throw new Error("The App authorization callback was replaced");
308
+ } else {
309
+ const i = /* @__PURE__ */ new Map(), u = e.processAuthorizeResult, a = (s) => {
310
+ let c;
311
+ try {
312
+ c = Z(s);
313
+ } catch {
314
+ u?.call(e, s);
315
+ return;
316
+ }
317
+ const f = typeof c.requestId == "string" ? i.get(c.requestId) : void 0;
318
+ f ? f(c) : u?.call(e, s);
319
+ };
320
+ n = {
321
+ previous: u,
322
+ hadPrevious: Object.prototype.hasOwnProperty.call(
323
+ e,
324
+ "processAuthorizeResult"
325
+ ),
326
+ dispatch: a,
327
+ pending: i
328
+ }, e.processAuthorizeResult = a, U.set(e, n);
329
+ }
330
+ n.pending.set(t, r);
331
+ const o = n;
332
+ return () => {
333
+ o.pending.delete(t), o.pending.size === 0 && (e.processAuthorizeResult === o.dispatch && (o.hadPrevious ? e.processAuthorizeResult = o.previous : delete e.processAuthorizeResult), U.delete(e));
334
+ };
335
+ }
336
+ function X(e, t) {
337
+ const r = typeof t.code == "number" || typeof t.code == "string" ? t.code : void 0, n = typeof t.reason == "string" ? t.reason : void 0, o = ["unsupported_type", "invalid_params", "busy"].includes(n ?? "") || n === "server_error" && [1001, 2e4, 1002, 1003, 1004, 401, 11001, 10001].includes(
338
+ Number(r)
339
+ );
340
+ return new d("authorization", "App authorization failed", {
341
+ bizOrderNo: e.bizOrderNo,
342
+ code: r,
343
+ type: n,
344
+ appMessage: typeof t.message == "string" ? t.message : void 0,
345
+ authorizationUnknown: !o
346
+ });
347
+ }
348
+ function Y(e, t) {
349
+ return e.benefitKey !== t.benefitKey || !e.authorized ? !1 : e.authMode === "none" || e.authMode === "once" ? !0 : e.authMode === "every" && e.bizOrderNo === t.bizOrderNo && e.quantity >= t.quantity && e.quantity <= 99 && !e.used;
350
+ }
351
+ function Ue(e, t) {
352
+ const r = v(t.data), n = l(r.reason);
353
+ return !n && r.authorized !== !1 && !(r.authMode === "every" && r.used === !0) && y(), new d("authorization", "App authorization rejected", {
354
+ bizOrderNo: e.bizOrderNo,
355
+ type: "rejected",
356
+ reason: n || "authorization_required",
357
+ code: typeof t.code == "number" || typeof t.code == "string" ? t.code : void 0,
358
+ appMessage: typeof t.message == "string" ? t.message : void 0,
359
+ authorizationUnknown: !1
360
+ });
361
+ }
362
+ function Te(e, t) {
363
+ const r = Z(e), n = b(r.code), o = l(r.msg);
364
+ if (n !== 0)
365
+ throw X(t, {
366
+ reason: "server_error",
367
+ code: n,
368
+ message: o
369
+ });
370
+ const i = I(r.data, W);
371
+ return i.ok ? Y(i.data, t) ? i : { ok: !1, reason: "authorization_required", data: i.data } : i;
372
+ }
373
+ function ee(e, t) {
374
+ const r = v(e);
375
+ switch (r.status) {
376
+ case "closed":
377
+ if (r.requesting === !0) return L;
378
+ throw r.requesting !== !1 && y(), new d("authorization", "App authorization closed", {
379
+ bizOrderNo: t.bizOrderNo,
380
+ type: "closed"
381
+ });
382
+ case "failed":
383
+ throw r.reason === "rejected" ? Ue(t, r) : X(t, r);
384
+ case "timeout":
385
+ throw new d("authorization", "App authorization timed out", {
386
+ bizOrderNo: t.bizOrderNo,
387
+ type: "timeout",
388
+ authorizationUnknown: !0
389
+ });
390
+ case "success":
391
+ return Te(r.data, t);
392
+ default:
393
+ y();
394
+ }
395
+ }
396
+ function te(e, t) {
397
+ return {
398
+ type: "authorize",
399
+ title: e.title,
400
+ buttonText: e.buttonText,
401
+ ...e.content !== void 0 ? { content: e.content } : {},
402
+ payload: {
403
+ benefitKey: e.benefitKey,
404
+ bizOrderNo: e.bizOrderNo,
405
+ quantity: e.quantity
406
+ },
407
+ requestId: t
408
+ };
409
+ }
410
+ function qe(e, t, r, n, o) {
411
+ if (!r)
412
+ try {
413
+ const i = ee(e, t);
414
+ if (i === L) return;
415
+ n(), o(i);
416
+ } catch (i) {
417
+ n(
418
+ i instanceof d && i.kind === "authorization" ? i : new d("protocol", "Invalid App authorization result", {
419
+ bizOrderNo: t.bizOrderNo,
420
+ authorizationUnknown: !0,
421
+ cause: i
422
+ })
423
+ );
424
+ }
425
+ }
426
+ function Se(e, t, r) {
427
+ if (t?.aborted)
428
+ return Promise.reject(
429
+ new d("abort", "App authorization cancelled", {
430
+ bizOrderNo: e.bizOrderNo
431
+ })
432
+ );
433
+ const n = Q();
434
+ return new Promise((o, i) => {
435
+ let u = !1;
436
+ const a = () => {
437
+ clearTimeout(f), window.removeEventListener("message", g), t?.removeEventListener("abort", c);
438
+ }, s = (p) => {
439
+ u || (u = !0, a(), i(p));
440
+ }, c = () => s(
441
+ new d("abort", "App authorization cancelled", {
442
+ bizOrderNo: e.bizOrderNo,
443
+ authorizationUnknown: !0
444
+ })
445
+ ), f = setTimeout(() => {
446
+ s(
447
+ new d(
448
+ "authorization",
449
+ "App authorization response timed out",
450
+ {
451
+ bizOrderNo: e.bizOrderNo,
452
+ type: "timeout",
453
+ authorizationUnknown: !0
454
+ }
455
+ )
456
+ );
457
+ }, J), g = (p) => {
458
+ if (le(p, r)) {
459
+ if (p.data.type === Ee && p.data.requestId === n) {
460
+ window.parent.postMessage(
461
+ { type: Ie, requestId: n },
462
+ r
463
+ );
464
+ return;
465
+ }
466
+ p.data.type === ke && p.data.requestId === n && qe(
467
+ p.data,
468
+ e,
469
+ u,
470
+ (m) => {
471
+ m ? s(m) : (u = !0, a());
472
+ },
473
+ o
474
+ );
475
+ }
476
+ };
477
+ window.addEventListener("message", g), t?.addEventListener("abort", c, { once: !0 });
478
+ try {
479
+ window.parent.postMessage(
480
+ {
481
+ type: "wallet.authorize",
482
+ parameters: te(e, n)
483
+ },
484
+ r
485
+ );
486
+ } catch (p) {
487
+ s(
488
+ new d(
489
+ "authorization",
490
+ "App authorization request failed",
491
+ {
492
+ bizOrderNo: e.bizOrderNo,
493
+ authorizationUnknown: !0,
494
+ cause: p
495
+ }
496
+ )
497
+ );
498
+ }
499
+ });
500
+ }
501
+ function Je() {
502
+ if (typeof window > "u" || typeof navigator > "u" || typeof window.webkit?.messageHandlers?.aippyListener?.postMessage != "function")
503
+ return !1;
504
+ const e = B("ministore");
505
+ return e.envCheck.type === "new_app" && e.supported;
506
+ }
507
+ function Pe(e, t) {
508
+ const r = typeof window > "u" ? void 0 : window.webkit?.messageHandlers?.aippyListener;
509
+ if (typeof r?.postMessage != "function") {
510
+ const i = V();
511
+ return i ? Se(e, t, i) : Promise.reject(
512
+ new d("unsupported", "App authorization is unavailable", {
513
+ bizOrderNo: e.bizOrderNo
514
+ })
515
+ );
516
+ }
517
+ if (t?.aborted)
518
+ return Promise.reject(
519
+ new d("abort", "App authorization cancelled", {
520
+ bizOrderNo: e.bizOrderNo
521
+ })
522
+ );
523
+ try {
524
+ const i = D("ministore");
525
+ if (i.envCheck.type !== "new_app" || !i.supported)
526
+ throw new Error("Native App authorization is unavailable");
527
+ } catch (i) {
528
+ return Promise.reject(
529
+ new d("unsupported", "App authorization is unavailable", {
530
+ bizOrderNo: e.bizOrderNo,
531
+ cause: i
532
+ })
533
+ );
534
+ }
535
+ const n = window, o = Q();
536
+ return new Promise((i, u) => {
537
+ let a = !1, s = () => {
538
+ };
539
+ const c = () => {
540
+ clearTimeout(p), s(), t?.removeEventListener("abort", g);
541
+ }, f = (m) => {
542
+ a || (a = !0, c(), u(m));
543
+ }, g = () => f(
544
+ new d("abort", "App authorization cancelled", {
545
+ bizOrderNo: e.bizOrderNo,
546
+ authorizationUnknown: !0
547
+ })
548
+ ), p = setTimeout(() => {
549
+ f(
550
+ new d(
551
+ "authorization",
552
+ "App authorization response timed out",
553
+ {
554
+ bizOrderNo: e.bizOrderNo,
555
+ type: "timeout",
556
+ authorizationUnknown: !0
557
+ }
558
+ )
559
+ );
560
+ }, J);
561
+ try {
562
+ s = Re(n, o, (m) => {
563
+ if (!a)
564
+ try {
565
+ const A = ee(m, e);
566
+ if (A === L) return;
567
+ a = !0, c(), i(A);
568
+ } catch (A) {
569
+ f(
570
+ A instanceof d && A.kind === "authorization" ? A : new d(
571
+ "protocol",
572
+ "Invalid App authorization result",
573
+ {
574
+ bizOrderNo: e.bizOrderNo,
575
+ authorizationUnknown: !0,
576
+ cause: A
577
+ }
578
+ )
579
+ );
580
+ }
581
+ });
582
+ } catch (m) {
583
+ f(
584
+ new d(
585
+ "unsupported",
586
+ "App authorization callback unavailable",
587
+ {
588
+ bizOrderNo: e.bizOrderNo,
589
+ cause: m
590
+ }
591
+ )
592
+ );
593
+ return;
594
+ }
595
+ t?.addEventListener("abort", g, { once: !0 });
596
+ try {
597
+ r.postMessage({
598
+ command: "wallet.authorize",
599
+ parameters: te(e, o)
600
+ });
601
+ } catch (m) {
602
+ f(
603
+ new d(
604
+ "authorization",
605
+ "App authorization request failed",
606
+ {
607
+ bizOrderNo: e.bizOrderNo,
608
+ authorizationUnknown: !0,
609
+ cause: m
610
+ }
611
+ )
612
+ );
613
+ }
614
+ });
615
+ }
616
+ const S = /* @__PURE__ */ Symbol("ministoreScopeCheck");
617
+ function re(e) {
618
+ let t;
619
+ try {
620
+ t = new URL(e ?? `${fe()}/aisdk/v1/`);
621
+ } catch {
622
+ h("baseUrl must be a valid official Aippy API URL");
623
+ }
624
+ return (!he(t.origin) || t.username || t.password || t.search || t.hash || t.pathname !== "/aisdk/v1" && t.pathname !== "/aisdk/v1/") && h(
625
+ "baseUrl must use an official HTTPS Aippy API origin and /aisdk/v1/"
626
+ ), t.href;
627
+ }
628
+ function oe(e) {
629
+ if (e !== void 0)
630
+ try {
631
+ return JSON.stringify(e, (t, r) => {
632
+ if (["undefined", "bigint", "function", "symbol"].includes(typeof r) || typeof r == "number" && !Number.isFinite(r))
633
+ throw new TypeError("Non-JSON request value");
634
+ return r;
635
+ });
636
+ } catch {
637
+ h("Request body must contain finite, non-cyclic JSON values");
638
+ }
639
+ }
640
+ function Qe(e) {
641
+ return JSON.parse(oe(e));
642
+ }
643
+ function T(e) {
644
+ if (e?.aborted)
645
+ throw e.reason ?? new DOMException("Aborted", "AbortError");
646
+ }
647
+ function j(e) {
648
+ return e !== null && typeof e == "object" && !Array.isArray(e) ? e : void 0;
649
+ }
650
+ function Ke(e) {
651
+ if (!e) return;
652
+ const t = Number(e), r = Number.isFinite(t) ? t * 1e3 : Date.parse(e) - Date.now();
653
+ return Number.isFinite(r) && r >= 0 ? r : void 0;
654
+ }
655
+ async function M(e, t, r, n) {
656
+ const o = ye(
657
+ re(t.baseUrl),
658
+ `ministore/${e}`
659
+ ), i = oe(r.body);
660
+ let u = !1, a, s = "auth";
661
+ const c = {
662
+ bizOrderNo: r.bizOrderNo
663
+ };
664
+ try {
665
+ T(t.signal);
666
+ const f = await pe();
667
+ T(t.signal), t[S]?.(), t.language && f.set("App-Language", t.language), r.body !== void 0 && f.set("Content-Type", "application/json"), s = "network", u = !0, a = await globalThis.fetch(o, {
668
+ method: r.method,
669
+ headers: f,
670
+ cache: "no-store",
671
+ credentials: "omit",
672
+ redirect: "error",
673
+ signal: t.signal,
674
+ body: i
675
+ }), c.httpStatus = a.status, c.traceId = a.headers.get("X-Trace-Id") ?? void 0, c.retryAfterMs = Ke(a.headers.get("Retry-After")), s = "protocol";
676
+ const g = await a.json().catch((p) => {
677
+ if (a.ok) throw p;
678
+ });
679
+ if (T(t.signal), a.status !== 200) {
680
+ const p = j(g), m = j(p?.error), ce = typeof m?.message == "string" && (typeof m.code == "number" || typeof m.code == "string") && [400, 401, 403, 429].includes(a.status);
681
+ if (c.settlementUnknown = r.method === "POST" && !ce, c.code = typeof m?.code == "number" || typeof m?.code == "string" ? m.code : void 0, c.type = typeof m?.type == "string" ? m.type : void 0, c.appMessage = typeof p?.appMessage == "string" ? p.appMessage : void 0, c.appMessage)
682
+ try {
683
+ be(c.appMessage);
684
+ } catch {
685
+ }
686
+ throw new d(
687
+ "http",
688
+ typeof m?.message == "string" ? m.message : `Ministore request failed (${a.status})`,
689
+ c
690
+ );
691
+ }
692
+ return n(g);
693
+ } catch (f) {
694
+ if (f instanceof d && f.kind === "http") throw f;
695
+ c.settlementUnknown = u && r.method === "POST", c.cause = f;
696
+ const g = t.signal?.aborted || f instanceof Error && f.name === "AbortError";
697
+ throw new d(
698
+ g ? "abort" : s,
699
+ g ? "Ministore request cancelled" : s === "auth" ? "Ministore authentication unavailable" : s === "network" ? "Ministore network request failed" : "Invalid ministore response",
700
+ c
701
+ );
702
+ }
703
+ }
704
+ function _e(e = {}) {
705
+ return M(
706
+ "wallet/balance",
707
+ e,
708
+ { method: "GET" },
709
+ (t) => {
710
+ const r = v(t);
711
+ return r.assetType !== "GEM" && y(), b(r.balance, !0);
712
+ }
713
+ );
714
+ }
715
+ async function Le(e, t = {}) {
716
+ (!e || typeof e != "object") && h("An authorization query is required"), k(e.benefitKey, "benefitKey", 64), e.bizOrderNo !== void 0 && k(e.bizOrderNo, "bizOrderNo", 64);
717
+ const r = new URLSearchParams({ benefitKey: e.benefitKey });
718
+ return e.bizOrderNo && r.set("bizOrderNo", e.bizOrderNo), M(
719
+ `benefits/authorization?${r}`,
720
+ t,
721
+ { method: "GET", bizOrderNo: e.bizOrderNo },
722
+ (n) => I(n, W)
723
+ );
724
+ }
725
+ function Ze(e = {}) {
726
+ return M(
727
+ "catalog",
728
+ e,
729
+ { method: "GET" },
730
+ (t) => I(t, (r) => ({
731
+ enabled: O(r.enabled),
732
+ items: E(r.items, (n) => {
733
+ const o = v(n);
734
+ return {
735
+ ...F(o),
736
+ price: b(o.price),
737
+ freeQuota: b(o.freeQuota),
738
+ maxQuantity: b(o.maxQuantity),
739
+ consumable: O(o.consumable),
740
+ maxUserPurchase: b(o.maxUserPurchase),
741
+ userPurchased: b(o.userPurchased),
742
+ maxTotalPurchase: b(o.maxTotalPurchase),
743
+ totalSold: b(o.totalSold),
744
+ authMode: l(o.authMode),
745
+ authRequired: O(o.authRequired),
746
+ authorized: O(o.authorized),
747
+ authorizedAt: l(o.authorizedAt)
748
+ };
749
+ })
750
+ }))
751
+ );
752
+ }
753
+ function Xe(e = {}) {
754
+ return M(
755
+ "benefits",
756
+ e,
757
+ { method: "GET" },
758
+ (t) => I(t, (r) => ({ benefits: E(r.benefits, Oe) }))
759
+ );
760
+ }
761
+ async function Ye(e = {}, t = {}) {
762
+ e.benefitKey !== void 0 && k(e.benefitKey, "benefitKey", 64);
763
+ const r = e.limit ?? 20;
764
+ (!Number.isInteger(r) || r < 1 || r > 50) && h("limit must be an integer from 1 to 50"), e.cursor !== void 0 && (typeof e.cursor != "string" || e.cursor !== "" && !N(e.cursor)) && h("cursor must be a uint64 decimal string");
765
+ const n = new URLSearchParams({ limit: String(r) });
766
+ return e.cursor && n.set("cursor", e.cursor), e.benefitKey !== void 0 && n.set("benefitKey", e.benefitKey), M(
767
+ `records?${n}`,
768
+ t,
769
+ { method: "GET" },
770
+ (o) => I(o, (i) => {
771
+ const u = l(i.nextCursor);
772
+ return u !== "" && !N(u) && y(), {
773
+ nextCursor: u,
774
+ records: E(i.records, (a) => {
775
+ const s = v(a), c = l(s.id);
776
+ return (!N(c) || !("value" in s)) && y(), {
777
+ id: c,
778
+ op: l(s.op),
779
+ benefitKey: l(s.benefitKey),
780
+ quantity: b(s.quantity),
781
+ charged: b(s.charged),
782
+ projectName: l(s.projectName),
783
+ createdAt: l(s.createdAt),
784
+ bizOrderNo: l(s.bizOrderNo),
785
+ value: s.value
786
+ };
787
+ })
788
+ };
789
+ })
790
+ );
791
+ }
792
+ async function et(e, t = {}) {
793
+ (!e || typeof e != "object") && h("An authorization input is required"), e.operation !== "purchase" && e.operation !== "consume" && h("Invalid authorization operation"), k(e.benefitKey, "benefitKey", 64), k(e.bizOrderNo, "bizOrderNo", 64), K(e.quantity), re(t.baseUrl);
794
+ for (const i of ["title", "buttonText"])
795
+ (typeof e[i] != "string" || e[i].trim() === "") && h(`${i} must be a nonempty string`);
796
+ e.content !== void 0 && typeof e.content != "string" && h("content must be a string");
797
+ const r = {
798
+ operation: e.operation,
799
+ benefitKey: e.benefitKey,
800
+ quantity: e.quantity,
801
+ bizOrderNo: e.bizOrderNo,
802
+ title: e.title,
803
+ buttonText: e.buttonText,
804
+ ...e.content !== void 0 ? { content: e.content } : {}
805
+ }, n = await Pe(r, t.signal);
806
+ if (!n.ok) return n;
807
+ let o;
808
+ try {
809
+ o = await Le(
810
+ { benefitKey: r.benefitKey, bizOrderNo: r.bizOrderNo },
811
+ t
812
+ );
813
+ } catch (i) {
814
+ throw new d(
815
+ i instanceof d ? i.kind : "authorization",
816
+ "Unable to verify App authorization",
817
+ {
818
+ ...i instanceof d ? i : {},
819
+ bizOrderNo: r.bizOrderNo,
820
+ authorizationUnknown: !0,
821
+ cause: i
822
+ }
823
+ );
824
+ }
825
+ return o.ok ? Y(o.data, r) ? o : {
826
+ ok: !1,
827
+ reason: "authorization_required",
828
+ data: o.data
829
+ } : o;
830
+ }
831
+ const C = 6e4, ne = 1e3;
832
+ function tt(e) {
833
+ const t = typeof window > "u" ? void 0 : window, r = t?.location?.href, n = B("ministore"), o = n.supported && n.envCheck.type === "new_app";
834
+ let i = null, u = !1;
835
+ const a = () => {
836
+ if (e[S]?.(), !o) return;
837
+ if (typeof window > "u" || window !== t || t?.location?.href !== r)
838
+ throw new DOMException("Purchase page changed", "AbortError");
839
+ if (u && typeof document < "u" && document.visibilityState === "hidden")
840
+ throw new DOMException("Purchase page is not visible", "AbortError");
841
+ const c = me();
842
+ if (!i) i = c;
843
+ else if (c !== i)
844
+ throw new DOMException("Purchase account changed", "AbortError");
845
+ };
846
+ return {
847
+ options: { ...e, [S]: a },
848
+ check: a,
849
+ prepareContinuation: () => {
850
+ u = !0, a();
851
+ },
852
+ available: () => o && !!i && typeof t?.webkit?.messageHandlers?.aippyListener?.postMessage == "function" && !(typeof document < "u" && document.visibilityState === "hidden")
853
+ };
854
+ }
855
+ function ie(e, t) {
856
+ return new Promise((r, n) => {
857
+ if (t.aborted) {
858
+ n(t.reason);
859
+ return;
860
+ }
861
+ const o = () => {
862
+ clearTimeout(i), n(t.reason);
863
+ }, i = setTimeout(() => {
864
+ t.removeEventListener("abort", o), r();
865
+ }, e);
866
+ t.addEventListener("abort", o, { once: !0 });
867
+ });
868
+ }
869
+ async function ae(e, t) {
870
+ const r = new AbortController(), n = e.options.signal, o = () => r.abort(n?.reason);
871
+ n?.addEventListener("abort", o, { once: !0 }), n?.aborted && o();
872
+ const i = Date.now() + C;
873
+ let u = !1;
874
+ const a = setTimeout(() => {
875
+ u = !0, r.abort(
876
+ new DOMException("Recharge balance update timed out", "TimeoutError")
877
+ );
878
+ }, C);
879
+ try {
880
+ return await t(
881
+ { ...e.options, signal: r.signal },
882
+ i
883
+ );
884
+ } catch (s) {
885
+ throw n?.aborted ? new w(
886
+ "cancelled",
887
+ "Recharge continuation cancelled"
888
+ ) : u ? new d(
889
+ "recharge",
890
+ "Recharge balance update was not observed",
891
+ { type: "balance_timeout" }
892
+ ) : s;
893
+ } finally {
894
+ clearTimeout(a), n?.removeEventListener("abort", o);
895
+ }
896
+ }
897
+ async function se(e, t, r) {
898
+ for (; ; ) {
899
+ e.check();
900
+ let n = ne;
901
+ try {
902
+ const o = await _e(t);
903
+ return e.check(), o;
904
+ } catch (o) {
905
+ if (!(o instanceof d && (o.kind === "network" || o.kind === "http" && (o.httpStatus === 429 || (o.httpStatus ?? 0) >= 500)))) throw o;
906
+ n = Math.max(n, o.retryAfterMs ?? 0);
907
+ }
908
+ await ie(
909
+ Math.min(n, Math.max(0, r - Date.now())),
910
+ t.signal
911
+ );
912
+ }
913
+ }
914
+ async function xe(e, t) {
915
+ await ae(e, async (r, n) => {
916
+ for (; ; ) {
917
+ if (await se(e, r, n) > t) return;
918
+ await ie(
919
+ Math.min(ne, Math.max(0, n - Date.now())),
920
+ r.signal
921
+ );
922
+ }
923
+ });
924
+ }
925
+ async function rt(e, t, r, n = !0) {
926
+ const o = await e();
927
+ if (o.ok || o.reason !== "insufficient_balance" || !n)
928
+ return o;
929
+ try {
930
+ if (t.options.signal?.aborted)
931
+ throw new w("cancelled", "Recharge action cancelled");
932
+ if (!t.available()) {
933
+ try {
934
+ D("ministore");
935
+ } catch {
936
+ return o;
937
+ }
938
+ return ze(), o;
939
+ }
940
+ t.check();
941
+ const i = await ae(
942
+ t,
943
+ (a, s) => se(t, a, s)
944
+ );
945
+ t.check();
946
+ const u = await ve(t.options.signal);
947
+ if (t.check(), !u) return o;
948
+ await xe(t, i), t.prepareContinuation();
949
+ } catch (i) {
950
+ const u = i instanceof d ? i : {}, a = t.options.signal?.aborted || i instanceof w && i.type === "cancelled" || i instanceof d && i.kind === "abort" || i instanceof Error && i.name === "AbortError";
951
+ throw new d(
952
+ a ? "abort" : "recharge",
953
+ a ? "Recharge continuation cancelled" : "Unable to continue purchase after recharge",
954
+ {
955
+ ...u,
956
+ type: i instanceof w ? i.type : u.type,
957
+ bizOrderNo: r,
958
+ settlementUnknown: !1,
959
+ authorizationUnknown: !1,
960
+ cause: i
961
+ }
962
+ );
963
+ }
964
+ return e();
965
+ }
966
+ export {
967
+ d as M,
968
+ S as R,
969
+ h as a,
970
+ Fe as b,
971
+ M as c,
972
+ I as d,
973
+ b as e,
974
+ O as f,
975
+ Oe as g,
976
+ Je as h,
977
+ k as i,
978
+ l as j,
979
+ y as k,
980
+ Ye as l,
981
+ Xe as m,
982
+ Ze as n,
983
+ $e as o,
984
+ Ge as p,
985
+ et as q,
986
+ tt as r,
987
+ Qe as s,
988
+ Le as t,
989
+ We as u,
990
+ _e as v,
991
+ rt as w,
992
+ ze as x
993
+ };