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

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.5", 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
+ }, A = 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,141 @@ 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(() => E.abort(), s);
179
+ try {
180
+ const u = await fetch(a, {
181
+ method: n,
182
+ headers: {
183
+ "Content-Type": "application/json",
184
+ ...i
185
+ },
186
+ body: r ? JSON.stringify(r) : void 0,
187
+ signal: E.signal
188
+ });
189
+ clearTimeout(p);
190
+ let v;
191
+ const C = u.headers.get("content-type");
192
+ if (C && C.includes("application/json") ? v = await u.json() : v = await u.text(), !u.ok)
193
+ throw new b(
194
+ `API request failed: ${u.statusText}`,
195
+ u.status,
196
+ v
197
+ );
198
+ return v;
199
+ } catch (u) {
200
+ 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");
201
+ }
170
202
  }
171
- function Y(e) {
203
+ function Q(t) {
172
204
  return {
173
205
  /**
174
206
  * Get the token from the URL query parameter.
@@ -185,6 +217,13 @@ function Y(e) {
185
217
  getToken() {
186
218
  return new URLSearchParams(window.location.search).get("token");
187
219
  },
220
+ /**
221
+ * Get the app ID from the URL query parameter.
222
+ * The app ID is passed to the iframe via ?app_id=XXX
223
+ */
224
+ getAppId() {
225
+ return new URLSearchParams(window.location.search).get("app_id");
226
+ },
188
227
  /**
189
228
  * Request a token refresh from the host.
190
229
  * This will re-render the iframe with a new token URL.
@@ -196,162 +235,199 @@ function Y(e) {
196
235
  * ```
197
236
  */
198
237
  refresh() {
199
- o(M.REFRESH, {});
238
+ l(q.REFRESH, {});
239
+ },
240
+ /**
241
+ * Introspect (verify) a short-lived token with Salla's API.
242
+ * This method verifies the token and returns token information.
243
+ *
244
+ * @param options - Optional parameters (appId and token). If not provided, will be extracted from URL params.
245
+ * @returns Promise that resolves with the introspect response
246
+ * @throws {Error} If appId or token is missing, or if the API request fails
247
+ *
248
+ */
249
+ async introspect(e = {}) {
250
+ const n = e.token ?? this.getToken();
251
+ if (!n)
252
+ throw new Error(
253
+ "Token is required. Provide it as a parameter or in URL as ?token=XXX"
254
+ );
255
+ const i = e.appId ?? this.getAppId();
256
+ if (!i)
257
+ throw new Error(
258
+ "App ID is required. Provide it as a parameter or in URL as ?app_id=XXX"
259
+ );
260
+ return await J(
261
+ "/exchange-authority/v1/introspect",
262
+ {
263
+ method: "POST",
264
+ headers: {
265
+ "S-Source": i,
266
+ "Content-Type": "application/json"
267
+ },
268
+ body: {
269
+ env: "prod",
270
+ token: n,
271
+ iss: "merchant-dashboard",
272
+ subject: "embedded-page"
273
+ }
274
+ }
275
+ );
200
276
  }
201
277
  };
202
278
  }
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");
279
+ const N = ["success", "error", "warning", "info"];
280
+ function ee(t) {
281
+ const e = [];
282
+ return t.type === void 0 || t.type === null ? e.push("Toast type is required") : (typeof t.type != "string" || !N.includes(t.type)) && e.push(
283
+ `Invalid toast type "${t.type}". Expected: ${N.join(" | ")}`
284
+ ), 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 };
285
+ }
286
+ function te(t) {
287
+ const e = [];
288
+ 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 });
289
+ }
290
+ function ie(t) {
291
+ const e = [];
292
+ 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 };
293
+ }
294
+ function ne(t) {
295
+ const e = [];
296
+ if (t.url === void 0 || t.url === null)
297
+ e.push("Redirect URL is required");
298
+ else if (typeof t.url != "string")
299
+ e.push("Redirect URL must be a string");
300
+ else if (t.url.trim() === "")
301
+ e.push("Redirect URL cannot be empty");
226
302
  else
227
303
  try {
228
- new URL(e.url);
304
+ new URL(t.url);
229
305
  } catch {
230
- t.push(`Invalid redirect URL: "${e.url}"`);
306
+ e.push(`Invalid redirect URL: "${t.url}"`);
231
307
  }
232
- return { valid: t.length === 0, errors: t };
308
+ return { valid: e.length === 0, errors: e };
233
309
  }
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) => {
310
+ function re(t) {
311
+ const e = [];
312
+ 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
313
  if (typeof n != "object" || n === null) {
238
- t.push(`Extended action at index ${i} must be an object`);
314
+ e.push(`Extended action at index ${i} must be an object`);
239
315
  return;
240
316
  }
241
317
  const r = n;
242
- (!r.title || typeof r.title != "string") && t.push(
318
+ (!r.title || typeof r.title != "string") && e.push(
243
319
  `Extended action at index ${i} is missing required "title" property`
244
- ), r.subTitle !== void 0 && typeof r.subTitle != "string" && t.push(
320
+ ), r.subTitle !== void 0 && typeof r.subTitle != "string" && e.push(
245
321
  `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(
322
+ ), 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
323
  `Extended action at index ${i} value must be a string`
248
- ), r.icon !== void 0 && typeof r.icon != "string" && t.push(
324
+ ), r.icon !== void 0 && typeof r.icon != "string" && e.push(
249
325
  `Extended action at index ${i} icon must be a string`
250
- ), r.disabled !== void 0 && typeof r.disabled != "boolean" && t.push(
326
+ ), r.disabled !== void 0 && typeof r.disabled != "boolean" && e.push(
251
327
  `Extended action at index ${i} disabled must be a boolean`
252
328
  );
253
- }) : t.push("Nav action extendedActions must be an array")), { valid: t.length === 0, errors: t };
329
+ }) : e.push("Nav action extendedActions must be an array")), { valid: e.length === 0, errors: e };
254
330
  }
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 };
331
+ const $ = ["danger", "warning", "info"];
332
+ function se(t) {
333
+ const e = [];
334
+ 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(
335
+ `Invalid confirm variant "${t.variant}". Expected: ${$.join(" | ")}`
336
+ ), { valid: e.length === 0, errors: e };
261
337
  }
262
- function c(e, t) {
338
+ function d(t, e) {
263
339
  console.error(
264
- `[EmbeddedSDK] Validation failed for ${e}:
265
- ` + t.map((n) => ` • ${n}`).join(`
340
+ `[EmbeddedSDK] Validation failed for ${t}:
341
+ ` + e.map((n) => ` • ${n}`).join(`
266
342
  `)
267
343
  );
268
344
  }
269
- function te() {
345
+ function ae() {
270
346
  return {
271
347
  /**
272
348
  * Navigate to a path using React Router (SPA navigation).
273
349
  */
274
- navigate(e, t) {
275
- const n = Q({ path: e, ...t });
350
+ navigate(t, e) {
351
+ const n = ie({ path: t, ...e });
276
352
  if (!n.valid) {
277
- c(h.NAVIGATE, n.errors);
353
+ d(m.NAVIGATE, n.errors);
278
354
  return;
279
355
  }
280
- o(h.NAVIGATE, {
281
- path: e,
282
- state: t == null ? void 0 : t.state,
283
- replace: t == null ? void 0 : t.replace
356
+ l(m.NAVIGATE, {
357
+ path: t,
358
+ state: e == null ? void 0 : e.state,
359
+ replace: e == null ? void 0 : e.replace
284
360
  });
285
361
  },
286
362
  /**
287
363
  * Redirect to a URL (full page reload).
288
364
  */
289
- redirect(e) {
290
- const t = B({ url: e });
291
- if (!t.valid) {
292
- c(h.REDIRECT, t.errors);
365
+ redirect(t) {
366
+ const e = ne({ url: t });
367
+ if (!e.valid) {
368
+ d(m.REDIRECT, e.errors);
293
369
  return;
294
370
  }
295
- o(h.REDIRECT, { url: e });
371
+ l(m.REDIRECT, { url: t });
296
372
  },
297
373
  /**
298
374
  * Navigate to a path - auto-detects internal vs external.
299
375
  */
300
- navTo(e, t) {
301
- if (e.startsWith("http://") || e.startsWith("https://")) {
302
- this.redirect(e);
376
+ navTo(t, e) {
377
+ if (t.startsWith("http://") || t.startsWith("https://")) {
378
+ this.redirect(t);
303
379
  return;
304
380
  }
305
- this.navigate(e, t);
381
+ this.navigate(t, e);
306
382
  },
307
383
  /**
308
384
  * Update the iframe height.
309
385
  */
310
- resize(e) {
311
- if (typeof e != "number" || e < 0) {
312
- c(g.RESIZE, [
386
+ resize(t) {
387
+ if (typeof t != "number" || t < 0) {
388
+ d(T.RESIZE, [
313
389
  "Height must be a non-negative number"
314
390
  ]);
315
391
  return;
316
392
  }
317
- o(g.RESIZE, { height: e });
393
+ l(T.RESIZE, { height: t });
318
394
  },
319
395
  /**
320
396
  * Auto-resize iframe to content height.
321
397
  */
322
398
  autoResize() {
323
- const e = document.documentElement.scrollHeight;
324
- this.resize(e);
399
+ const t = document.documentElement.scrollHeight;
400
+ this.resize(t);
325
401
  },
326
402
  /**
327
403
  * Set the page title in the host document.
328
404
  */
329
- setTitle(e) {
330
- if (typeof e != "string" || !e.trim()) {
331
- c(h.SET_TITLE, [
405
+ setTitle(t) {
406
+ if (typeof t != "string" || !t.trim()) {
407
+ d(m.SET_TITLE, [
332
408
  "Title must be a non-empty string"
333
409
  ]);
334
410
  return;
335
411
  }
336
- o(h.SET_TITLE, { title: e });
412
+ l(m.SET_TITLE, { title: t });
337
413
  }
338
414
  };
339
415
  }
340
- function ie() {
341
- const e = /* @__PURE__ */ new Set();
342
- let t = null;
343
- return E(b.ACTION_CLICK, (i) => {
344
- if (t)
416
+ function oe() {
417
+ const t = /* @__PURE__ */ new Set();
418
+ let e = null;
419
+ return y(w.ACTION_CLICK, (i) => {
420
+ if (e)
345
421
  try {
346
- t();
422
+ e();
347
423
  } catch (r) {
348
424
  console.error("[EmbeddedSDK] Error in onClick callback:", r);
349
425
  }
350
- e.forEach((r) => {
426
+ t.forEach((r) => {
351
427
  try {
352
428
  r(i.url, i.value);
353
- } catch (l) {
354
- console.error("[EmbeddedSDK] Error in action click callback:", l);
429
+ } catch (s) {
430
+ console.error("[EmbeddedSDK] Error in action click callback:", s);
355
431
  }
356
432
  });
357
433
  }), {
@@ -359,20 +435,20 @@ function ie() {
359
435
  * Set the primary action button.
360
436
  */
361
437
  setAction(i) {
362
- var l;
363
- const r = J(i);
438
+ var s;
439
+ const r = re(i);
364
440
  if (!r.valid) {
365
- c(b.SET_ACTION, r.errors);
441
+ d(w.SET_ACTION, r.errors);
366
442
  return;
367
443
  }
368
- i.onClick ? t = i.onClick : t = null, o(b.SET_ACTION, {
444
+ i.onClick ? e = i.onClick : e = null, l(w.SET_ACTION, {
369
445
  title: i.title,
370
446
  onClick: i.onClick ? !0 : void 0,
371
447
  value: i.value,
372
448
  subTitle: i.subTitle,
373
449
  icon: i.icon,
374
450
  disabled: i.disabled,
375
- extendedActions: (l = i.extendedActions) == null ? void 0 : l.map((a) => ({
451
+ extendedActions: (s = i.extendedActions) == null ? void 0 : s.map((a) => ({
376
452
  title: a.title,
377
453
  subTitle: a.subTitle,
378
454
  url: a.url,
@@ -386,7 +462,7 @@ function ie() {
386
462
  * Clear the primary action button.
387
463
  */
388
464
  clearAction() {
389
- t = null, o(b.SET_ACTION, {
465
+ e = null, l(w.SET_ACTION, {
390
466
  title: ""
391
467
  });
392
468
  },
@@ -394,8 +470,8 @@ function ie() {
394
470
  * Subscribe to action button clicks.
395
471
  */
396
472
  onActionClick(i) {
397
- return e.add(i), () => {
398
- e.delete(i);
473
+ return t.add(i), () => {
474
+ t.delete(i);
399
475
  };
400
476
  },
401
477
  // Legacy aliases
@@ -407,141 +483,141 @@ function ie() {
407
483
  }
408
484
  };
409
485
  }
410
- function ne() {
486
+ function le() {
411
487
  return {
412
488
  /**
413
489
  * Show loading indicator.
414
490
  * @param mode - Display mode ('full' for full page, 'component' for inline)
415
491
  */
416
- show(e = "full") {
417
- o(u.LOADING, { status: !1, mode: e });
492
+ show(t = "full") {
493
+ l(c.LOADING, { status: !1, mode: t });
418
494
  },
419
495
  /**
420
496
  * Hide loading indicator.
421
497
  */
422
498
  hide() {
423
- o(u.LOADING, { status: !0, mode: "full" });
499
+ l(c.LOADING, { status: !0, mode: "full" });
424
500
  }
425
501
  };
426
502
  }
427
- function re() {
428
- const e = (t) => {
429
- const n = Z(t);
503
+ function ue() {
504
+ const t = (e) => {
505
+ const n = ee(e);
430
506
  if (!n.valid) {
431
- c(u.TOAST, n.errors);
507
+ d(c.TOAST, n.errors);
432
508
  return;
433
509
  }
434
- o(u.TOAST, {
435
- type: t.type,
436
- message: t.message,
437
- duration: t.duration
510
+ l(c.TOAST, {
511
+ type: e.type,
512
+ message: e.message,
513
+ duration: e.duration
438
514
  });
439
515
  };
440
516
  return {
441
517
  /**
442
518
  * Show a toast notification.
443
519
  */
444
- show: e,
520
+ show: t,
445
521
  /**
446
522
  * Show success toast.
447
523
  */
448
- success(t, n) {
449
- e({ type: "success", message: t, duration: n });
524
+ success(e, n) {
525
+ t({ type: "success", message: e, duration: n });
450
526
  },
451
527
  /**
452
528
  * Show error toast.
453
529
  */
454
- error(t, n) {
455
- e({ type: "error", message: t, duration: n });
530
+ error(e, n) {
531
+ t({ type: "error", message: e, duration: n });
456
532
  },
457
533
  /**
458
534
  * Show warning toast.
459
535
  */
460
- warning(t, n) {
461
- e({ type: "warning", message: t, duration: n });
536
+ warning(e, n) {
537
+ t({ type: "warning", message: e, duration: n });
462
538
  },
463
539
  /**
464
540
  * Show info toast.
465
541
  */
466
- info(t, n) {
467
- e({ type: "info", message: t, duration: n });
542
+ info(e, n) {
543
+ t({ type: "info", message: e, duration: n });
468
544
  }
469
545
  };
470
546
  }
471
- function se() {
547
+ function ce() {
472
548
  return {
473
549
  /**
474
550
  * Open a modal.
475
551
  */
476
- open(e, t) {
477
- o(u.MODAL, {
552
+ open(t, e) {
553
+ l(c.MODAL, {
478
554
  action: "open",
479
- id: e,
480
- content: t
555
+ id: t,
556
+ content: e
481
557
  });
482
558
  },
483
559
  /**
484
560
  * Close a modal.
485
561
  */
486
- close(e) {
487
- o(u.MODAL, {
562
+ close(t) {
563
+ l(c.MODAL, {
488
564
  action: "close",
489
- id: e
565
+ id: t
490
566
  });
491
567
  }
492
568
  };
493
569
  }
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(", "))));
570
+ function de() {
571
+ return async (t) => {
572
+ const e = se(t);
573
+ return e.valid ? Y(c.CONFIRM, {
574
+ title: t.title,
575
+ message: t.message,
576
+ confirmText: t.confirmText ?? "Confirm",
577
+ cancelText: t.cancelText ?? "Cancel",
578
+ variant: t.variant ?? "info"
579
+ }) : (d(c.CONFIRM, e.errors), Promise.reject(new Error(e.errors.join(", "))));
504
580
  };
505
581
  }
506
- function oe() {
582
+ function fe() {
507
583
  return {
508
- loading: ne(),
509
- toast: re(),
510
- modal: se(),
511
- confirm: ae()
584
+ loading: le(),
585
+ toast: ue(),
586
+ modal: ce(),
587
+ confirm: de()
512
588
  };
513
589
  }
514
- function le() {
590
+ function he() {
515
591
  return {
516
592
  /**
517
593
  * Create/initiate a checkout.
518
594
  */
519
- create(e) {
520
- const t = X(e);
521
- if (!t.valid) {
522
- c(p.CREATE, t.errors);
595
+ create(t) {
596
+ const e = te(t);
597
+ if (!e.valid) {
598
+ d(I.CREATE, e.errors);
523
599
  return;
524
600
  }
525
- o(p.CREATE, { payload: e });
601
+ l(I.CREATE, { payload: t });
526
602
  }
527
603
  };
528
604
  }
529
- const R = {
605
+ const D = {
530
606
  debug: !1,
531
607
  initialized: !1
532
- }, ue = {
608
+ }, me = {
533
609
  theme: "light",
534
610
  width: 0,
535
611
  locale: "ar",
536
612
  currency: "SAR"
537
- }, I = {
613
+ }, k = {
538
614
  ready: !1,
539
615
  initializing: !1,
540
- layout: { ...ue }
616
+ layout: { ...me }
541
617
  };
542
- class ce {
618
+ class ge {
543
619
  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();
620
+ 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
621
  }
546
622
  /**
547
623
  * Get current SDK state (layout info only, no token).
@@ -568,23 +644,23 @@ class ce {
568
644
  /**
569
645
  * Log debug messages if debug mode is enabled.
570
646
  */
571
- debugLog(...t) {
572
- this.config.debug && console.log(`[EmbeddedSDK v${y}]`, ...t);
647
+ debugLog(...e) {
648
+ this.config.debug && console.log(`[EmbeddedSDK v${A}]`, ...e);
573
649
  }
574
650
  /**
575
651
  * Log warnings.
576
652
  */
577
- warn(...t) {
578
- console.warn(`[EmbeddedSDK v${y}]`, ...t);
653
+ warn(...e) {
654
+ console.warn(`[EmbeddedSDK v${A}]`, ...e);
579
655
  }
580
656
  /**
581
657
  * Set up listener for theme changes from host.
582
658
  */
583
659
  setupThemeListener() {
584
- E(w.THEME_CHANGE, (t) => {
585
- this.state.layout.theme = t.theme, this.debugLog("Theme changed:", t.theme), this.themeCallbacks.forEach((n) => {
660
+ y(R.THEME_CHANGE, (e) => {
661
+ this.state.layout.theme = e.theme, this.debugLog("Theme changed:", e.theme), this.themeCallbacks.forEach((n) => {
586
662
  try {
587
- n(t.theme);
663
+ n(e.theme);
588
664
  } catch (i) {
589
665
  console.error("[EmbeddedSDK] Error in theme callback:", i);
590
666
  }
@@ -595,10 +671,10 @@ class ce {
595
671
  * Set up listeners for async response events from host.
596
672
  */
597
673
  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);
674
+ y(c.CONFIRM_RESPONSE, (e) => {
675
+ this.debugLog("Received confirm response:", e), L(e.requestId, { confirmed: e.confirmed });
676
+ }), y(c.MODAL_RESPONSE, (e) => {
677
+ this.debugLog("Received modal response:", e), L(e.requestId, e.result, e.error);
602
678
  });
603
679
  }
604
680
  /**
@@ -614,9 +690,9 @@ class ce {
614
690
  * });
615
691
  * ```
616
692
  */
617
- onThemeChange(t) {
618
- return this.themeCallbacks.add(t), () => {
619
- this.themeCallbacks.delete(t);
693
+ onThemeChange(e) {
694
+ return this.themeCallbacks.add(e), () => {
695
+ this.themeCallbacks.delete(e);
620
696
  };
621
697
  }
622
698
  /**
@@ -632,15 +708,15 @@ class ce {
632
708
  * });
633
709
  * ```
634
710
  */
635
- onInit(t) {
711
+ onInit(e) {
636
712
  if (this.config.initialized)
637
713
  try {
638
- t(this.getState());
714
+ e(this.getState());
639
715
  } catch (n) {
640
716
  console.error("[EmbeddedSDK] Error in init callback:", n);
641
717
  }
642
- return this.initCallbacks.add(t), () => {
643
- this.initCallbacks.delete(t);
718
+ return this.initCallbacks.add(e), () => {
719
+ this.initCallbacks.delete(e);
644
720
  };
645
721
  }
646
722
  /**
@@ -655,9 +731,9 @@ class ce {
655
731
  * embedded.log('error', 'Failed to load data', { endpoint: '/api/data' });
656
732
  * ```
657
733
  */
658
- log(t, n, i) {
659
- o(_.LOG, {
660
- level: t,
734
+ log(e, n, i) {
735
+ l(U.LOG, {
736
+ level: e,
661
737
  message: n,
662
738
  context: i
663
739
  });
@@ -681,7 +757,7 @@ class ce {
681
757
  this.warn("Cannot signal ready before init() is called");
682
758
  return;
683
759
  }
684
- this.appReady = !0, o(g.READY, {}), this.debugLog("Sent ready signal to host");
760
+ this.appReady = !0, l(T.READY, {}), this.debugLog("Sent ready signal to host");
685
761
  }
686
762
  /**
687
763
  * Initialize the SDK and establish connection with the host.
@@ -696,22 +772,22 @@ class ce {
696
772
  * console.log('Locale:', layout.locale);
697
773
  * ```
698
774
  */
699
- async init(t = {}) {
700
- var n, i, r, l;
775
+ async init(e = {}) {
776
+ var n, i, r, s;
701
777
  if (this.config.initialized)
702
778
  return this.debugLog("Already initialized, returning current layout"), { layout: { ...this.state.layout } };
703
779
  if (this.state.initializing)
704
780
  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,
781
+ H() || this.warn("Not running in an iframe. Some features may not work."), this.config = {
782
+ debug: e.debug ?? !1,
707
783
  initialized: !1
708
784
  }, this.state.initializing = !0, this.debugLog("Initializing SDK...");
709
785
  try {
710
- o(g.INIT, {
786
+ l(T.INIT, {
711
787
  height: document.documentElement.scrollHeight
712
788
  }), this.debugLog("Sent iframe.ready message, waiting for context...");
713
- const a = await F(
714
- w.PROVIDE
789
+ const a = await j(
790
+ R.PROVIDE
715
791
  );
716
792
  this.debugLog("Received context from host:", a), this.state = {
717
793
  ready: !0,
@@ -720,15 +796,15 @@ class ce {
720
796
  theme: ((n = a.layout) == null ? void 0 : n.theme) ?? "light",
721
797
  width: ((i = a.layout) == null ? void 0 : i.width) ?? 0,
722
798
  locale: ((r = a.layout) == null ? void 0 : r.locale) ?? "ar",
723
- currency: ((l = a.layout) == null ? void 0 : l.currency) ?? "SAR"
799
+ currency: ((s = a.layout) == null ? void 0 : s.currency) ?? "SAR"
724
800
  }
725
801
  }, this.config.initialized = !0, this.debugLog("Initialization complete. Layout:", this.state.layout);
726
- const v = this.getState();
727
- return this.initCallbacks.forEach((D) => {
802
+ const E = this.getState();
803
+ return this.initCallbacks.forEach((p) => {
728
804
  try {
729
- D(v);
730
- } catch (O) {
731
- console.error("[EmbeddedSDK] Error in init callback:", O);
805
+ p(E);
806
+ } catch (u) {
807
+ console.error("[EmbeddedSDK] Error in init callback:", u);
732
808
  }
733
809
  }), { layout: { ...this.state.layout } };
734
810
  } catch (a) {
@@ -740,11 +816,10 @@ class ce {
740
816
  * Useful when multiple calls to init() might happen.
741
817
  */
742
818
  waitForInit() {
743
- return new Promise((t) => {
744
- const n = () => {
745
- this.state.ready ? t({ layout: { ...this.state.layout } }) : setTimeout(n, 100);
746
- };
747
- n();
819
+ return new Promise((e) => {
820
+ const n = this.onInit((i) => {
821
+ n(), e({ layout: { ...i.layout } });
822
+ });
748
823
  });
749
824
  }
750
825
  /**
@@ -758,23 +833,23 @@ class ce {
758
833
  * ```
759
834
  */
760
835
  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;
836
+ 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
837
  }
763
838
  }
764
- let m = null;
765
- function de() {
766
- return m || (m = new ce()), m;
839
+ let g = null;
840
+ function Ee() {
841
+ return g || (g = new ge()), g;
767
842
  }
768
- function fe() {
769
- m && (m.destroy(), m = null);
843
+ function be() {
844
+ g && (g.destroy(), g = null);
770
845
  }
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));
846
+ const O = Ee(), Te = A;
847
+ 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
848
  export {
774
- ce as EmbeddedApp,
775
- N as embedded,
776
- de as getEmbeddedApp,
777
- fe as resetEmbeddedApp,
778
- he as version
849
+ ge as EmbeddedApp,
850
+ O as embedded,
851
+ Ee as getEmbeddedApp,
852
+ be as resetEmbeddedApp,
853
+ Te as version
779
854
  };
780
855
  //# sourceMappingURL=index.js.map