@viceme-ai/sdk 0.6.2 → 0.7.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.
Files changed (38) hide show
  1. package/README.md +18 -6
  2. package/dist/capability-mount.d.ts +2 -0
  3. package/dist/capability-mount.d.ts.map +1 -1
  4. package/dist/chunks/{client-DEg6YouY.js → client-C1Sw400H.js} +386 -300
  5. package/dist/chunks/client-C1Sw400H.js.map +1 -0
  6. package/dist/chunks/integration-CTsnJ8mN.js +38 -0
  7. package/dist/chunks/integration-CTsnJ8mN.js.map +1 -0
  8. package/dist/chunks/version-CAPPYmfK.js +6 -0
  9. package/dist/chunks/{version-BJiUVcir.js.map → version-CAPPYmfK.js.map} +1 -1
  10. package/dist/core/capabilities.d.ts.map +1 -1
  11. package/dist/core/presentation.d.ts +5 -0
  12. package/dist/core/presentation.d.ts.map +1 -1
  13. package/dist/danmaku/mount.d.ts.map +1 -1
  14. package/dist/danmaku.js +117 -91
  15. package/dist/danmaku.js.map +1 -1
  16. package/dist/engagement/integration.d.ts +15 -0
  17. package/dist/engagement/integration.d.ts.map +1 -0
  18. package/dist/generated/public-contract.d.ts +15 -1
  19. package/dist/generated/public-contract.d.ts.map +1 -1
  20. package/dist/index.js +7 -6
  21. package/dist/index.js.map +1 -1
  22. package/dist/loader/auto-loader.d.ts.map +1 -1
  23. package/dist/manifest.json +36 -27
  24. package/dist/session/session.d.ts +12 -0
  25. package/dist/session/session.d.ts.map +1 -1
  26. package/dist/testing.js +1 -1
  27. package/dist/tip/index.d.ts +2 -0
  28. package/dist/tip/index.d.ts.map +1 -1
  29. package/dist/tip/mount.d.ts.map +1 -1
  30. package/dist/tip.js +211 -194
  31. package/dist/tip.js.map +1 -1
  32. package/dist/transport/transport.d.ts.map +1 -1
  33. package/dist/version.d.ts +1 -1
  34. package/dist/viceme.min.js +46 -4
  35. package/dist/viceme.min.js.map +1 -1
  36. package/package.json +1 -1
  37. package/dist/chunks/client-DEg6YouY.js.map +0 -1
  38. package/dist/chunks/version-BJiUVcir.js +0 -6
package/dist/tip.js CHANGED
@@ -1,7 +1,8 @@
1
- import { B as Y } from "./chunks/build-endpoints-NrDmg6jf.js";
2
- import { c as E, V as h } from "./chunks/errors-C7DfLKcF.js";
3
- import { p as Z, t as M, i as U, a as ee } from "./chunks/validation-patAYhXC.js";
4
- const te = ["work", "workKey", "environment", "currency", "amount", "providers"], J = ["id", "title"], re = ["minCents", "maxCents", "stepCents"], oe = ["statusCode", "code", "message", "requestId"], ie = ["type", "channel", "workKey"], ae = [
1
+ import { B as x } from "./chunks/build-endpoints-NrDmg6jf.js";
2
+ import { c as I, V as b } from "./chunks/errors-C7DfLKcF.js";
3
+ import { p as ee, t as W, i as j, a as te } from "./chunks/validation-patAYhXC.js";
4
+ import { a as re, f as V } from "./chunks/integration-CTsnJ8mN.js";
5
+ const oe = ["work", "workKey", "environment", "currency", "amount", "providers"], Z = ["id", "title"], ie = ["minCents", "maxCents", "stepCents"], ae = ["statusCode", "code", "message", "requestId"], ne = ["type", "channel", "workKey"], se = [
5
6
  "type",
6
7
  "channel",
7
8
  "workKey",
@@ -9,25 +10,25 @@ const te = ["work", "workKey", "environment", "currency", "amount", "providers"]
9
10
  "work",
10
11
  "amountCents",
11
12
  "currency"
12
- ], ne = ["type", "channel", "workKey", "status"], se = 8e3, ce = 8e3, V = 16 * 1024, le = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
13
- function I(e, t) {
13
+ ], ce = ["type", "channel", "workKey", "status"], le = 8e3, de = 8e3, F = 16 * 1024, ye = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
14
+ function S(e, t) {
14
15
  if (typeof e != "object" || e === null || Array.isArray(e)) return !1;
15
16
  const o = Object.keys(e);
16
17
  return o.length === t.length && t.every((a) => o.includes(a));
17
18
  }
18
- function p() {
19
- return new h({
19
+ function h() {
20
+ return new b({
20
21
  code: "TIP_CONFIG_INVALID",
21
22
  message: "Shop returned an invalid Tip configuration.",
22
23
  retryable: !1,
23
24
  capability: "tip"
24
25
  });
25
26
  }
26
- function de(e) {
27
- return I(e, oe) ? e.statusCode === 400 && e.code === "TIP_CONFIG_CREDENTIALS_NOT_ALLOWED" && typeof e.message == "string" && typeof e.requestId == "string" && e.requestId.length >= 1 && e.requestId.length <= 128 : !1;
27
+ function ue(e) {
28
+ return S(e, ae) ? e.statusCode === 400 && e.code === "TIP_CONFIG_CREDENTIALS_NOT_ALLOWED" && typeof e.message == "string" && typeof e.requestId == "string" && e.requestId.length >= 1 && e.requestId.length <= 128 : !1;
28
29
  }
29
- function ye(e) {
30
- return new h({
30
+ function we(e) {
31
+ return new b({
31
32
  code: "TIP_CONFIG_CREDENTIALS_NOT_ALLOWED",
32
33
  message: "Tip configuration requests must not include credentials.",
33
34
  retryable: !1,
@@ -35,9 +36,9 @@ function ye(e) {
35
36
  capability: "tip"
36
37
  });
37
38
  }
38
- function we(e, t) {
39
- if (!I(e, te) || !I(e.work, J) || typeof e.work.id != "string" || !le.test(e.work.id) || !U(e.work.title) || e.workKey !== t || e.environment !== "SANDBOX" && e.environment !== "PRODUCTION" || e.workKey.startsWith("wrk_test_") && e.environment !== "SANDBOX" || e.workKey.startsWith("wrk_live_") && e.environment !== "PRODUCTION" || e.currency !== "CNY" || !I(e.amount, re) || e.amount.minCents !== 100 || e.amount.maxCents !== 2e4 || e.amount.stepCents !== 1 || !Array.isArray(e.providers) || e.providers.length < 1 || !e.providers.every(ee) || new Set(e.providers).size !== e.providers.length)
40
- throw p();
39
+ function fe(e, t) {
40
+ if (!S(e, oe) || !S(e.work, Z) || typeof e.work.id != "string" || !ye.test(e.work.id) || !j(e.work.title) || e.workKey !== t || e.environment !== "SANDBOX" && e.environment !== "PRODUCTION" || e.workKey.startsWith("wrk_test_") && e.environment !== "SANDBOX" || e.workKey.startsWith("wrk_live_") && e.environment !== "PRODUCTION" || e.currency !== "CNY" || !S(e.amount, ie) || e.amount.minCents !== 100 || e.amount.maxCents !== 2e4 || e.amount.stepCents !== 1 || !Array.isArray(e.providers) || e.providers.length < 1 || !e.providers.every(te) || new Set(e.providers).size !== e.providers.length)
41
+ throw h();
41
42
  return {
42
43
  work: { id: e.work.id, title: e.work.title },
43
44
  workKey: e.workKey,
@@ -47,134 +48,134 @@ function we(e, t) {
47
48
  providers: [...e.providers]
48
49
  };
49
50
  }
50
- function ue(e, t, o, a) {
51
+ function pe(e, t, o, a) {
51
52
  if (typeof e != "object" || e === null || Array.isArray(e)) return;
52
53
  const i = e;
53
54
  if (!(i.type !== "viceme:tip-headless-result" || i.channel !== t || i.workKey !== o.workKey)) {
54
55
  if (i.status === "PAID")
55
- return !I(i, ae) || !I(i.work, J) || i.work.id !== o.work.id || i.work.title !== o.work.title || i.amountCents !== a || i.currency !== "CNY" ? void 0 : {
56
+ return !S(i, se) || !S(i.work, Z) || i.work.id !== o.work.id || i.work.title !== o.work.title || i.amountCents !== a || i.currency !== "CNY" ? void 0 : {
56
57
  status: "PAID",
57
58
  work: { id: o.work.id, title: o.work.title },
58
59
  amountCents: a,
59
60
  currency: "CNY"
60
61
  };
61
- if ((i.status === "CANCELLED" || i.status === "UNKNOWN") && I(i, ne))
62
+ if ((i.status === "CANCELLED" || i.status === "UNKNOWN") && S(i, ce))
62
63
  return { status: i.status };
63
64
  }
64
65
  }
65
- function _() {
66
- return new h({
66
+ function N() {
67
+ return new b({
67
68
  code: "INTERNAL_ERROR",
68
69
  message: "Unable to open the hosted Tip flow.",
69
70
  capability: "tip"
70
71
  });
71
72
  }
72
- function D() {
73
- return new h({
73
+ function M() {
74
+ return new b({
74
75
  code: "CAPABILITY_DISABLED",
75
76
  message: "Tip is not available for this Work or region.",
76
77
  retryable: !1,
77
78
  capability: "tip"
78
79
  });
79
80
  }
80
- async function z(e) {
81
+ async function $(e) {
81
82
  const t = e.headers.get("content-length");
82
83
  if (t !== null) {
83
- const y = Number(t);
84
- if (!Number.isInteger(y) || y < 0 || y > V)
84
+ const d = Number(t);
85
+ if (!Number.isInteger(d) || d < 0 || d > F)
85
86
  throw await e.body?.cancel().catch(() => {
86
- }), p();
87
+ }), h();
87
88
  }
88
89
  const o = e.body?.getReader();
89
- if (!o) throw p();
90
+ if (!o) throw h();
90
91
  const a = new TextDecoder("utf-8", { fatal: !0 });
91
- let i = 0, c = "";
92
+ let i = 0, s = "";
92
93
  try {
93
94
  for (; ; ) {
94
- const y = await o.read();
95
- if (y.done) break;
96
- if (i += y.value.byteLength, i > V)
95
+ const d = await o.read();
96
+ if (d.done) break;
97
+ if (i += d.value.byteLength, i > F)
97
98
  throw await o.cancel().catch(() => {
98
- }), p();
99
+ }), h();
99
100
  try {
100
- c += a.decode(y.value, { stream: !0 });
101
+ s += a.decode(d.value, { stream: !0 });
101
102
  } catch {
102
103
  throw await o.cancel().catch(() => {
103
- }), p();
104
+ }), h();
104
105
  }
105
106
  }
106
107
  try {
107
- c += a.decode();
108
+ s += a.decode();
108
109
  } catch {
109
110
  throw await o.cancel().catch(() => {
110
- }), p();
111
+ }), h();
111
112
  }
112
113
  } finally {
113
114
  o.releaseLock();
114
115
  }
115
116
  try {
116
- return JSON.parse(c);
117
+ return JSON.parse(s);
117
118
  } catch {
118
- throw p();
119
+ throw h();
119
120
  }
120
121
  }
121
- async function fe(e, t) {
122
- if (await e.ready(), !e.hasCapability("tip") || e.region !== "cn") throw D();
123
- if (t.aborted) throw E();
122
+ async function me(e, t) {
123
+ if (await e.ready(), !e.hasCapability("tip") || e.region !== "cn") throw M();
124
+ if (t.aborted) throw I();
124
125
  const o = new AbortController(), a = () => o.abort();
125
126
  t.addEventListener("abort", a, { once: !0 });
126
- const i = globalThis.setTimeout(() => o.abort(), ce), c = Y[e.region], y = new URL(
127
+ const i = globalThis.setTimeout(() => o.abort(), de), s = x[e.region], d = new URL(
127
128
  `/v1/work-sdk/${encodeURIComponent(e.workKey)}/tip-config`,
128
- c
129
+ s
129
130
  );
130
131
  try {
131
- const n = await fetch(y.toString(), {
132
+ const n = await fetch(d.toString(), {
132
133
  method: "GET",
133
134
  credentials: "omit",
134
135
  redirect: "error",
135
136
  headers: { accept: "application/json" },
136
137
  signal: o.signal
137
- }), u = n.headers.get("content-type")?.split(";", 1)[0]?.trim().toLowerCase();
138
- let l;
138
+ }), p = n.headers.get("content-type")?.split(";", 1)[0]?.trim().toLowerCase();
139
+ let w;
139
140
  try {
140
- l = n.url ? new URL(n.url).origin : void 0;
141
+ w = n.url ? new URL(n.url).origin : void 0;
141
142
  } catch {
142
- l = "invalid";
143
+ w = "invalid";
143
144
  }
144
145
  if (n.status !== 200) {
145
146
  if (n.status === 404)
146
147
  throw await n.body?.cancel().catch(() => {
147
- }), D();
148
- if (n.status === 400 && u === "application/json" && (!l || l === c)) {
148
+ }), M();
149
+ if (n.status === 400 && p === "application/json" && (!w || w === s)) {
149
150
  let v;
150
151
  try {
151
- v = await z(n);
152
+ v = await $(n);
152
153
  } catch {
153
- throw _();
154
+ throw N();
154
155
  }
155
- if (de(v))
156
- throw ye(v.requestId);
156
+ if (ue(v))
157
+ throw we(v.requestId);
157
158
  } else
158
159
  await n.body?.cancel().catch(() => {
159
160
  });
160
- throw new h({
161
+ throw new b({
161
162
  code: "INTERNAL_ERROR",
162
163
  message: "Unable to load Tip configuration.",
163
164
  capability: "tip"
164
165
  });
165
166
  }
166
- if (u !== "application/json" || l && l !== c)
167
+ if (p !== "application/json" || w && w !== s)
167
168
  throw await n.body?.cancel().catch(() => {
168
- }), p();
169
- const w = await z(n);
170
- return we(w, e.workKey);
169
+ }), h();
170
+ const y = await $(n);
171
+ return fe(y, e.workKey);
171
172
  } catch (n) {
172
- throw t.aborted ? E() : n instanceof h ? n : _();
173
+ throw t.aborted ? I() : n instanceof b ? n : N();
173
174
  } finally {
174
175
  o.abort(), globalThis.clearTimeout(i), t.removeEventListener("abort", a);
175
176
  }
176
177
  }
177
- function me(e) {
178
+ function he(e) {
178
179
  return {
179
180
  work: { ...e.work },
180
181
  workKey: e.workKey,
@@ -184,11 +185,11 @@ function me(e) {
184
185
  providers: [...e.providers]
185
186
  };
186
187
  }
187
- function pe(e) {
188
+ function ge(e) {
188
189
  let t = !1, o, a;
189
- const i = new AbortController(), c = () => t || e.state === "DESTROYED", y = () => {
190
+ const i = new AbortController(), s = () => t || e.state === "DESTROYED", d = () => {
190
191
  if (!a) {
191
- const n = fe(e, i.signal);
192
+ const n = me(e, i.signal);
192
193
  a = n, n.then(
193
194
  () => {
194
195
  a === n && (a = void 0);
@@ -202,118 +203,118 @@ function pe(e) {
202
203
  };
203
204
  return {
204
205
  async getConfig() {
205
- if (c()) throw E();
206
- const n = await y();
207
- if (c()) throw E();
208
- return me(n);
206
+ if (s()) throw I();
207
+ const n = await d();
208
+ if (s()) throw I();
209
+ return he(n);
209
210
  },
210
211
  open(n) {
211
- if (c()) return Promise.reject(E());
212
+ if (s()) return Promise.reject(I());
212
213
  if (o)
213
214
  return Promise.reject(
214
- new h({
215
+ new b({
215
216
  code: "TIP_OPEN_IN_PROGRESS",
216
217
  message: "This Tip client already has an open flow.",
217
218
  retryable: !1,
218
219
  capability: "tip"
219
220
  })
220
221
  );
221
- if (!e.hasCapability("tip") || e.region !== "cn") return Promise.reject(D());
222
- let u;
222
+ if (!e.hasCapability("tip") || e.region !== "cn") return Promise.reject(M());
223
+ let p;
223
224
  try {
224
- u = Z(n);
225
- } catch (s) {
226
- return Promise.reject(s);
225
+ p = ee(n);
226
+ } catch (l) {
227
+ return Promise.reject(l);
227
228
  }
228
229
  if (typeof document > "u")
229
- return Promise.reject(M());
230
- const l = document, w = l.defaultView;
231
- if (!l.body || !w) return Promise.reject(M());
232
- const v = Y[e.region], d = l.documentElement.lang || w.navigator.language, O = u.locale ?? (d.toLowerCase().startsWith("zh") ? "zh-CN" : "en-US"), L = u.appearance ?? "auto", S = L === "auto" ? w.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light" : L;
233
- let C, K;
230
+ return Promise.reject(W());
231
+ const w = document, y = w.defaultView;
232
+ if (!w.body || !y) return Promise.reject(W());
233
+ const v = x[e.region], R = w.documentElement.lang || y.navigator.language, u = p.locale ?? (R.toLowerCase().startsWith("zh") ? "zh-CN" : "en-US"), U = p.appearance ?? "auto", c = U === "auto" ? y.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light" : U;
234
+ let T, Y;
234
235
  try {
235
- const s = new Uint8Array(16);
236
- w.crypto.getRandomValues(s), C = Array.from(s, (b) => b.toString(16).padStart(2, "0")).join(""), K = e.ready();
236
+ const l = new Uint8Array(16);
237
+ y.crypto.getRandomValues(l), T = Array.from(l, (E) => E.toString(16).padStart(2, "0")).join(""), Y = e.ready();
237
238
  } catch {
238
- return Promise.reject(_());
239
+ return Promise.reject(N());
239
240
  }
240
- const m = l.createElement("div");
241
- m.dataset.vicemeTipHeadless = "open", m.style.all = "initial", m.style.position = "fixed", m.style.inset = "0", m.style.display = "block", m.style.width = "100vw", m.style.height = "100vh", m.style.zIndex = "2147483647";
242
- const f = l.createElement("iframe"), T = new URL(`/widget/tip/${encodeURIComponent(e.workKey)}`, v);
243
- T.searchParams.set("mode", "headless"), T.searchParams.set("channel", C), T.searchParams.set("appearance", S), T.searchParams.set("locale", O), f.title = "ViceMe Tip", f.src = T.toString(), f.loading = "eager", f.referrerPolicy = "strict-origin", f.setAttribute(
241
+ const g = w.createElement("div");
242
+ g.dataset.vicemeTipHeadless = "open", g.style.all = "initial", g.style.position = "fixed", g.style.inset = "0", g.style.display = "block", g.style.width = "100vw", g.style.height = "100vh", g.style.zIndex = "2147483647";
243
+ const f = w.createElement("iframe"), A = new URL(`/widget/tip/${encodeURIComponent(e.workKey)}`, v);
244
+ A.searchParams.set("mode", "headless"), A.searchParams.set("channel", T), A.searchParams.set("appearance", c), A.searchParams.set("locale", u), f.title = "ViceMe Tip", f.src = A.toString(), f.loading = "eager", f.referrerPolicy = "strict-origin", f.setAttribute(
244
245
  "sandbox",
245
246
  "allow-forms allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts"
246
- ), f.setAttribute("allow", "payment"), f.style.display = "block", f.style.width = "100%", f.style.height = "100%", f.style.border = "0", f.style.background = "transparent", f.style.colorScheme = S, m.attachShadow({ mode: "open" }).append(f);
247
- let k = !1, g = !1, r = !1, R, A, j, W;
248
- const Q = new Promise((s, b) => {
249
- j = s, W = b;
250
- }), x = () => {
251
- A !== void 0 && (w.clearTimeout(A), A = void 0), w.removeEventListener("message", H), m.remove(), o === B && (o = void 0);
252
- }, G = (s) => {
253
- k || (k = !0, x(), j(s));
254
- }, N = (s) => {
255
- k || (k = !0, x(), W(s));
256
- }, B = () => G({ status: "UNKNOWN" }), q = () => {
257
- if (!g || !R || r || k) return;
258
- if (u.provider && !R.providers.includes(u.provider)) {
259
- N(M());
247
+ ), f.setAttribute("allow", "payment"), f.style.display = "block", f.style.width = "100%", f.style.height = "100%", f.style.border = "0", f.style.background = "transparent", f.style.colorScheme = c, g.attachShadow({ mode: "open" }).append(f);
248
+ let O = !1, P = !1, _ = !1, C, k, L, m;
249
+ const r = new Promise((l, E) => {
250
+ L = l, m = E;
251
+ }), G = () => {
252
+ k !== void 0 && (y.clearTimeout(k), k = void 0), y.removeEventListener("message", H), g.remove(), o === q && (o = void 0);
253
+ }, B = (l) => {
254
+ O || (O = !0, G(), L(l));
255
+ }, K = (l) => {
256
+ O || (O = !0, G(), m(l));
257
+ }, q = () => B({ status: "UNKNOWN" }), z = () => {
258
+ if (!P || !C || _ || O) return;
259
+ if (p.provider && !C.providers.includes(p.provider)) {
260
+ K(W());
260
261
  return;
261
262
  }
262
- r = !0;
263
- const s = {
263
+ _ = !0;
264
+ const l = {
264
265
  type: "viceme:tip-headless-init",
265
- channel: C,
266
+ channel: T,
266
267
  workKey: e.workKey,
267
- amountCents: u.amountCents,
268
- ...u.provider === void 0 ? {} : { provider: u.provider },
269
- locale: O,
270
- appearance: S
268
+ amountCents: p.amountCents,
269
+ ...p.provider === void 0 ? {} : { provider: p.provider },
270
+ locale: u,
271
+ appearance: c
271
272
  };
272
273
  try {
273
- const b = f.contentWindow;
274
- if (!b) {
275
- N(_());
274
+ const E = f.contentWindow;
275
+ if (!E) {
276
+ K(N());
276
277
  return;
277
278
  }
278
- b.postMessage(s, v);
279
+ E.postMessage(l, v);
279
280
  } catch {
280
- N(_());
281
+ K(N());
281
282
  }
282
- }, H = (s) => {
283
- if (s.origin !== v || s.source !== f.contentWindow) return;
284
- if (!r) {
285
- if (!I(s.data, ie) || s.data.type !== "viceme:tip-headless-ready" || s.data.channel !== C || s.data.workKey !== e.workKey)
283
+ }, H = (l) => {
284
+ if (l.origin !== v || l.source !== f.contentWindow) return;
285
+ if (!_) {
286
+ if (!S(l.data, ne) || l.data.type !== "viceme:tip-headless-ready" || l.data.channel !== T || l.data.workKey !== e.workKey)
286
287
  return;
287
- g = !0, A !== void 0 && (w.clearTimeout(A), A = void 0), q();
288
+ P = !0, k !== void 0 && (y.clearTimeout(k), k = void 0), z();
288
289
  return;
289
290
  }
290
- if (!R) return;
291
- const b = ue(s.data, C, R, u.amountCents);
292
- b && b.status !== "UNKNOWN" && G(b);
291
+ if (!C) return;
292
+ const E = pe(l.data, T, C, p.amountCents);
293
+ E && E.status !== "UNKNOWN" && B(E);
293
294
  };
294
- return o = B, w.addEventListener("message", H), A = w.setTimeout(() => {
295
- N(
296
- new h({
295
+ return o = q, y.addEventListener("message", H), k = y.setTimeout(() => {
296
+ K(
297
+ new b({
297
298
  code: "TIP_READY_TIMEOUT",
298
299
  message: "Hosted Tip did not become ready in time.",
299
300
  retryable: !0,
300
301
  capability: "tip"
301
302
  })
302
303
  );
303
- }, se), l.body.append(m), K.catch((s) => {
304
- N(s instanceof h ? s : _());
305
- }), y().then((s) => {
306
- R = s, q();
307
- }).catch((s) => {
308
- N(s instanceof h ? s : _());
309
- }), Q;
304
+ }, le), w.body.append(g), Y.catch((l) => {
305
+ K(l instanceof b ? l : N());
306
+ }), d().then((l) => {
307
+ C = l, z();
308
+ }).catch((l) => {
309
+ K(l instanceof b ? l : N());
310
+ }), r;
310
311
  },
311
312
  destroy() {
312
313
  t || (t = !0, i.abort(), o?.());
313
314
  }
314
315
  };
315
316
  }
316
- const he = /* @__PURE__ */ new Map([
317
+ const be = /* @__PURE__ */ new Map([
317
318
  ["viceme:ready", /* @__PURE__ */ new Set(["clientKey", "workKey", "capabilities", "version"])],
318
319
  ["viceme:capability-ready", /* @__PURE__ */ new Set(["clientKey", "instanceKey", "capability", "version"])],
319
320
  [
@@ -324,7 +325,7 @@ const he = /* @__PURE__ */ new Map([
324
325
  ["viceme:widget-close", /* @__PURE__ */ new Set(["workId"])],
325
326
  ["viceme:tip-paid", /* @__PURE__ */ new Set(["status", "work", "amountCents", "currency"])]
326
327
  ]);
327
- function ge(e, t) {
328
+ function ve(e, t) {
328
329
  if (e === "viceme:tip-paid") {
329
330
  const i = t;
330
331
  return {
@@ -334,38 +335,38 @@ function ge(e, t) {
334
335
  currency: i.currency
335
336
  };
336
337
  }
337
- const o = he.get(e), a = {};
338
- for (const [i, c] of Object.entries(t))
339
- o.has(i) && c !== void 0 && (a[i] = c);
338
+ const o = be.get(e), a = {};
339
+ for (const [i, s] of Object.entries(t))
340
+ o.has(i) && s !== void 0 && (a[i] = s);
340
341
  return a;
341
342
  }
342
- function F(e, t, o) {
343
+ function X(e, t, o) {
343
344
  const a = new CustomEvent(t, {
344
- detail: ge(t, o),
345
+ detail: ve(t, o),
345
346
  bubbles: !0,
346
347
  composed: !0,
347
348
  cancelable: !1
348
349
  });
349
350
  e.dispatchEvent(a);
350
351
  }
351
- const be = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i, ve = 100, ke = 2e4, Ee = 2048, Te = 8e3, Ie = ["type", "workId", "work", "height"], Ce = ["type", "workId"], Ae = ["type", "workKey", "status", "work", "amountCents", "currency"], $ = ["id", "title"];
352
- function P(e, t) {
352
+ const ke = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i, Ee = 100, Ie = 2e4, Te = 2048, Ce = 8e3, Ae = ["type", "workId", "work", "height"], _e = ["type", "workId"], Se = ["type", "workKey", "status", "work", "amountCents", "currency"], J = ["id", "title"];
353
+ function D(e, t) {
353
354
  if (typeof e != "object" || e === null || Array.isArray(e)) return !1;
354
355
  const o = Object.keys(e);
355
356
  return o.length === t.length && t.every((a) => o.includes(a));
356
357
  }
357
- async function _e(e, t) {
358
- if (t.signal?.aborted) throw E();
359
- if (await e.ready(), t.signal?.aborted) throw E();
358
+ async function Oe(e, t) {
359
+ if (t.signal?.aborted) throw I();
360
+ if (await e.ready(), t.signal?.aborted) throw I();
360
361
  if (!e.hasCapability("tip"))
361
- throw new h({
362
+ throw new b({
362
363
  code: "CAPABILITY_DISABLED",
363
364
  message: "Tip is not available in this SDK build.",
364
365
  retryable: !1,
365
366
  capability: "tip"
366
367
  });
367
368
  if (e.region !== "cn")
368
- throw new h({
369
+ throw new b({
369
370
  code: "CAPABILITY_DISABLED",
370
371
  message: "Tip is not available in this region.",
371
372
  retryable: !1,
@@ -373,100 +374,116 @@ async function _e(e, t) {
373
374
  });
374
375
  const o = t.target.ownerDocument, a = o.defaultView;
375
376
  if (!a)
376
- throw new h({
377
+ throw new b({
377
378
  code: "CONFIG_INVALID",
378
379
  message: "Tip requires a browser document.",
379
380
  retryable: !1,
380
381
  capability: "tip"
381
382
  });
382
- const i = Y[e.region], c = t.theme === "auto" ? a.matchMedia("(prefers-color-scheme: dark)") : void 0;
383
- let y = t.theme === "auto" ? c?.matches ? "dark" : "light" : t.theme, n = !1, u, l;
384
- const w = o.createElement("div");
385
- w.dataset.vicemeTip = "mounted", w.style.all = "initial", w.style.display = "block", w.style.width = "100%", w.style.maxWidth = "100%", w.style.contain = "layout style";
386
- const v = w.attachShadow({ mode: "open" }), d = o.createElement("iframe"), O = new URL(`/widget/tip/${encodeURIComponent(e.workKey)}`, i);
387
- O.searchParams.set("appearance", y), d.title = "ViceMe Tip", d.src = O.toString(), d.loading = "eager", d.referrerPolicy = "strict-origin", d.setAttribute(
383
+ const i = x[e.region], s = t.presentation === "integrated", d = t.theme === "auto" ? a.matchMedia("(prefers-color-scheme: dark)") : void 0;
384
+ let n = t.theme === "auto" ? d?.matches ? "dark" : "light" : t.theme, p = !1, w, y, v = !1, R;
385
+ const u = o.createElement("div");
386
+ u.dataset.vicemeTip = "mounted", u.style.all = "initial", u.style.display = s ? "none" : "block", u.style.width = "100%", u.style.maxWidth = "100%", u.style.contain = s ? "layout style size" : "layout style", s && (u.style.position = "fixed", u.style.inset = "0", u.style.height = "100%", u.style.pointerEvents = "none", u.style.zIndex = "2147483001");
387
+ const U = u.attachShadow({ mode: "open" }), c = o.createElement("iframe"), T = new URL(`/widget/tip/${encodeURIComponent(e.workKey)}`, i);
388
+ T.searchParams.set("appearance", n), s && T.searchParams.set("mode", "dialog"), c.title = "ViceMe Tip", c.src = T.toString(), c.loading = "eager", c.referrerPolicy = "strict-origin", c.setAttribute(
388
389
  "sandbox",
389
390
  "allow-forms allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts"
390
- ), d.setAttribute("allow", "payment"), d.style.boxSizing = "border-box", d.style.display = "block", d.style.width = "100%", d.style.height = "0px", d.style.margin = "0", d.style.border = "0", d.style.background = "transparent", d.style.colorScheme = y, d.style.pointerEvents = "none", v.append(d);
391
- let L, S;
392
- const C = new Promise((g, r) => {
393
- L = g, S = r, u = a.setTimeout(() => {
391
+ ), c.setAttribute("allow", "payment"), c.style.boxSizing = "border-box", c.style.display = "block", c.style.width = "100%", c.style.height = s ? "100%" : "0px", c.style.margin = "0", c.style.border = "0", c.style.background = "transparent", c.style.colorScheme = n, c.style.pointerEvents = "none", U.append(c);
392
+ const Y = () => {
393
+ p || !y || (v = !0, u.style.display = "block", u.style.pointerEvents = "auto", c.style.pointerEvents = "auto", c.contentWindow?.postMessage(
394
+ { type: "viceme:widget-open", workId: y.id },
395
+ i
396
+ ));
397
+ }, g = (m) => {
398
+ if (!s) return;
399
+ const r = v;
400
+ v = !1, u.style.display = "none", u.style.pointerEvents = "none", c.style.pointerEvents = "none", m && r && V(e, t.target);
401
+ };
402
+ let f, A;
403
+ const O = new Promise((m, r) => {
404
+ f = m, A = r, w = a.setTimeout(() => {
394
405
  r(
395
- new h({
406
+ new b({
396
407
  code: "INTERNAL_ERROR",
397
408
  message: "Hosted Tip widget did not become ready in time.",
398
409
  retryable: !0,
399
410
  capability: "tip"
400
411
  })
401
412
  );
402
- }, Te);
403
- }), K = (g) => {
404
- if (g.origin !== i || g.source !== d.contentWindow) return;
405
- const r = g.data;
413
+ }, Ce);
414
+ }), P = (m) => {
415
+ if (m.origin !== i || m.source !== c.contentWindow) return;
416
+ const r = m.data;
406
417
  if (!(typeof r != "object" || r === null || Array.isArray(r))) {
407
- if (P(r, Ie) && r.type === "viceme:widget-resize") {
408
- if (typeof r.workId != "string" || !be.test(r.workId) || !P(r.work, $) || r.work.id !== r.workId || !U(r.work.title) || typeof r.height != "number" || !Number.isInteger(r.height) || r.height < 1 || r.height > Ee || l !== void 0 && (r.work.id !== l.id || r.work.title !== l.title))
418
+ if (D(r, Ae) && r.type === "viceme:widget-resize") {
419
+ if (typeof r.workId != "string" || !ke.test(r.workId) || !D(r.work, J) || r.work.id !== r.workId || !j(r.work.title) || typeof r.height != "number" || !Number.isInteger(r.height) || r.height < 1 || r.height > Te || y !== void 0 && (r.work.id !== y.id || r.work.title !== y.title))
409
420
  return;
410
- d.style.height = `${r.height}px`, l === void 0 && (l = { id: r.workId, title: r.work.title }, d.style.pointerEvents = "auto", u !== void 0 && (a.clearTimeout(u), u = void 0), L?.());
421
+ s || (c.style.height = `${r.height}px`), y === void 0 && (y = { id: r.workId, title: r.work.title }, s || (c.style.pointerEvents = "auto"), w !== void 0 && (a.clearTimeout(w), w = void 0), f?.());
411
422
  return;
412
423
  }
413
- if (P(r, Ce) && r.type === "viceme:widget-close") {
414
- if (typeof r.workId != "string" || r.workId !== l?.id) return;
415
- F(t.target, "viceme:widget-close", { workId: r.workId });
424
+ if (D(r, _e) && r.type === "viceme:widget-close") {
425
+ if (typeof r.workId != "string" || r.workId !== y?.id) return;
426
+ g(!0), X(t.target, "viceme:widget-close", { workId: r.workId });
416
427
  return;
417
428
  }
418
- P(r, Ae) && r.type === "viceme:tip-paid" && r.workKey === e.workKey && P(r.work, $) && l !== void 0 && typeof r.work.id == "string" && r.work.id === l.id && U(r.work.title) && r.work.title === l.title && r.status === "PAID" && typeof r.amountCents == "number" && Number.isInteger(r.amountCents) && r.amountCents >= ve && r.amountCents <= ke && r.currency === "CNY" && F(t.target, "viceme:tip-paid", {
429
+ D(r, Se) && r.type === "viceme:tip-paid" && r.workKey === e.workKey && D(r.work, J) && y !== void 0 && typeof r.work.id == "string" && r.work.id === y.id && j(r.work.title) && r.work.title === y.title && r.status === "PAID" && typeof r.amountCents == "number" && Number.isInteger(r.amountCents) && r.amountCents >= Ee && r.amountCents <= Ie && r.currency === "CNY" && X(t.target, "viceme:tip-paid", {
419
430
  status: "PAID",
420
431
  work: { id: r.work.id, title: r.work.title },
421
432
  amountCents: r.amountCents,
422
433
  currency: "CNY"
423
434
  });
424
435
  }
425
- }, m = (g) => {
426
- y = g.matches ? "dark" : "light", d.style.colorScheme = y, d.contentWindow?.postMessage(
427
- { type: "viceme:widget-appearance", appearance: y },
436
+ }, _ = (m) => {
437
+ n = m.matches ? "dark" : "light", c.style.colorScheme = n, c.contentWindow?.postMessage(
438
+ { type: "viceme:widget-appearance", appearance: n },
428
439
  i
429
440
  );
430
441
  };
431
- let f;
432
- const T = () => {
433
- S?.(E()), k();
434
- }, k = () => {
435
- n || (n = !0, u !== void 0 && a.clearTimeout(u), a.removeEventListener("message", K), f?.(), t.signal?.removeEventListener("abort", T), w.remove());
442
+ let C;
443
+ const k = () => {
444
+ A?.(I()), L();
445
+ }, L = () => {
446
+ if (p) return;
447
+ p = !0, w !== void 0 && a.clearTimeout(w), a.removeEventListener("message", P), C?.();
448
+ const m = v;
449
+ g(!1), R?.(), R = void 0, m && V(e, t.target), t.signal?.removeEventListener("abort", k), u.remove();
436
450
  };
437
451
  try {
438
- if (a.addEventListener("message", K), c && (typeof c.addEventListener == "function" ? (c.addEventListener("change", m), f = () => c.removeEventListener("change", m)) : (c.addListener(m), f = () => c.removeListener(m))), t.signal?.addEventListener("abort", T, { once: !0 }), t.signal?.aborted) throw E();
439
- t.target.appendChild(w), await C;
440
- } catch (g) {
441
- throw k(), g;
452
+ if (a.addEventListener("message", P), d && (typeof d.addEventListener == "function" ? (d.addEventListener("change", _), C = () => d.removeEventListener("change", _)) : (d.addListener(_), C = () => d.removeListener(_))), t.signal?.addEventListener("abort", k, { once: !0 }), t.signal?.aborted) throw I();
453
+ if (t.target.appendChild(u), await O, p || t.signal?.aborted) throw I();
454
+ s && (R = re(e, t.target, {
455
+ open: Y
456
+ }));
457
+ } catch (m) {
458
+ throw L(), m;
442
459
  }
443
- return { capability: "tip", destroy: k };
460
+ return { capability: "tip", destroy: L };
444
461
  }
445
- const X = /* @__PURE__ */ new WeakMap();
446
- function Le(e) {
447
- return pe(e);
462
+ const Q = /* @__PURE__ */ new WeakMap();
463
+ function Pe(e) {
464
+ return ge(e);
448
465
  }
449
- function Ke(e, t) {
450
- let o = X.get(e);
451
- o || (o = /* @__PURE__ */ new WeakMap(), X.set(e, o));
466
+ function De(e, t) {
467
+ let o = Q.get(e);
468
+ o || (o = /* @__PURE__ */ new WeakMap(), Q.set(e, o));
452
469
  const a = o.get(t.target);
453
470
  if (a) return a;
454
- const i = _e(e, t).then((c) => {
455
- let y = !1;
471
+ const i = Oe(e, t).then((s) => {
472
+ let d = !1;
456
473
  const n = {
457
474
  capability: "tip",
458
475
  destroy() {
459
- y || (y = !0, t.signal?.removeEventListener("abort", n.destroy), o.get(t.target) === i && o.delete(t.target), c.destroy());
476
+ d || (d = !0, t.signal?.removeEventListener("abort", n.destroy), o.get(t.target) === i && o.delete(t.target), s.destroy());
460
477
  }
461
478
  };
462
479
  return t.signal?.addEventListener("abort", n.destroy, { once: !0 }), t.signal?.aborted && n.destroy(), n;
463
- }).catch((c) => {
464
- throw o.get(t.target) === i && o.delete(t.target), c;
480
+ }).catch((s) => {
481
+ throw o.get(t.target) === i && o.delete(t.target), s;
465
482
  });
466
483
  return o.set(t.target, i), i;
467
484
  }
468
485
  export {
469
- Le as createTip,
470
- Ke as mountTip
486
+ Pe as createTip,
487
+ De as mountTip
471
488
  };
472
489
  //# sourceMappingURL=tip.js.map