@salla.sa/embedded-sdk 0.1.0-beta.12 → 0.1.0-beta.3

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