@salla.sa/embedded-sdk 0.1.0-beta.2 → 0.1.0-beta.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/esm/index.js CHANGED
@@ -1,178 +1,174 @@
1
- const _ = "0.1.0-beta.2", M = {
2
- version: _
3
- }, n = "embedded::", y = {
1
+ const $ = "0.1.0-beta.4", k = {
2
+ version: $
3
+ }, s = "embedded::", g = {
4
4
  /** Initialize handshake - iframe signals it's ready to receive context */
5
- INIT: `${n}iframe.ready`,
5
+ INIT: `${s}iframe.ready`,
6
6
  /** Request iframe resize */
7
- RESIZE: `${n}iframe.resize`,
7
+ RESIZE: `${s}iframe.resize`,
8
8
  /** App signals it's fully loaded and ready */
9
- READY: `${n}ready`
10
- }, S = {
9
+ READY: `${s}ready`,
10
+ /** App requests to be destroyed and navigate away */
11
+ DESTROY: `${s}destroy`
12
+ }, w = {
11
13
  /** Context data provision */
12
- PROVIDE: `${n}context.provide`,
14
+ PROVIDE: `${s}context.provide`,
13
15
  /** Theme change notification */
14
- THEME_CHANGE: `${n}theme.change`
15
- }, k = {
16
+ THEME_CHANGE: `${s}theme.change`
17
+ }, _ = {
16
18
  /** Send log message to host */
17
- LOG: `${n}log`
18
- }, v = {
19
- /** Request logout and redirect to apps page */
20
- LOGOUT: `${n}auth.logout`,
19
+ LOG: `${s}log`
20
+ }, M = {
21
21
  /** Request token refresh (re-renders iframe with new token) */
22
- REFRESH: `${n}auth.refresh`,
23
- /** Signal auth error to host */
24
- ERROR: `${n}auth.error`
22
+ REFRESH: `${s}auth.refresh`
25
23
  }, h = {
26
24
  /** Navigate using React Router (SPA navigation) */
27
- NAVIGATE: `${n}page.navigate`,
25
+ NAVIGATE: `${s}page.navigate`,
28
26
  /** Redirect using window.location (full page reload) */
29
- REDIRECT: `${n}page.redirect`,
27
+ REDIRECT: `${s}page.redirect`,
30
28
  /** Set page title */
31
- SET_TITLE: `${n}page.setTitle`
32
- }, E = {
29
+ SET_TITLE: `${s}page.setTitle`
30
+ }, b = {
33
31
  /** Set primary action button in navbar */
34
- SET_ACTION: `${n}nav.setAction`,
32
+ SET_ACTION: `${s}nav.setAction`,
35
33
  /** Notification when action button is clicked (host → iframe) */
36
- ACTION_CLICK: `${n}nav.actionClick`
37
- }, l = {
34
+ ACTION_CLICK: `${s}nav.actionClick`
35
+ }, u = {
38
36
  /** Set loading state */
39
- LOADING: `${n}ui.loading`,
40
- /** Control overlay state */
41
- OVERLAY: `${n}ui.overlay`,
37
+ LOADING: `${s}ui.loading`,
42
38
  /** Show toast notification */
43
- TOAST: `${n}ui.toast`,
39
+ TOAST: `${s}ui.toast`,
44
40
  /** Control modal state */
45
- MODAL: `${n}ui.modal`,
41
+ MODAL: `${s}ui.modal`,
46
42
  /** Show confirm dialog (async request) */
47
- CONFIRM: `${n}ui.confirm`,
43
+ CONFIRM: `${s}ui.confirm`,
48
44
  /** Confirm dialog response (from host) */
49
- CONFIRM_RESPONSE: `${n}ui.confirm.response`,
45
+ CONFIRM_RESPONSE: `${s}ui.confirm.response`,
50
46
  /** Modal response (from host) */
51
- MODAL_RESPONSE: `${n}ui.modal.response`
47
+ MODAL_RESPONSE: `${s}ui.modal.response`
52
48
  }, p = {
53
49
  /** Initialize checkout flow */
54
- CREATE: `${n}checkout.create`
55
- }, b = M.version, x = 1e4, V = [
50
+ CREATE: `${s}checkout.create`
51
+ }, y = k.version, x = 1e4, z = [
56
52
  "localhost",
57
53
  "merchants.workers.dev",
58
54
  "s.salla.sa",
59
55
  ".salla.group",
60
56
  ".salla.sa"
61
57
  ];
62
- function U(t) {
58
+ function K(e) {
63
59
  try {
64
- const i = new URL(t).hostname;
65
- return V.some((r) => r.startsWith(".") ? i.endsWith(r) || i === r.slice(1) : i === r || i.startsWith(`${r}:`));
60
+ const n = new URL(e).hostname;
61
+ return z.some((i) => i.startsWith(".") ? n.endsWith(i) || n === i.slice(1) : n === i || n.startsWith(`${i}:`));
66
62
  } catch {
67
63
  return !1;
68
64
  }
69
65
  }
70
- function z() {
66
+ function V() {
71
67
  return typeof window > "u" || window.parent === window ? null : window.parent;
72
68
  }
73
- function s(t, e, i = "*") {
74
- const r = z();
75
- if (!r) {
69
+ function o(e, t, n = "*") {
70
+ const i = V();
71
+ if (!i) {
76
72
  console.warn("[EmbeddedSDK] Not running in an iframe, cannot post to host");
77
73
  return;
78
74
  }
79
- const a = {
80
- event: t,
81
- ...e
75
+ const r = {
76
+ event: e,
77
+ ...t
82
78
  };
83
- r.postMessage(a, i);
79
+ i.postMessage(r, n);
84
80
  }
85
81
  const d = /* @__PURE__ */ new Map();
86
82
  let T = !1;
87
- function O(t) {
88
- if (process.env.NODE_ENV === "production" && !U(t.origin))
83
+ function L(e) {
84
+ if (process.env.NODE_ENV === "production" && !K(e.origin))
89
85
  return;
90
- const e = t.data;
91
- if (!e || typeof e.event != "string")
86
+ const t = e.data;
87
+ if (!t || typeof t.event != "string")
92
88
  return;
93
- const i = d.get(e.event);
94
- i && i.forEach((a) => {
89
+ const n = d.get(t.event);
90
+ n && n.forEach((r) => {
95
91
  try {
96
- a(e);
97
- } catch (o) {
98
- console.error("[EmbeddedSDK] Error in message handler:", o);
92
+ r(t);
93
+ } catch (l) {
94
+ console.error("[EmbeddedSDK] Error in message handler:", l);
99
95
  }
100
96
  });
101
- const r = d.get("*");
102
- r && r.forEach((a) => {
97
+ const i = d.get("*");
98
+ i && i.forEach((r) => {
103
99
  try {
104
- a(e);
105
- } catch (o) {
106
- console.error("[EmbeddedSDK] Error in wildcard handler:", o);
100
+ r(t);
101
+ } catch (l) {
102
+ console.error("[EmbeddedSDK] Error in wildcard handler:", l);
107
103
  }
108
104
  });
109
105
  }
110
- function K() {
111
- T || typeof window > "u" || (window.addEventListener("message", O), T = !0);
106
+ function U() {
107
+ T || typeof window > "u" || (window.addEventListener("message", L), T = !0);
112
108
  }
113
- function g(t, e) {
114
- K(), d.has(t) || d.set(t, /* @__PURE__ */ new Set());
115
- const i = d.get(t);
116
- return i.add(e), () => {
117
- i.delete(e), i.size === 0 && d.delete(t);
109
+ function E(e, t) {
110
+ U(), d.has(e) || d.set(e, /* @__PURE__ */ new Set());
111
+ const n = d.get(e);
112
+ return n.add(t), () => {
113
+ n.delete(t), n.size === 0 && d.delete(e);
118
114
  };
119
115
  }
120
- function F(t, e = x) {
121
- return new Promise((i, r) => {
122
- const a = setTimeout(() => {
123
- o(), r(new Error(`[EmbeddedSDK] Timeout waiting for "${t}" message`));
124
- }, e), o = g(t, (u) => {
125
- clearTimeout(a), o(), i(u);
116
+ function F(e, t = x) {
117
+ return new Promise((n, i) => {
118
+ const r = setTimeout(() => {
119
+ l(), i(new Error(`[EmbeddedSDK] Timeout waiting for "${e}" message`));
120
+ }, t), l = E(e, (a) => {
121
+ clearTimeout(r), l(), n(a);
126
122
  });
127
123
  });
128
124
  }
129
125
  function q() {
130
- d.clear(), T && typeof window < "u" && (window.removeEventListener("message", O), T = !1);
126
+ d.clear(), T && typeof window < "u" && (window.removeEventListener("message", L), T = !1);
131
127
  }
132
128
  function P() {
133
129
  return typeof window > "u" ? !1 : window.parent !== window;
134
130
  }
135
131
  const f = /* @__PURE__ */ new Map(), G = 3e4;
136
132
  function H() {
137
- const t = Date.now(), e = Math.random().toString(36).slice(2, 9);
138
- return `req_${t}_${e}`;
139
- }
140
- function j(t, e = {}, i = G) {
141
- const r = H();
142
- return new Promise((a, o) => {
143
- const u = setTimeout(() => {
144
- f.get(r) && (f.delete(r), o(
133
+ const e = Date.now(), t = Math.random().toString(36).slice(2, 9);
134
+ return `req_${e}_${t}`;
135
+ }
136
+ function j(e, t = {}, n = G) {
137
+ const i = H();
138
+ return new Promise((r, l) => {
139
+ const a = setTimeout(() => {
140
+ f.get(i) && (f.delete(i), l(
145
141
  new Error(
146
- `[EmbeddedSDK] Request "${t}" timed out after ${i}ms`
142
+ `[EmbeddedSDK] Request "${e}" timed out after ${n}ms`
147
143
  )
148
144
  ));
149
- }, i);
150
- f.set(r, {
151
- resolve: a,
152
- reject: o,
153
- timeout: u,
154
- event: t
155
- }), s(t, { ...e, requestId: r });
145
+ }, n);
146
+ f.set(i, {
147
+ resolve: r,
148
+ reject: l,
149
+ timeout: a,
150
+ event: e
151
+ }), o(e, { ...t, requestId: i });
156
152
  });
157
153
  }
158
- function A(t, e, i) {
159
- const r = f.get(t);
160
- if (!r) {
154
+ function S(e, t, n) {
155
+ const i = f.get(e);
156
+ if (!i) {
161
157
  console.warn(
162
- `[EmbeddedSDK] Received response for unknown request: ${t}`
158
+ `[EmbeddedSDK] Received response for unknown request: ${e}`
163
159
  );
164
160
  return;
165
161
  }
166
- clearTimeout(r.timeout), f.delete(t), i ? r.reject(new Error(i)) : r.resolve(e);
162
+ clearTimeout(i.timeout), f.delete(e), n ? i.reject(new Error(n)) : i.resolve(t);
167
163
  }
168
- function Y(t = "SDK cleanup") {
169
- f.forEach((e, i) => {
170
- clearTimeout(e.timeout), e.reject(
171
- new Error(`[EmbeddedSDK] Request ${i} cancelled: ${t}`)
164
+ function W(e = "SDK cleanup") {
165
+ f.forEach((t, n) => {
166
+ clearTimeout(t.timeout), t.reject(
167
+ new Error(`[EmbeddedSDK] Request ${n} cancelled: ${e}`)
172
168
  );
173
169
  }), f.clear();
174
170
  }
175
- function W(t) {
171
+ function Y(e) {
176
172
  return {
177
173
  /**
178
174
  * Get the token from the URL query parameter.
@@ -189,18 +185,6 @@ function W(t) {
189
185
  getToken() {
190
186
  return new URLSearchParams(window.location.search).get("token");
191
187
  },
192
- /**
193
- * Request logout from the host.
194
- * This will navigate to the installed apps page.
195
- *
196
- * @example
197
- * ```typescript
198
- * embedded.auth.logout();
199
- * ```
200
- */
201
- logout() {
202
- s(v.LOGOUT, {});
203
- },
204
188
  /**
205
189
  * Request a token refresh from the host.
206
190
  * This will re-render the iframe with a new token URL.
@@ -212,83 +196,73 @@ function W(t) {
212
196
  * ```
213
197
  */
214
198
  refresh() {
215
- s(v.REFRESH, {});
216
- },
217
- /**
218
- * Signal an authentication error to the host.
219
- * This will navigate away and show an error toast.
220
- *
221
- * @param message - Error message to display
222
- *
223
- * @example
224
- * ```typescript
225
- * try {
226
- * await verifyToken(token);
227
- * } catch (err) {
228
- * embedded.auth.error(err.message);
229
- * }
230
- * ```
231
- */
232
- error(e) {
233
- s(v.ERROR, { message: e });
199
+ o(M.REFRESH, {});
234
200
  }
235
201
  };
236
202
  }
237
- const R = ["success", "error", "warning", "info"];
238
- function Z(t) {
239
- const e = [];
240
- return t.type === void 0 || t.type === null ? e.push("Toast type is required") : (typeof t.type != "string" || !R.includes(t.type)) && e.push(
241
- `Invalid toast type "${t.type}". Expected: ${R.join(" | ")}`
242
- ), t.message === void 0 || t.message === null ? e.push("Toast message is required") : typeof t.message != "string" ? e.push("Toast message must be a string") : t.message.trim() === "" && e.push("Toast message cannot be empty"), t.duration !== void 0 && t.duration !== null && (typeof t.duration != "number" ? e.push("Toast duration must be a number") : t.duration < 0 && e.push("Toast duration cannot be negative")), { valid: e.length === 0, errors: e };
243
- }
244
- function X(t) {
245
- const e = [];
246
- return typeof t != "object" || t === null ? (e.push("Checkout payload must be an object"), { valid: !1, errors: e }) : (t.amount !== void 0 && t.amount !== null && (typeof t.amount != "number" ? e.push("Checkout amount must be a number") : t.amount < 0 && e.push("Checkout amount cannot be negative")), t.currency !== void 0 && t.currency !== null && (typeof t.currency != "string" ? e.push("Checkout currency must be a string") : t.currency.trim() === "" && e.push("Checkout currency cannot be empty")), t.items !== void 0 && t.items !== null && (Array.isArray(t.items) || e.push("Checkout items must be an array")), { valid: e.length === 0, errors: e });
247
- }
248
- function Q(t) {
249
- const e = [];
250
- return t.path === void 0 || t.path === null ? e.push("Navigation path is required") : typeof t.path != "string" ? e.push("Navigation path must be a string") : t.path.trim() === "" && e.push("Navigation path cannot be empty"), t.replace !== void 0 && typeof t.replace != "boolean" && e.push("Navigation replace option must be a boolean"), { valid: e.length === 0, errors: e };
251
- }
252
- function B(t) {
253
- const e = [];
254
- if (t.url === void 0 || t.url === null)
255
- e.push("Redirect URL is required");
256
- else if (typeof t.url != "string")
257
- e.push("Redirect URL must be a string");
258
- else if (t.url.trim() === "")
259
- e.push("Redirect URL cannot be empty");
203
+ const A = ["success", "error", "warning", "info"];
204
+ function Z(e) {
205
+ const t = [];
206
+ return e.type === void 0 || e.type === null ? t.push("Toast type is required") : (typeof e.type != "string" || !A.includes(e.type)) && t.push(
207
+ `Invalid toast type "${e.type}". Expected: ${A.join(" | ")}`
208
+ ), e.message === void 0 || e.message === null ? t.push("Toast message is required") : typeof e.message != "string" ? t.push("Toast message must be a string") : e.message.trim() === "" && t.push("Toast message cannot be empty"), e.duration !== void 0 && e.duration !== null && (typeof e.duration != "number" ? t.push("Toast duration must be a number") : e.duration < 0 && t.push("Toast duration cannot be negative")), { valid: t.length === 0, errors: t };
209
+ }
210
+ function X(e) {
211
+ const t = [];
212
+ return typeof e != "object" || e === null ? (t.push("Checkout payload must be an object"), { valid: !1, errors: t }) : (e.amount !== void 0 && e.amount !== null && (typeof e.amount != "number" ? t.push("Checkout amount must be a number") : e.amount < 0 && t.push("Checkout amount cannot be negative")), e.currency !== void 0 && e.currency !== null && (typeof e.currency != "string" ? t.push("Checkout currency must be a string") : e.currency.trim() === "" && t.push("Checkout currency cannot be empty")), e.items !== void 0 && e.items !== null && (Array.isArray(e.items) || t.push("Checkout items must be an array")), { valid: t.length === 0, errors: t });
213
+ }
214
+ function Q(e) {
215
+ const t = [];
216
+ return e.path === void 0 || e.path === null ? t.push("Navigation path is required") : typeof e.path != "string" ? t.push("Navigation path must be a string") : e.path.trim() === "" && t.push("Navigation path cannot be empty"), e.replace !== void 0 && typeof e.replace != "boolean" && t.push("Navigation replace option must be a boolean"), { valid: t.length === 0, errors: t };
217
+ }
218
+ function B(e) {
219
+ const t = [];
220
+ if (e.url === void 0 || e.url === null)
221
+ t.push("Redirect URL is required");
222
+ else if (typeof e.url != "string")
223
+ t.push("Redirect URL must be a string");
224
+ else if (e.url.trim() === "")
225
+ t.push("Redirect URL cannot be empty");
260
226
  else
261
227
  try {
262
- new URL(t.url);
228
+ new URL(e.url);
263
229
  } catch {
264
- e.push(`Invalid redirect URL: "${t.url}"`);
230
+ t.push(`Invalid redirect URL: "${e.url}"`);
265
231
  }
266
- return { valid: e.length === 0, errors: e };
232
+ return { valid: t.length === 0, errors: t };
267
233
  }
268
- function J(t) {
269
- const e = [];
270
- return t.title === void 0 || t.title === null ? e.push("Nav action title is required") : typeof t.title != "string" && e.push("Nav action title must be a string"), t.url !== void 0 && t.url !== null && typeof t.url != "string" && e.push("Nav action URL must be a string"), t.value !== void 0 && t.value !== null && typeof t.value != "string" && e.push("Nav action value must be a string"), t.extendedActions !== void 0 && t.extendedActions !== null && (Array.isArray(t.extendedActions) ? t.extendedActions.forEach((i, r) => {
271
- if (typeof i != "object" || i === null) {
272
- e.push(`Extended action at index ${r} must be an object`);
234
+ function J(e) {
235
+ const t = [];
236
+ return e.title === void 0 || e.title === null ? t.push("Nav action title is required") : typeof e.title != "string" && t.push("Nav action title must be a string"), e.onClick !== void 0 && e.onClick !== null && typeof e.onClick != "function" && t.push("Nav action onClick must be a function"), e.value !== void 0 && e.value !== null && typeof e.value != "string" && t.push("Nav action value must be a string"), e.subTitle !== void 0 && e.subTitle !== null && typeof e.subTitle != "string" && t.push("Nav action subTitle must be a string"), e.icon !== void 0 && e.icon !== null && typeof e.icon != "string" && t.push("Nav action icon must be a string"), e.disabled !== void 0 && e.disabled !== null && typeof e.disabled != "boolean" && t.push("Nav action disabled must be a boolean"), e.extendedActions !== void 0 && e.extendedActions !== null && (Array.isArray(e.extendedActions) ? e.extendedActions.forEach((n, i) => {
237
+ if (typeof n != "object" || n === null) {
238
+ t.push(`Extended action at index ${i} must be an object`);
273
239
  return;
274
240
  }
275
- const a = i;
276
- (!a.title || typeof a.title != "string") && e.push(
277
- `Extended action at index ${r} is missing required "title" property`
241
+ const r = n;
242
+ (!r.title || typeof r.title != "string") && t.push(
243
+ `Extended action at index ${i} is missing required "title" property`
244
+ ), r.subTitle !== void 0 && typeof r.subTitle != "string" && t.push(
245
+ `Extended action at index ${i} subTitle must be a string`
246
+ ), r.url !== void 0 && typeof r.url != "string" && t.push(`Extended action at index ${i} url must be a string`), r.value !== void 0 && typeof r.value != "string" && t.push(
247
+ `Extended action at index ${i} value must be a string`
248
+ ), r.icon !== void 0 && typeof r.icon != "string" && t.push(
249
+ `Extended action at index ${i} icon must be a string`
250
+ ), r.disabled !== void 0 && typeof r.disabled != "boolean" && t.push(
251
+ `Extended action at index ${i} disabled must be a boolean`
278
252
  );
279
- }) : e.push("Nav action extendedActions must be an array")), { valid: e.length === 0, errors: e };
253
+ }) : t.push("Nav action extendedActions must be an array")), { valid: t.length === 0, errors: t };
280
254
  }
281
255
  const C = ["danger", "warning", "info"];
282
- function ee(t) {
283
- const e = [];
284
- return t.title === void 0 || t.title === null ? e.push("Confirm dialog title is required") : typeof t.title != "string" ? e.push("Confirm dialog title must be a string") : t.title.trim() === "" && e.push("Confirm dialog title cannot be empty"), t.message === void 0 || t.message === null ? e.push("Confirm dialog message is required") : typeof t.message != "string" ? e.push("Confirm dialog message must be a string") : t.message.trim() === "" && e.push("Confirm dialog message cannot be empty"), t.confirmText !== void 0 && t.confirmText !== null && typeof t.confirmText != "string" && e.push("Confirm dialog confirmText must be a string"), t.cancelText !== void 0 && t.cancelText !== null && typeof t.cancelText != "string" && e.push("Confirm dialog cancelText must be a string"), t.variant !== void 0 && t.variant !== null && (typeof t.variant != "string" || !C.includes(t.variant)) && e.push(
285
- `Invalid confirm variant "${t.variant}". Expected: ${C.join(" | ")}`
286
- ), { valid: e.length === 0, errors: e };
256
+ function ee(e) {
257
+ const t = [];
258
+ return e.title === void 0 || e.title === null ? t.push("Confirm dialog title is required") : typeof e.title != "string" ? t.push("Confirm dialog title must be a string") : e.title.trim() === "" && t.push("Confirm dialog title cannot be empty"), e.message === void 0 || e.message === null ? t.push("Confirm dialog message is required") : typeof e.message != "string" ? t.push("Confirm dialog message must be a string") : e.message.trim() === "" && t.push("Confirm dialog message cannot be empty"), e.confirmText !== void 0 && e.confirmText !== null && typeof e.confirmText != "string" && t.push("Confirm dialog confirmText must be a string"), e.cancelText !== void 0 && e.cancelText !== null && typeof e.cancelText != "string" && t.push("Confirm dialog cancelText must be a string"), e.variant !== void 0 && e.variant !== null && (typeof e.variant != "string" || !C.includes(e.variant)) && t.push(
259
+ `Invalid confirm variant "${e.variant}". Expected: ${C.join(" | ")}`
260
+ ), { valid: t.length === 0, errors: t };
287
261
  }
288
- function c(t, e) {
262
+ function c(e, t) {
289
263
  console.error(
290
- `[EmbeddedSDK] Validation failed for ${t}:
291
- ` + e.map((i) => ` • ${i}`).join(`
264
+ `[EmbeddedSDK] Validation failed for ${e}:
265
+ ` + t.map((n) => ` • ${n}`).join(`
292
266
  `)
293
267
  );
294
268
  }
@@ -297,264 +271,265 @@ function te() {
297
271
  /**
298
272
  * Navigate to a path using React Router (SPA navigation).
299
273
  */
300
- navigate(t, e) {
301
- const i = Q({ path: t, ...e });
302
- if (!i.valid) {
303
- c(h.NAVIGATE, i.errors);
274
+ navigate(e, t) {
275
+ const n = Q({ path: e, ...t });
276
+ if (!n.valid) {
277
+ c(h.NAVIGATE, n.errors);
304
278
  return;
305
279
  }
306
- s(h.NAVIGATE, {
307
- path: t,
308
- state: e == null ? void 0 : e.state,
309
- replace: e == null ? void 0 : e.replace
280
+ o(h.NAVIGATE, {
281
+ path: e,
282
+ state: t == null ? void 0 : t.state,
283
+ replace: t == null ? void 0 : t.replace
310
284
  });
311
285
  },
312
286
  /**
313
287
  * Redirect to a URL (full page reload).
314
288
  */
315
- redirect(t) {
316
- const e = B({ url: t });
317
- if (!e.valid) {
318
- c(h.REDIRECT, e.errors);
289
+ redirect(e) {
290
+ const t = B({ url: e });
291
+ if (!t.valid) {
292
+ c(h.REDIRECT, t.errors);
319
293
  return;
320
294
  }
321
- s(h.REDIRECT, { url: t });
295
+ o(h.REDIRECT, { url: e });
322
296
  },
323
297
  /**
324
298
  * Navigate to a path - auto-detects internal vs external.
325
299
  */
326
- navTo(t, e) {
327
- if (t.startsWith("http://") || t.startsWith("https://")) {
328
- this.redirect(t);
300
+ navTo(e, t) {
301
+ if (e.startsWith("http://") || e.startsWith("https://")) {
302
+ this.redirect(e);
329
303
  return;
330
304
  }
331
- this.navigate(t, e);
305
+ this.navigate(e, t);
332
306
  },
333
307
  /**
334
308
  * Update the iframe height.
335
309
  */
336
- resize(t) {
337
- if (typeof t != "number" || t < 0) {
338
- c(y.RESIZE, [
310
+ resize(e) {
311
+ if (typeof e != "number" || e < 0) {
312
+ c(g.RESIZE, [
339
313
  "Height must be a non-negative number"
340
314
  ]);
341
315
  return;
342
316
  }
343
- s(y.RESIZE, { height: t });
317
+ o(g.RESIZE, { height: e });
344
318
  },
345
319
  /**
346
320
  * Auto-resize iframe to content height.
347
321
  */
348
322
  autoResize() {
349
- const t = document.documentElement.scrollHeight;
350
- this.resize(t);
323
+ const e = document.documentElement.scrollHeight;
324
+ this.resize(e);
351
325
  },
352
326
  /**
353
327
  * Set the page title in the host document.
354
328
  */
355
- setTitle(t) {
356
- if (typeof t != "string" || !t.trim()) {
329
+ setTitle(e) {
330
+ if (typeof e != "string" || !e.trim()) {
357
331
  c(h.SET_TITLE, [
358
332
  "Title must be a non-empty string"
359
333
  ]);
360
334
  return;
361
335
  }
362
- s(h.SET_TITLE, { title: t });
336
+ o(h.SET_TITLE, { title: e });
363
337
  }
364
338
  };
365
339
  }
366
340
  function ie() {
367
- const t = /* @__PURE__ */ new Set();
368
- return g(E.ACTION_CLICK, (e) => {
369
- t.forEach((i) => {
341
+ const e = /* @__PURE__ */ new Set();
342
+ let t = null;
343
+ return E(b.ACTION_CLICK, (i) => {
344
+ if (t)
370
345
  try {
371
- i(e.url, e.value);
346
+ t();
372
347
  } catch (r) {
373
- console.error("[EmbeddedSDK] Error in action click callback:", r);
348
+ console.error("[EmbeddedSDK] Error in onClick callback:", r);
349
+ }
350
+ e.forEach((r) => {
351
+ try {
352
+ r(i.url, i.value);
353
+ } catch (l) {
354
+ console.error("[EmbeddedSDK] Error in action click callback:", l);
374
355
  }
375
356
  });
376
357
  }), {
377
358
  /**
378
359
  * Set the primary action button.
379
360
  */
380
- setAction(e) {
381
- const i = J(e);
382
- if (!i.valid) {
383
- c(E.SET_ACTION, i.errors);
361
+ setAction(i) {
362
+ var l;
363
+ const r = J(i);
364
+ if (!r.valid) {
365
+ c(b.SET_ACTION, r.errors);
384
366
  return;
385
367
  }
386
- s(E.SET_ACTION, {
387
- title: e.title,
388
- url: e.url,
389
- value: e.value,
390
- extendedActions: e.extendedActions
368
+ i.onClick ? t = i.onClick : t = null, o(b.SET_ACTION, {
369
+ title: i.title,
370
+ onClick: i.onClick ? !0 : void 0,
371
+ value: i.value,
372
+ subTitle: i.subTitle,
373
+ icon: i.icon,
374
+ disabled: i.disabled,
375
+ extendedActions: (l = i.extendedActions) == null ? void 0 : l.map((a) => ({
376
+ title: a.title,
377
+ subTitle: a.subTitle,
378
+ url: a.url,
379
+ value: a.value,
380
+ icon: a.icon,
381
+ disabled: a.disabled
382
+ }))
391
383
  });
392
384
  },
393
385
  /**
394
386
  * Clear the primary action button.
395
387
  */
396
388
  clearAction() {
397
- s(E.SET_ACTION, {
389
+ t = null, o(b.SET_ACTION, {
398
390
  title: ""
399
391
  });
400
392
  },
401
393
  /**
402
394
  * Subscribe to action button clicks.
403
395
  */
404
- onActionClick(e) {
405
- return t.add(e), () => {
406
- t.delete(e);
396
+ onActionClick(i) {
397
+ return e.add(i), () => {
398
+ e.delete(i);
407
399
  };
408
400
  },
409
401
  // Legacy aliases
410
- primaryAction(e) {
411
- this.setAction(e);
402
+ primaryAction(i) {
403
+ this.setAction(i);
412
404
  },
413
405
  clearPrimaryAction() {
414
406
  this.clearAction();
415
407
  }
416
408
  };
417
409
  }
418
- function re() {
410
+ function ne() {
419
411
  return {
420
412
  /**
421
413
  * Show loading indicator.
422
414
  * @param mode - Display mode ('full' for full page, 'component' for inline)
423
415
  */
424
- show(t = "full") {
425
- s(l.LOADING, { status: !1, mode: t });
416
+ show(e = "full") {
417
+ o(u.LOADING, { status: !1, mode: e });
426
418
  },
427
419
  /**
428
420
  * Hide loading indicator.
429
421
  */
430
422
  hide() {
431
- s(l.LOADING, { status: !0, mode: "full" });
432
- }
433
- };
434
- }
435
- function ne() {
436
- return {
437
- /**
438
- * Open overlay mode.
439
- */
440
- open() {
441
- s(l.OVERLAY, { action: "open" });
442
- },
443
- /**
444
- * Close overlay mode.
445
- */
446
- close() {
447
- s(l.OVERLAY, { action: "close" });
423
+ o(u.LOADING, { status: !0, mode: "full" });
448
424
  }
449
425
  };
450
426
  }
451
- function se() {
452
- const t = (e) => {
453
- const i = Z(e);
454
- if (!i.valid) {
455
- c(l.TOAST, i.errors);
427
+ function re() {
428
+ const e = (t) => {
429
+ const n = Z(t);
430
+ if (!n.valid) {
431
+ c(u.TOAST, n.errors);
456
432
  return;
457
433
  }
458
- s(l.TOAST, {
459
- type: e.type,
460
- message: e.message,
461
- duration: e.duration
434
+ o(u.TOAST, {
435
+ type: t.type,
436
+ message: t.message,
437
+ duration: t.duration
462
438
  });
463
439
  };
464
440
  return {
465
441
  /**
466
442
  * Show a toast notification.
467
443
  */
468
- show: t,
444
+ show: e,
469
445
  /**
470
446
  * Show success toast.
471
447
  */
472
- success(e, i) {
473
- t({ type: "success", message: e, duration: i });
448
+ success(t, n) {
449
+ e({ type: "success", message: t, duration: n });
474
450
  },
475
451
  /**
476
452
  * Show error toast.
477
453
  */
478
- error(e, i) {
479
- t({ type: "error", message: e, duration: i });
454
+ error(t, n) {
455
+ e({ type: "error", message: t, duration: n });
480
456
  },
481
457
  /**
482
458
  * Show warning toast.
483
459
  */
484
- warning(e, i) {
485
- t({ type: "warning", message: e, duration: i });
460
+ warning(t, n) {
461
+ e({ type: "warning", message: t, duration: n });
486
462
  },
487
463
  /**
488
464
  * Show info toast.
489
465
  */
490
- info(e, i) {
491
- t({ type: "info", message: e, duration: i });
466
+ info(t, n) {
467
+ e({ type: "info", message: t, duration: n });
492
468
  }
493
469
  };
494
470
  }
495
- function ae() {
471
+ function se() {
496
472
  return {
497
473
  /**
498
474
  * Open a modal.
499
475
  */
500
- open(t, e) {
501
- s(l.MODAL, {
476
+ open(e, t) {
477
+ o(u.MODAL, {
502
478
  action: "open",
503
- id: t,
504
- content: e
479
+ id: e,
480
+ content: t
505
481
  });
506
482
  },
507
483
  /**
508
484
  * Close a modal.
509
485
  */
510
- close(t) {
511
- s(l.MODAL, {
486
+ close(e) {
487
+ o(u.MODAL, {
512
488
  action: "close",
513
- id: t
489
+ id: e
514
490
  });
515
491
  }
516
492
  };
517
493
  }
518
- function oe() {
519
- return async (t) => {
520
- const e = ee(t);
521
- return e.valid ? j(l.CONFIRM, {
522
- title: t.title,
523
- message: t.message,
524
- confirmText: t.confirmText ?? "Confirm",
525
- cancelText: t.cancelText ?? "Cancel",
526
- variant: t.variant ?? "info"
527
- }) : (c(l.CONFIRM, e.errors), Promise.reject(new Error(e.errors.join(", "))));
494
+ function ae() {
495
+ return async (e) => {
496
+ const t = ee(e);
497
+ return t.valid ? j(u.CONFIRM, {
498
+ title: e.title,
499
+ message: e.message,
500
+ confirmText: e.confirmText ?? "Confirm",
501
+ cancelText: e.cancelText ?? "Cancel",
502
+ variant: e.variant ?? "info"
503
+ }) : (c(u.CONFIRM, t.errors), Promise.reject(new Error(t.errors.join(", "))));
528
504
  };
529
505
  }
530
- function le() {
506
+ function oe() {
531
507
  return {
532
- loading: re(),
533
- overlay: ne(),
534
- toast: se(),
535
- modal: ae(),
536
- confirm: oe()
508
+ loading: ne(),
509
+ toast: re(),
510
+ modal: se(),
511
+ confirm: ae()
537
512
  };
538
513
  }
539
- function ue() {
514
+ function le() {
540
515
  return {
541
516
  /**
542
517
  * Create/initiate a checkout.
543
518
  */
544
- create(t) {
545
- const e = X(t);
546
- if (!e.valid) {
547
- c(p.CREATE, e.errors);
519
+ create(e) {
520
+ const t = X(e);
521
+ if (!t.valid) {
522
+ c(p.CREATE, t.errors);
548
523
  return;
549
524
  }
550
- s(p.CREATE, { payload: t });
525
+ o(p.CREATE, { payload: e });
551
526
  }
552
527
  };
553
528
  }
554
- const L = {
529
+ const R = {
555
530
  debug: !1,
556
531
  initialized: !1
557
- }, ce = {
532
+ }, ue = {
558
533
  theme: "light",
559
534
  width: 0,
560
535
  locale: "ar",
@@ -562,11 +537,11 @@ const L = {
562
537
  }, I = {
563
538
  ready: !1,
564
539
  initializing: !1,
565
- layout: { ...ce }
540
+ layout: { ...ue }
566
541
  };
567
- class de {
542
+ class ce {
568
543
  constructor() {
569
- this.config = { ...L }, this.state = { ...I }, this.themeCallbacks = /* @__PURE__ */ new Set(), this.initCallbacks = /* @__PURE__ */ new Set(), this.appReady = !1, this.auth = W(), this.page = te(), this.nav = ie(), this.ui = le(), this.checkout = ue(), this.setupThemeListener(), this.setupResponseListeners();
544
+ this.config = { ...R }, this.state = { ...I }, this.themeCallbacks = /* @__PURE__ */ new Set(), this.initCallbacks = /* @__PURE__ */ new Set(), this.appReady = !1, this.auth = Y(), this.page = te(), this.nav = ie(), this.ui = oe(), this.checkout = le(), this.setupThemeListener(), this.setupResponseListeners();
570
545
  }
571
546
  /**
572
547
  * Get current SDK state (layout info only, no token).
@@ -593,25 +568,25 @@ class de {
593
568
  /**
594
569
  * Log debug messages if debug mode is enabled.
595
570
  */
596
- debugLog(...e) {
597
- this.config.debug && console.log(`[EmbeddedSDK v${b}]`, ...e);
571
+ debugLog(...t) {
572
+ this.config.debug && console.log(`[EmbeddedSDK v${y}]`, ...t);
598
573
  }
599
574
  /**
600
575
  * Log warnings.
601
576
  */
602
- warn(...e) {
603
- console.warn(`[EmbeddedSDK v${b}]`, ...e);
577
+ warn(...t) {
578
+ console.warn(`[EmbeddedSDK v${y}]`, ...t);
604
579
  }
605
580
  /**
606
581
  * Set up listener for theme changes from host.
607
582
  */
608
583
  setupThemeListener() {
609
- g(S.THEME_CHANGE, (e) => {
610
- this.state.layout.theme = e.theme, this.debugLog("Theme changed:", e.theme), this.themeCallbacks.forEach((i) => {
584
+ E(w.THEME_CHANGE, (t) => {
585
+ this.state.layout.theme = t.theme, this.debugLog("Theme changed:", t.theme), this.themeCallbacks.forEach((n) => {
611
586
  try {
612
- i(e.theme);
613
- } catch (r) {
614
- console.error("[EmbeddedSDK] Error in theme callback:", r);
587
+ n(t.theme);
588
+ } catch (i) {
589
+ console.error("[EmbeddedSDK] Error in theme callback:", i);
615
590
  }
616
591
  });
617
592
  });
@@ -620,10 +595,10 @@ class de {
620
595
  * Set up listeners for async response events from host.
621
596
  */
622
597
  setupResponseListeners() {
623
- g(l.CONFIRM_RESPONSE, (e) => {
624
- this.debugLog("Received confirm response:", e), A(e.requestId, { confirmed: e.confirmed });
625
- }), g(l.MODAL_RESPONSE, (e) => {
626
- this.debugLog("Received modal response:", e), A(e.requestId, e.result, e.error);
598
+ E(u.CONFIRM_RESPONSE, (t) => {
599
+ this.debugLog("Received confirm response:", t), S(t.requestId, { confirmed: t.confirmed });
600
+ }), E(u.MODAL_RESPONSE, (t) => {
601
+ this.debugLog("Received modal response:", t), S(t.requestId, t.result, t.error);
627
602
  });
628
603
  }
629
604
  /**
@@ -639,9 +614,9 @@ class de {
639
614
  * });
640
615
  * ```
641
616
  */
642
- onThemeChange(e) {
643
- return this.themeCallbacks.add(e), () => {
644
- this.themeCallbacks.delete(e);
617
+ onThemeChange(t) {
618
+ return this.themeCallbacks.add(t), () => {
619
+ this.themeCallbacks.delete(t);
645
620
  };
646
621
  }
647
622
  /**
@@ -657,15 +632,15 @@ class de {
657
632
  * });
658
633
  * ```
659
634
  */
660
- onInit(e) {
635
+ onInit(t) {
661
636
  if (this.config.initialized)
662
637
  try {
663
- e(this.getState());
664
- } catch (i) {
665
- console.error("[EmbeddedSDK] Error in init callback:", i);
638
+ t(this.getState());
639
+ } catch (n) {
640
+ console.error("[EmbeddedSDK] Error in init callback:", n);
666
641
  }
667
- return this.initCallbacks.add(e), () => {
668
- this.initCallbacks.delete(e);
642
+ return this.initCallbacks.add(t), () => {
643
+ this.initCallbacks.delete(t);
669
644
  };
670
645
  }
671
646
  /**
@@ -680,11 +655,11 @@ class de {
680
655
  * embedded.log('error', 'Failed to load data', { endpoint: '/api/data' });
681
656
  * ```
682
657
  */
683
- log(e, i, r) {
684
- s(k.LOG, {
685
- level: e,
686
- message: i,
687
- context: r
658
+ log(t, n, i) {
659
+ o(_.LOG, {
660
+ level: t,
661
+ message: n,
662
+ context: i
688
663
  });
689
664
  }
690
665
  /**
@@ -706,7 +681,7 @@ class de {
706
681
  this.warn("Cannot signal ready before init() is called");
707
682
  return;
708
683
  }
709
- this.appReady = !0, s(y.READY, {}), this.debugLog("Sent ready signal to host");
684
+ this.appReady = !0, o(g.READY, {}), this.debugLog("Sent ready signal to host");
710
685
  }
711
686
  /**
712
687
  * Initialize the SDK and establish connection with the host.
@@ -721,43 +696,43 @@ class de {
721
696
  * console.log('Locale:', layout.locale);
722
697
  * ```
723
698
  */
724
- async init(e = {}) {
725
- var i, r, a, o;
699
+ async init(t = {}) {
700
+ var n, i, r, l;
726
701
  if (this.config.initialized)
727
702
  return this.debugLog("Already initialized, returning current layout"), { layout: { ...this.state.layout } };
728
703
  if (this.state.initializing)
729
704
  return this.warn("Initialization already in progress"), this.waitForInit();
730
705
  P() || this.warn("Not running in an iframe. Some features may not work."), this.config = {
731
- debug: e.debug ?? !1,
706
+ debug: t.debug ?? !1,
732
707
  initialized: !1
733
708
  }, this.state.initializing = !0, this.debugLog("Initializing SDK...");
734
709
  try {
735
- s(y.INIT, {
710
+ o(g.INIT, {
736
711
  height: document.documentElement.scrollHeight
737
712
  }), this.debugLog("Sent iframe.ready message, waiting for context...");
738
- const u = await F(
739
- S.PROVIDE
713
+ const a = await F(
714
+ w.PROVIDE
740
715
  );
741
- this.debugLog("Received context from host:", u), this.state = {
716
+ this.debugLog("Received context from host:", a), this.state = {
742
717
  ready: !0,
743
718
  initializing: !1,
744
719
  layout: {
745
- theme: ((i = u.layout) == null ? void 0 : i.theme) ?? "light",
746
- width: ((r = u.layout) == null ? void 0 : r.width) ?? 0,
747
- locale: ((a = u.layout) == null ? void 0 : a.locale) ?? "ar",
748
- currency: ((o = u.layout) == null ? void 0 : o.currency) ?? "SAR"
720
+ theme: ((n = a.layout) == null ? void 0 : n.theme) ?? "light",
721
+ width: ((i = a.layout) == null ? void 0 : i.width) ?? 0,
722
+ locale: ((r = a.layout) == null ? void 0 : r.locale) ?? "ar",
723
+ currency: ((l = a.layout) == null ? void 0 : l.currency) ?? "SAR"
749
724
  }
750
725
  }, this.config.initialized = !0, this.debugLog("Initialization complete. Layout:", this.state.layout);
751
- const w = this.getState();
726
+ const v = this.getState();
752
727
  return this.initCallbacks.forEach((D) => {
753
728
  try {
754
- D(w);
755
- } catch ($) {
756
- console.error("[EmbeddedSDK] Error in init callback:", $);
729
+ D(v);
730
+ } catch (O) {
731
+ console.error("[EmbeddedSDK] Error in init callback:", O);
757
732
  }
758
733
  }), { layout: { ...this.state.layout } };
759
- } catch (u) {
760
- throw this.state.initializing = !1, this.state.ready = !1, u;
734
+ } catch (a) {
735
+ throw this.state.initializing = !1, this.state.ready = !1, a;
761
736
  }
762
737
  }
763
738
  /**
@@ -765,34 +740,41 @@ class de {
765
740
  * Useful when multiple calls to init() might happen.
766
741
  */
767
742
  waitForInit() {
768
- return new Promise((e) => {
769
- const i = () => {
770
- this.state.ready ? e({ layout: { ...this.state.layout } }) : setTimeout(i, 100);
743
+ return new Promise((t) => {
744
+ const n = () => {
745
+ this.state.ready ? t({ layout: { ...this.state.layout } }) : setTimeout(n, 100);
771
746
  };
772
- i();
747
+ n();
773
748
  });
774
749
  }
775
750
  /**
776
751
  * Destroy the SDK instance and clean up resources.
752
+ * Sends a destroy event to the host to navigate away from the embedded view.
753
+ *
754
+ * @example
755
+ * ```typescript
756
+ * // On auth failure or when app needs to exit
757
+ * embedded.destroy();
758
+ * ```
777
759
  */
778
760
  destroy() {
779
- this.debugLog("Destroying SDK instance"), Y("SDK destroyed"), q(), this.themeCallbacks.clear(), this.initCallbacks.clear(), this.config = { ...L }, this.state = { ...I }, this.appReady = !1;
761
+ this.debugLog("Destroying SDK instance"), this.config.initialized && (o(g.DESTROY, {}), this.debugLog("Sent destroy event to host")), W("SDK destroyed"), q(), this.themeCallbacks.clear(), this.initCallbacks.clear(), this.config = { ...R }, this.state = { ...I }, this.appReady = !1;
780
762
  }
781
763
  }
782
764
  let m = null;
783
- function fe() {
784
- return m || (m = new de()), m;
765
+ function de() {
766
+ return m || (m = new ce()), m;
785
767
  }
786
- function he() {
768
+ function fe() {
787
769
  m && (m.destroy(), m = null);
788
770
  }
789
- const N = fe(), me = b;
771
+ const N = de(), he = y;
790
772
  typeof window < "u" && (window.salla = window.salla || window.Salla || {}, window.Salla = window.salla, window.salla.embedded || (window.salla.embedded = N), window.Salla.embedded || (window.Salla.embedded = N));
791
773
  export {
792
- de as EmbeddedApp,
774
+ ce as EmbeddedApp,
793
775
  N as embedded,
794
- fe as getEmbeddedApp,
795
- he as resetEmbeddedApp,
796
- me as version
776
+ de as getEmbeddedApp,
777
+ fe as resetEmbeddedApp,
778
+ he as version
797
779
  };
798
780
  //# sourceMappingURL=index.js.map