@lemoncloud/chatic-apps-sdk 0.1.0 → 0.3.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/index.js CHANGED
@@ -1,83 +1,156 @@
1
- import { C as w, b as L, s as I } from "./storage-BuvWwLx2.js";
2
- const P = "2.19.0", x = 1e4, V = 2e3;
3
- let W = 0;
4
- const M = () => `cas-${Date.now().toString(36)}-${(++W).toString(36)}`, E = () => typeof document < "u" && document.visibilityState === "hidden", N = (t, o = x) => {
5
- const r = /* @__PURE__ */ new Map(), i = /* @__PURE__ */ new Map();
6
- let l = !1;
7
- const v = (a, n) => {
8
- n.remaining === null || E() || (n.startedAt = Date.now(), n.timer = setTimeout(() => {
9
- r.delete(a), n.reject(new w("TIMEOUT", "The app did not answer in time."));
10
- }, n.remaining));
11
- }, f = () => {
12
- for (const [a, n] of r)
13
- n.remaining !== null && (E() ? (n.timer && clearTimeout(n.timer), n.timer = null, n.remaining = Math.max(0, n.remaining - (Date.now() - n.startedAt))) : n.timer || (n.remaining = Math.max(n.remaining, V), v(a, n)));
14
- };
15
- typeof document < "u" && document.addEventListener("visibilitychange", f);
16
- const h = t.listen((a) => {
17
- let n;
1
+ import { C as u, c as H, b as K, s as F } from "./storage-BXB76dd8.js";
2
+ import { S as fe } from "./storage-BXB76dd8.js";
3
+ const J = [1], z = Object.freeze({ $listener: !0 }), D = "data", Q = (e) => typeof e == "object" && e !== null && e.$listener === !0, X = (e) => {
4
+ if (e === null || typeof e != "object" || Array.isArray(e)) return null;
5
+ const t = e.$ended;
6
+ return t && typeof t == "object" ? t : null;
7
+ }, Y = (e, t, a) => {
8
+ let r = null, v = null, h = null, p = null, y = !1, g = 0;
9
+ const f = /* @__PURE__ */ new Map(), S = /* @__PURE__ */ new Set(), R = /* @__PURE__ */ new Set(), m = (o) => {
18
10
  try {
19
- n = JSON.parse(a);
20
- } catch {
21
- return;
11
+ o();
12
+ } catch (s) {
13
+ a(s);
22
14
  }
23
- if (!n || typeof n != "object" || typeof n.type != "string") return;
24
- const d = typeof n.refId == "string" ? r.get(n.refId) : void 0;
25
- if (d && typeof n.refId == "string") {
26
- if (r.delete(n.refId), d.timer && clearTimeout(d.timer), n.success === !0)
27
- d.resolve(n.data);
28
- else {
29
- const u = typeof n.error?.code == "string" ? n.error.code : "INTERNAL_ERROR", m = typeof n.error?.message == "string" ? n.error.message : u;
30
- d.reject(new w(u, m));
31
- }
15
+ }, A = () => {
16
+ y || (y = !0, R.forEach((o) => m(o)));
17
+ }, w = (o) => {
18
+ throw o instanceof u && o.code === "CLOUD_CHANGED" && A(), o;
19
+ }, n = (o) => {
20
+ const s = [...f.values()];
21
+ f.clear(), s.forEach((c) => m(() => c.onEnd?.(o)));
22
+ }, i = e.onPush("OnRuntimeEvent", (o) => {
23
+ const { subscriptionId: s, event: c } = o ?? {}, E = typeof s == "string" ? f.get(s) : void 0;
24
+ if (!E || typeof s != "string") return;
25
+ const b = X(c);
26
+ if (b) {
27
+ f.delete(s);
28
+ const L = typeof b.code == "string" ? b.code : "INTERNAL_ERROR", B = typeof b.message == "string" ? b.message : L;
29
+ L === "CLOUD_CHANGED" && A(), m(() => E.onEnd?.(new u(L, B)));
32
30
  return;
33
31
  }
34
- i.get(n.type)?.forEach((u) => {
35
- try {
36
- u(n.data);
37
- } catch {
32
+ m(() => E.listener(...Array.isArray(c) ? c : [c]));
33
+ }), d = e.onPush("OnRuntimeSessionChanged", (o) => {
34
+ const { seq: s, session: c } = o ?? {};
35
+ !h || typeof s != "number" || s <= h.seq || (h = { ...h, seq: s }, p = c ?? null, S.forEach((E) => m(() => E(p))));
36
+ }), l = () => {
37
+ if (!t())
38
+ return Promise.reject(
39
+ new u("NOT_SUPPORTED", "This app does not answer this page's cloud data requests.")
40
+ );
41
+ if (!r) {
42
+ const o = new Promise((c, E) => {
43
+ v = E;
44
+ }), s = Promise.race([
45
+ e.request("RuntimeHello", { contracts: [...J] }, { timeoutMs: null }),
46
+ o
47
+ ]).then((c) => (r === s && (h = c, p = c.session ?? null), c)).catch((c) => (r === s && (r = null), w(c)));
48
+ r = s;
49
+ }
50
+ return r;
51
+ }, P = async (o, s) => {
52
+ if (!(await l()).allowed[o].includes(s))
53
+ throw new u(
54
+ "RUNTIME_NOT_ALLOWED",
55
+ `${s} is not one this page may ${o === "calls" ? "call" : "subscribe to"}.`
56
+ );
57
+ }, C = async (o, ...s) => (await P("calls", o), (await e.request("RuntimeCall", { target: D, method: o, args: s }).catch(w)).value), k = async (o, s, c, E) => {
58
+ await P("streams", o);
59
+ const b = `cas-s${(++g).toString(36)}`;
60
+ f.set(b, { listener: c, onEnd: E });
61
+ try {
62
+ await e.request("RuntimeSubscribe", {
63
+ subscriptionId: b,
64
+ target: D,
65
+ topic: o,
66
+ args: s.some(Q) ? s : [...s, z]
67
+ });
68
+ } catch (L) {
69
+ f.delete(b), w(L);
70
+ }
71
+ return {
72
+ stop: async () => {
73
+ f.delete(b) && await e.request("RuntimeUnsubscribe", { subscriptionId: b }).catch(() => {
74
+ });
38
75
  }
39
- });
40
- });
76
+ };
77
+ }, q = async () => {
78
+ await l();
79
+ const o = p?.committedSiteId;
80
+ if (!o)
81
+ throw new u(
82
+ "NOT_SUPPORTED",
83
+ "A place profile needs the session permission, while the session is on a place."
84
+ );
85
+ return o;
86
+ }, _ = async () => {
87
+ await l();
88
+ const o = p?.context.identity.userId;
89
+ if (!o) throw new u("NOT_SUPPORTED", "The user's own profile needs the session permission.");
90
+ return o;
91
+ }, V = async (o) => `${await q()}@${o}`, U = async (o) => C("profile.cacheRead", await V(o)), x = async (o, s, c) => k(
92
+ "profile.observeItem",
93
+ [await V(o)],
94
+ s,
95
+ c?.onEnd
96
+ );
41
97
  return {
42
- request: (a, n = {}, d = {}) => new Promise((u, m) => {
43
- if (l) {
44
- m(new w("DISCONNECTED", "The connection to the app was closed."));
45
- return;
46
- }
47
- const p = M(), e = d.timeoutMs === void 0 ? o : d.timeoutMs, s = {
48
- resolve: u,
49
- reject: m,
50
- remaining: e,
51
- startedAt: Date.now(),
52
- timer: null
53
- };
54
- r.set(p, s), v(p, s), t.send(JSON.stringify({ type: a, data: n, refId: p, version: P }));
55
- }),
56
- onPush: (a, n) => {
57
- let d = i.get(a);
58
- return d || i.set(a, d = /* @__PURE__ */ new Set()), d.add(n), () => d?.delete(n);
98
+ connect: () => l().then(({ contract: o, allowed: s }) => ({ contract: o, allowed: s })),
99
+ can: (o) => !!h && (h.allowed.calls.includes(o) || h.allowed.streams.includes(o)),
100
+ call: C,
101
+ subscribe: k,
102
+ sendMessage: (o, s) => C("chat.sendChat", { channelId: o, content: s }),
103
+ react: (o, s, c) => C("chat.setReaction", { chatId: o, emoji: s, action: c ? "on" : "off" }),
104
+ placeProfile: U,
105
+ placeProfiles: async () => C("profile.cacheReadList", { sid: await q() }),
106
+ observePlaceProfile: x,
107
+ myProfile: async () => U(await _()),
108
+ observeMyProfile: async (o, s) => x(await _(), o, s),
109
+ session: () => p,
110
+ onSessionChange: (o) => (S.add(o), () => S.delete(o)),
111
+ onCloudChanged: (o) => (R.add(o), () => R.delete(o)),
112
+ // A page restored from the back-forward cache meets a new host: its streams are gone there.
113
+ reset: () => {
114
+ v?.(
115
+ new u("DISCONNECTED", "The page was restored before the app answered. Connect again.")
116
+ ), v = null, r = null, h = null, p = null, n(new u("DISCONNECTED", "The page was restored; its streams ended. Subscribe again."));
59
117
  },
60
- close: () => {
61
- if (!l) {
62
- l = !0, h(), typeof document < "u" && document.removeEventListener("visibilitychange", f);
63
- for (const a of r.values())
64
- a.timer && clearTimeout(a.timer), a.reject(new w("DISCONNECTED", "The connection to the app was closed."));
65
- r.clear(), i.clear();
66
- }
118
+ dispose: () => {
119
+ i(), d(), f.clear();
120
+ }
121
+ };
122
+ }, Z = () => {
123
+ const e = () => Promise.reject(new u("NOT_IN_APP", "The app's data is read only in the app."));
124
+ return {
125
+ connect: e,
126
+ can: () => !1,
127
+ call: e,
128
+ subscribe: e,
129
+ sendMessage: e,
130
+ react: e,
131
+ placeProfile: e,
132
+ placeProfiles: e,
133
+ observePlaceProfile: e,
134
+ myProfile: e,
135
+ observeMyProfile: e,
136
+ session: () => null,
137
+ onSessionChange: () => () => {
138
+ },
139
+ onCloudChanged: () => () => {
67
140
  }
68
141
  };
69
- }, U = () => typeof window < "u" && !!window.ReactNativeWebView, _ = () => ({
70
- send: (t) => window.ReactNativeWebView?.postMessage(t),
71
- listen: (t) => {
72
- const o = (r) => {
73
- const i = r.data;
74
- typeof i == "string" && t(i);
142
+ }, ee = () => typeof window < "u" && !!window.ReactNativeWebView, te = () => ({
143
+ send: (e) => window.ReactNativeWebView?.postMessage(e),
144
+ listen: (e) => {
145
+ const t = (a) => {
146
+ const r = a.data;
147
+ typeof r == "string" && e(r);
75
148
  };
76
- return window.addEventListener("message", o), document.addEventListener("message", o), () => {
77
- window.removeEventListener("message", o), document.removeEventListener("message", o);
149
+ return window.addEventListener("message", t), document.addEventListener("message", t), () => {
150
+ window.removeEventListener("message", t), document.removeEventListener("message", t);
78
151
  };
79
152
  }
80
- }), b = {
153
+ }), O = {
81
154
  finish: "FinishSubWebView",
82
155
  title: "SetSubWebViewTitle",
83
156
  haptic: "TriggerHaptic",
@@ -88,201 +161,257 @@ const M = () => `cas-${Date.now().toString(36)}-${(++W).toString(36)}`, E = () =
88
161
  back: "SetCanGoBack",
89
162
  closeGuard: "SetSubWebViewCloseGuard",
90
163
  errorReport: "ReportSubWebViewError",
91
- storage: "GetSubWebViewStorage"
92
- }, T = 1, j = 1e3, q = 4e3, R = () => typeof window < "u" && window.ChaticSubWebView || null, D = () => new Promise((t) => {
164
+ storage: "GetSubWebViewStorage",
165
+ permissions: "RequestSubWebViewPermission",
166
+ data: "RuntimeHello"
167
+ }, W = 1, ne = 1e3, oe = 4e3, $ = () => typeof window < "u" && window.ChaticSubWebView || null, re = () => new Promise((e) => {
93
168
  if (typeof document > "u" || document.readyState === "complete") {
94
- t(R());
169
+ e($());
95
170
  return;
96
171
  }
97
- window.addEventListener("load", () => t(R()), { once: !0 });
98
- }), G = (t) => {
99
- let o, r;
172
+ window.addEventListener("load", () => e($()), { once: !0 });
173
+ }), se = (e) => {
174
+ let t, a;
100
175
  try {
101
- t instanceof Error ? (o = t.message || t.name, r = typeof t.stack == "string" ? t.stack : void 0) : typeof t == "string" ? o = t : o = `A non-Error value was thrown: ${Object.prototype.toString.call(t)}`;
176
+ e instanceof Error ? (t = e.message || e.name, a = typeof e.stack == "string" ? e.stack : void 0) : typeof e == "string" ? t = e : t = `A non-Error value was thrown: ${Object.prototype.toString.call(e)}`;
102
177
  } catch {
103
- o = "An error that could not be read was thrown";
178
+ t = "An error that could not be read was thrown";
104
179
  }
105
180
  return {
106
- message: (o || "Unknown error").slice(0, j),
107
- ...r ? { stack: r.slice(0, q) } : {}
181
+ message: (t || "Unknown error").slice(0, ne),
182
+ ...a ? { stack: a.slice(0, oe) } : {}
108
183
  };
109
- }, S = (t) => new w("NOT_SUPPORTED", `${t} is not available here: an older app, or not granted.`), O = (t) => Promise.reject(new w("NOT_IN_APP", `${t} needs the app; this page is in a browser.`)), C = (t = {}) => {
110
- if (t === null || typeof t != "object" || Array.isArray(t))
111
- throw new w("INVALID", "link() takes an object of query params, such as { level: 3 }.");
112
- const o = new URLSearchParams();
113
- for (const [i, l] of Object.entries(t)) o.append(i, String(l));
114
- const r = o.toString();
115
- return r ? `?${r}` : "";
116
- }, k = (t) => typeof location > "u" ? `/${t}` : `${location.origin}/${t}`;
117
- let y = null;
118
- const H = (t = {}) => (y || (y = (U() ? $(_(), t) : Promise.resolve(K())).catch((o) => {
119
- throw y = null, o;
120
- })), y), $ = async (t, o = {}) => {
121
- const r = N(t, o.timeoutMs);
184
+ }, I = (e) => new u("NOT_SUPPORTED", `${e} is not available here: an older app, or not granted.`), N = (e) => Promise.reject(new u("NOT_IN_APP", `${e} needs the app; this page is in a browser.`)), j = (e = {}) => {
185
+ if (e === null || typeof e != "object" || Array.isArray(e))
186
+ throw new u("INVALID", "link() takes an object of query params, such as { level: 3 }.");
187
+ const t = new URLSearchParams();
188
+ for (const [r, v] of Object.entries(e)) t.append(r, String(v));
189
+ const a = t.toString();
190
+ return a ? `?${a}` : "";
191
+ }, G = (e) => typeof location > "u" ? `/${e}` : `${location.origin}/${e}`;
192
+ let T = null;
193
+ const ue = (e = {}) => (T || (T = (ee() ? ae(te(), e) : Promise.resolve(de())).catch((t) => {
194
+ throw T = null, t;
195
+ })), T), ae = async (e, t = {}) => {
196
+ const a = H(e, t.timeoutMs);
122
197
  try {
123
- const i = await A(r, !0);
124
- return B(r, i, o);
125
- } catch (i) {
126
- throw r.close(), i;
198
+ const r = await M(a, !0);
199
+ return ie(a, r, t);
200
+ } catch (r) {
201
+ throw a.close(), r;
127
202
  }
128
- }, A = async (t, o) => {
129
- const r = await t.request("WebAppReady", {}), i = r.subWebView ?? (o ? await D() : null);
130
- if (i && i.protocolVersion !== T)
131
- throw new w(
203
+ }, M = async (e, t) => {
204
+ const a = await e.request("WebAppReady", {}), r = a.subWebView ?? (t ? await re() : null);
205
+ if (r && r.protocolVersion !== W)
206
+ throw new u(
132
207
  "UNSUPPORTED_PROTOCOL",
133
- `The app speaks protocol ${String(i.protocolVersion)}; this library speaks ${T}.`
208
+ `The app speaks protocol ${String(r.protocolVersion)}; this library speaks ${W}.`
134
209
  );
135
210
  return {
136
- supported: new Set(Array.isArray(r.supportedWebMessages) ? r.supportedWebMessages : []),
137
- context: i
211
+ supported: new Set(Array.isArray(a.supportedWebMessages) ? a.supportedWebMessages : []),
212
+ context: r
138
213
  };
139
- }, B = (t, o, r) => {
140
- let i = null, l = null;
141
- const v = /* @__PURE__ */ new Set(), f = [], h = (e) => o.supported.has(b[e]), a = (e, s, c) => h(e) ? t.request(b[e], s, c === void 0 ? {} : { timeoutMs: c }) : Promise.reject(S(e)), n = (e, s) => h("storage") ? t.request(e, s) : Promise.reject(S("storage")), d = (e) => v.forEach((s) => {
214
+ }, ie = (e, t, a) => {
215
+ let r = null, v = null;
216
+ const h = /* @__PURE__ */ new Set(), p = [], y = (n) => t.supported.has(O[n]), g = (n, i, d, l) => {
217
+ const P = l ?? O[n];
218
+ return t.supported.has(P) ? e.request(P, i, d === void 0 ? {} : { timeoutMs: d }) : Promise.reject(I(n));
219
+ }, f = Y(
220
+ e,
221
+ () => y("data"),
222
+ (n) => {
223
+ w.reportError(n);
224
+ }
225
+ );
226
+ p.push(() => f.dispose());
227
+ const S = (n, i) => y("storage") ? e.request(n, i) : Promise.reject(I("storage")), R = (n) => h.forEach((i) => {
142
228
  try {
143
- s(e);
144
- } catch (c) {
145
- p.reportError(c);
229
+ i(n);
230
+ } catch (d) {
231
+ w.reportError(d);
146
232
  }
147
- }), u = () => {
148
- h("back") && t.request(b.back, { canGoBack: !!i }).catch(() => {
149
- }), h("closeGuard") && t.request(b.closeGuard, { enabled: !!l }).catch(() => {
233
+ }), m = () => {
234
+ y("back") && e.request(O.back, { canGoBack: !!r }).catch(() => {
235
+ }), y("closeGuard") && e.request(O.closeGuard, { enabled: !!v }).catch(() => {
150
236
  });
151
- }, m = (e) => {
152
- if (!e.persisted) {
153
- u();
237
+ }, A = (n) => {
238
+ if (!n.persisted) {
239
+ m();
154
240
  return;
155
241
  }
156
- A(t, !1).then((s) => {
157
- const c = o.context;
158
- o.supported = s.supported, s.context && (o.context = s.context), u();
159
- const g = o.context;
160
- g && (g.theme !== c?.theme || g.locale !== c?.locale) && d(g);
242
+ M(e, !1).then((i) => {
243
+ const d = t.context;
244
+ t.supported = i.supported, i.context && (t.context = i.context), m(), f.reset();
245
+ const l = t.context, P = l && (l.theme !== d?.theme || l.locale !== d?.locale || JSON.stringify(l.safeArea) !== JSON.stringify(d?.safeArea));
246
+ l && P && R(l);
161
247
  }).catch(() => {
162
248
  });
163
249
  };
164
- typeof window < "u" && (window.addEventListener("pageshow", m), f.push(() => window.removeEventListener("pageshow", m))), f.push(
165
- t.onPush("OnBackPressed", () => {
250
+ typeof window < "u" && (window.addEventListener("pageshow", A), p.push(() => window.removeEventListener("pageshow", A))), p.push(
251
+ e.onPush("OnBackPressed", () => {
166
252
  try {
167
- i?.();
168
- } catch (e) {
169
- p.reportError(e);
253
+ r?.();
254
+ } catch (n) {
255
+ w.reportError(n);
170
256
  }
171
257
  })
172
- ), f.push(
173
- t.onPush("OnSubWebViewCloseRequested", (e) => {
174
- const s = l;
175
- if (!s) return;
176
- const c = e?.trigger ?? "control";
177
- Promise.resolve().then(() => s(c)).then((g) => g ? p.finish() : void 0).catch((g) => p.reportError(g));
258
+ ), p.push(
259
+ e.onPush("OnSubWebViewCloseRequested", (n) => {
260
+ const i = v;
261
+ if (!i) return;
262
+ const d = n?.trigger ?? "control";
263
+ Promise.resolve().then(() => i(d)).then((l) => l ? w.finish() : void 0).catch((l) => w.reportError(l));
178
264
  })
179
- ), f.push(
180
- t.onPush("OnSubWebViewContextChanged", (e) => {
181
- o.context && (o.context = { ...o.context, ...e }, d(o.context));
265
+ ), p.push(
266
+ e.onPush("OnSubWebViewContextChanged", (n) => {
267
+ t.context && (t.context = { ...t.context, ...n }, R(t.context));
182
268
  })
183
269
  );
184
- const p = {
270
+ const w = {
185
271
  inApp: !0,
186
272
  get context() {
187
- return o.context;
273
+ return t.context;
188
274
  },
189
- supports: h,
190
- finish: (e) => a("finish", e === void 0 ? {} : { result: e }).then(() => {
275
+ supports: y,
276
+ finish: (n) => g("finish", n === void 0 ? {} : { result: n }).then(() => {
191
277
  }),
192
- setTitle: (e) => a("title", { title: e }).then(() => {
278
+ setTitle: (n) => g("title", { title: n }).then(() => {
193
279
  }),
194
- haptic: (e) => {
195
- h("haptic") && t.request(b.haptic, { kind: e }).catch(() => {
280
+ haptic: (n) => {
281
+ y("haptic") && e.request(O.haptic, { kind: n }).catch(() => {
196
282
  });
197
283
  },
198
- copy: (e) => a("clipboard", { text: e }).then(() => {
284
+ copy: (n) => g("clipboard", { text: n }).then(() => {
199
285
  }),
200
- openUrl: (e) => a("openUrl", { url: e }).then(() => {
286
+ openUrl: (n) => g("openUrl", { url: n }).then(() => {
201
287
  }),
202
- share: (e) => a("share", e, null).then(
203
- (s) => s?.action === "dismissedAction" ? "dismissed" : "shared"
288
+ share: (n) => g("share", n, null).then(
289
+ (i) => i?.action === "dismissedAction" ? "dismissed" : "shared"
204
290
  ),
205
- getUserKey: () => a("userKey").then((e) => e.key),
206
- reportError: (e) => h("errorReport") ? t.request(b.errorReport, G(e)).then((s) => !!s?.recorded).catch(() => !1) : Promise.resolve(!1),
207
- storage: I({
208
- get: (e) => n("GetSubWebViewStorage", { keys: e }),
209
- set: (e) => n("SetSubWebViewStorage", { entries: e }),
210
- remove: (e) => n("RemoveSubWebViewStorage", { keys: e }),
211
- clear: () => n("ClearSubWebViewStorage", {}),
212
- info: () => n("GetSubWebViewStorageInfo", {})
291
+ getUserKey: () => g("userKey").then((n) => n.key),
292
+ permissions: {
293
+ query: (n) => g("permissions", { kind: n }, void 0, "GetSubWebViewPermission").then(
294
+ (i) => i.state
295
+ ),
296
+ // Waits for the user, however long they take.
297
+ request: (n) => g("permissions", { kind: n }, null).then((i) => i.state)
298
+ },
299
+ openSettings: () => g("permissions", {}, void 0, "OpenSettings").then(() => {
213
300
  }),
214
- onBack: (e) => {
215
- i = e, u();
301
+ reportError: (n) => y("errorReport") ? e.request(O.errorReport, se(n)).then((i) => !!i?.recorded).catch(() => !1) : Promise.resolve(!1),
302
+ data: f,
303
+ storage: F({
304
+ get: (n) => S("GetSubWebViewStorage", { keys: n }),
305
+ set: (n) => S("SetSubWebViewStorage", { entries: n }),
306
+ remove: (n) => S("RemoveSubWebViewStorage", { keys: n }),
307
+ clear: () => S("ClearSubWebViewStorage", {}),
308
+ info: () => S("GetSubWebViewStorageInfo", {})
309
+ }),
310
+ onBack: (n) => {
311
+ r = n, m();
216
312
  },
217
- onCloseRequest: (e) => {
218
- l = e, u();
313
+ onCloseRequest: (n) => {
314
+ v = n, m();
219
315
  },
220
- onContextChange: (e) => (v.add(e), () => v.delete(e)),
221
- link: (e) => {
222
- const s = C(e), c = o.context?.appLink;
223
- return c ? `${c}${s}` : k(s);
316
+ onContextChange: (n) => (h.add(n), () => h.delete(n)),
317
+ link: (n) => {
318
+ const i = j(n), d = t.context?.appLink;
319
+ return d ? `${d}${i}` : G(i);
224
320
  },
225
321
  disconnect: () => {
226
- f.forEach((e) => e()), t.close(), y = null;
322
+ p.forEach((n) => n()), e.close(), T = null;
227
323
  }
228
324
  };
229
- if (r.reportErrors !== !1 && typeof window < "u") {
230
- const e = (c) => {
231
- p.reportError(c.error ?? c.message);
232
- }, s = (c) => {
233
- p.reportError(c.reason);
325
+ if (a.reportErrors !== !1 && typeof window < "u") {
326
+ const n = (d) => {
327
+ w.reportError(d.error ?? d.message);
328
+ }, i = (d) => {
329
+ w.reportError(d.reason);
234
330
  };
235
- window.addEventListener("error", e), window.addEventListener("unhandledrejection", s), f.push(() => {
236
- window.removeEventListener("error", e), window.removeEventListener("unhandledrejection", s);
331
+ window.addEventListener("error", n), window.addEventListener("unhandledrejection", i), p.push(() => {
332
+ window.removeEventListener("error", n), window.removeEventListener("unhandledrejection", i);
237
333
  });
238
334
  }
239
- return p;
240
- }, K = () => {
241
- const t = (o) => typeof navigator < "u" && navigator.clipboard ? navigator.clipboard.writeText(o) : Promise.reject(S("The clipboard"));
335
+ return w;
336
+ }, ce = () => {
337
+ const e = async (t) => {
338
+ try {
339
+ const a = await navigator.permissions.query({ name: t });
340
+ return a.state === "granted" ? "granted" : a.state === "denied" ? "blocked" : "prompt";
341
+ } catch {
342
+ return "prompt";
343
+ }
344
+ };
345
+ return {
346
+ query: e,
347
+ request: async (t) => {
348
+ if (typeof navigator > "u" || !navigator.mediaDevices?.getUserMedia)
349
+ throw new u("NOT_SUPPORTED", `This browser cannot capture the ${t}.`);
350
+ try {
351
+ return (await navigator.mediaDevices.getUserMedia(
352
+ t === "camera" ? { video: !0 } : { audio: !0 }
353
+ )).getTracks().forEach((r) => r.stop()), "granted";
354
+ } catch (a) {
355
+ const r = a?.name;
356
+ if (r === "NotFoundError" || r === "OverconstrainedError")
357
+ throw new u("UNAVAILABLE", `This device has no ${t}.`);
358
+ if (r === "NotReadableError" || r === "SecurityError" || r === "TypeError" || r === "AbortError")
359
+ throw new u("INTERNAL_ERROR", `The ${t} could not be used: ${r}.`);
360
+ return await e(t) === "blocked" ? "blocked" : "prompt";
361
+ }
362
+ }
363
+ };
364
+ }, de = () => {
365
+ const e = (t) => typeof navigator < "u" && navigator.clipboard ? navigator.clipboard.writeText(t) : Promise.reject(I("The clipboard"));
242
366
  return {
243
367
  inApp: !1,
244
368
  context: null,
245
369
  supports: () => !1,
246
- finish: () => O("Finishing the view"),
247
- setTitle: async (o) => {
248
- typeof document < "u" && (document.title = o);
370
+ finish: () => N("Finishing the view"),
371
+ setTitle: async (t) => {
372
+ typeof document < "u" && (document.title = t);
249
373
  },
250
374
  haptic: () => {
251
375
  typeof navigator < "u" && navigator.vibrate?.(10);
252
376
  },
253
- copy: t,
254
- openUrl: async (o) => {
255
- typeof window < "u" && window.open(o, "_blank", "noopener,noreferrer");
377
+ copy: e,
378
+ openUrl: async (t) => {
379
+ typeof window < "u" && window.open(t, "_blank", "noopener,noreferrer");
256
380
  },
257
- share: async (o) => {
258
- const r = [o.message, o.url].filter(Boolean).join(" ");
259
- if (!r) throw new w("INVALID", "Share a message or a url.");
381
+ share: async (t) => {
382
+ const a = [t.message, t.url].filter(Boolean).join(" ");
383
+ if (!a) throw new u("INVALID", "Share a message or a url.");
260
384
  if (typeof navigator < "u" && navigator.share)
261
385
  try {
262
- return await navigator.share(o), "shared";
263
- } catch (i) {
264
- if (i?.name === "AbortError") return "dismissed";
386
+ return await navigator.share(t), "shared";
387
+ } catch (r) {
388
+ if (r?.name === "AbortError") return "dismissed";
265
389
  }
266
- return await t(r), "copied";
390
+ return await e(a), "copied";
267
391
  },
268
- getUserKey: () => O("A user key"),
269
- reportError: async (o) => (console.error(o), !1),
270
- storage: L(),
392
+ getUserKey: () => N("A user key"),
393
+ permissions: ce(),
394
+ data: Z(),
395
+ openSettings: () => N("Opening the settings"),
396
+ reportError: async (t) => (console.error(t), !1),
397
+ storage: K(),
271
398
  onBack: () => {
272
399
  },
273
400
  onCloseRequest: () => {
274
401
  },
275
402
  onContextChange: () => () => {
276
403
  },
277
- link: (o) => k(C(o)),
404
+ link: (t) => G(j(t)),
278
405
  disconnect: () => {
279
- y = null;
406
+ T = null;
280
407
  }
281
408
  };
282
409
  };
283
410
  export {
284
- w as ChaticAppsError,
285
- P as SDK_BRIDGE_VERSION,
286
- H as connect,
287
- U as isInApp
411
+ u as ChaticAppsError,
412
+ z as LISTENER,
413
+ fe as SDK_BRIDGE_VERSION,
414
+ J as SDK_DATA_CONTRACTS,
415
+ ue as connect,
416
+ ee as isInApp
288
417
  };