@salla.sa/embedded-sdk 0.1.0-beta.9 → 0.1.0

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