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

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