@salla.sa/embedded-sdk 0.2.6 → 0.2.7

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
@@ -5,7 +5,7 @@ const u = "embedded::", q = {
5
5
  READY: `${u}ready`,
6
6
  /** App requests to be destroyed and navigate away */
7
7
  DESTROY: `${u}destroy`
8
- }, U = {
8
+ }, j = {
9
9
  /** Context data provision */
10
10
  PROVIDE: `${u}context.provide`,
11
11
  /** Theme change notification */
@@ -21,7 +21,7 @@ const u = "embedded::", q = {
21
21
  CONFIRM: `${u}ui.confirm`,
22
22
  /** Confirm dialog response (from host) */
23
23
  CONFIRM_RESPONSE: `${u}ui.confirm.response`
24
- }, Z = {}, X = {
24
+ }, ne = {}, Z = {
25
25
  /** Request token refresh (re-renders iframe with new token) */
26
26
  REFRESH: `${u}auth.refresh`
27
27
  }, T = {
@@ -46,7 +46,7 @@ const u = "embedded::", q = {
46
46
  REMOVE_ITEM: `${u}nav.removeItem`,
47
47
  /** Injected sub-nav item clicked (host → iframe) */
48
48
  ITEM_CLICK: `${u}nav.itemClick`
49
- }, y = {
49
+ }, v = {
50
50
  /** Create checkout flow */
51
51
  CREATE: `${u}checkout.create`,
52
52
  /** Checkout response from host */
@@ -57,13 +57,13 @@ const u = "embedded::", q = {
57
57
  GET_ADDONS_RESPONSE: `${u}checkout.getAddons.response`,
58
58
  /** Reset checkout cache on host */
59
59
  RESET_CACHE: `${u}checkout.resetCache`
60
- }, D = Z.version || "", ee = 1e4, te = [
60
+ }, M = ne.version || "", se = 1e4, ae = [
61
61
  "localhost",
62
62
  "merchants.workers.dev",
63
63
  "s.salla.sa",
64
64
  ".salla.group",
65
65
  ".salla.sa"
66
- ], re = /* @__PURE__ */ new Set([
66
+ ], oe = /* @__PURE__ */ new Set([
67
67
  "ar",
68
68
  // Arabic
69
69
  "he",
@@ -79,28 +79,28 @@ const u = "embedded::", q = {
79
79
  "yi"
80
80
  // Yiddish
81
81
  ]);
82
- function ie(t) {
82
+ function ue(t) {
83
83
  const e = (t ?? "ar").toLowerCase().split(/[-_]/)[0];
84
- return re.has(e) ? "rtl" : "ltr";
84
+ return oe.has(e) ? "rtl" : "ltr";
85
85
  }
86
86
  let N = {
87
87
  showVersion: !0,
88
88
  debug: !1
89
89
  };
90
- function ne(t) {
90
+ function ce(t) {
91
91
  N = { ...N, ...t };
92
92
  }
93
- function P(t) {
93
+ function H(t) {
94
94
  return `%c${t}`;
95
95
  }
96
- function j(t, e = "#fff") {
96
+ function K(t, e = "#fff") {
97
97
  return `background-color: ${t}; color: ${e}; padding: 2px 6px; border-radius: 3px; font-weight: 500; font-size: 11px;`;
98
98
  }
99
99
  function S(t, ...e) {
100
100
  if (t === "debug" && !N.debug)
101
101
  return;
102
102
  const r = [], i = [];
103
- r.push(P("EmbeddedSDK")), i.push(j("#10b981", "#fff")), N.showVersion && (r.push(P(`v${D}`)), i.push(j("#6b7280", "#fff")));
103
+ r.push(H("EmbeddedSDK")), i.push(K("#10b981", "#fff")), N.showVersion && (r.push(H(`v${M}`)), i.push(K("#6b7280", "#fff")));
104
104
  const n = r.join("").trim();
105
105
  (console[t] || console.log)(n, ...i, ...e);
106
106
  }
@@ -121,19 +121,19 @@ const o = {
121
121
  S("debug", ...t);
122
122
  }
123
123
  };
124
- function se(t) {
124
+ function de(t) {
125
125
  try {
126
126
  const r = new URL(t).hostname;
127
- return te.some((i) => i.startsWith(".") ? r.endsWith(i) || r === i.slice(1) : r === i || r.startsWith(`${i}:`));
127
+ return ae.some((i) => i.startsWith(".") ? r.endsWith(i) || r === i.slice(1) : r === i || r.startsWith(`${i}:`));
128
128
  } catch {
129
129
  return !1;
130
130
  }
131
131
  }
132
- function ae() {
132
+ function le() {
133
133
  return typeof window > "u" || window.parent === window ? null : window.parent;
134
134
  }
135
- function c(t, e, r = "*", i, n) {
136
- const s = ae();
135
+ function d(t, e, r = "*", i, n) {
136
+ const s = le();
137
137
  if (!s) {
138
138
  o.warn("Not running in an iframe, cannot post to host");
139
139
  return;
@@ -145,22 +145,22 @@ function c(t, e, r = "*", i, n) {
145
145
  source: "embedded-app",
146
146
  ...i && { requestId: i },
147
147
  metadata: {
148
- version: D
148
+ version: M
149
149
  }
150
150
  };
151
151
  s.postMessage(a, r);
152
152
  }
153
- const v = /* @__PURE__ */ new Map();
154
- let $ = !1;
155
- function W(t) {
156
- if (process.env.NODE_ENV === "production" && !se(t.origin))
153
+ const b = /* @__PURE__ */ new Map();
154
+ let C = !1;
155
+ function ee(t) {
156
+ if (process.env.NODE_ENV === "production" && !de(t.origin))
157
157
  return;
158
158
  const e = t.data;
159
159
  if (!e || typeof e.event != "string" || !e.payload || typeof e.timestamp != "number" || !e.source) {
160
160
  o.warn("Invalid message structure received:", e);
161
161
  return;
162
162
  }
163
- const r = v.get(e.event);
163
+ const r = b.get(e.event);
164
164
  r && r.forEach((n) => {
165
165
  try {
166
166
  n(e);
@@ -168,7 +168,7 @@ function W(t) {
168
168
  o.error("Error in message handler:", s);
169
169
  }
170
170
  });
171
- const i = v.get("*");
171
+ const i = b.get("*");
172
172
  i && i.forEach((n) => {
173
173
  try {
174
174
  n(e);
@@ -177,41 +177,41 @@ function W(t) {
177
177
  }
178
178
  });
179
179
  }
180
- function oe() {
181
- $ || typeof window > "u" || (window.addEventListener("message", W), $ = !0);
180
+ function fe() {
181
+ C || typeof window > "u" || (window.addEventListener("message", ee), C = !0);
182
182
  }
183
- function b(t, e) {
184
- oe(), v.has(t) || v.set(t, /* @__PURE__ */ new Set());
185
- const r = v.get(t);
183
+ function y(t, e) {
184
+ fe(), b.has(t) || b.set(t, /* @__PURE__ */ new Set());
185
+ const r = b.get(t);
186
186
  return r.add(e), () => {
187
- r.delete(e), r.size === 0 && v.delete(t);
187
+ r.delete(e), r.size === 0 && b.delete(t);
188
188
  };
189
189
  }
190
- function ue(t, e = ee) {
190
+ function he(t, e = se) {
191
191
  return new Promise((r, i) => {
192
192
  const n = setTimeout(() => {
193
193
  s(), i(new Error(`[EmbeddedSDK] Timeout waiting for "${t}" message`));
194
- }, e), s = b(t, (a) => {
194
+ }, e), s = y(t, (a) => {
195
195
  clearTimeout(n), s(), r(a);
196
196
  });
197
197
  });
198
198
  }
199
- function de() {
200
- v.clear(), $ && typeof window < "u" && (window.removeEventListener("message", W), $ = !1);
199
+ function me() {
200
+ b.clear(), C && typeof window < "u" && (window.removeEventListener("message", ee), C = !1);
201
201
  }
202
- function ce() {
202
+ function pe() {
203
203
  return typeof window > "u" ? !1 : window.parent !== window;
204
204
  }
205
- function le(t, e, r) {
205
+ function ge(t, e, r) {
206
206
  const i = {
207
207
  event: t,
208
208
  payload: e,
209
209
  timestamp: Date.now(),
210
210
  source: "merchant-dashboard",
211
211
  ...r,
212
- metadata: { version: D, synthetic: !0 }
212
+ metadata: { version: M, synthetic: !0 }
213
213
  };
214
- v.get(t)?.forEach((s) => {
214
+ b.get(t)?.forEach((s) => {
215
215
  try {
216
216
  s(i);
217
217
  } catch (a) {
@@ -219,13 +219,13 @@ function le(t, e, r) {
219
219
  }
220
220
  });
221
221
  }
222
- const E = /* @__PURE__ */ new Map(), fe = 3e4;
223
- function Y() {
222
+ const E = /* @__PURE__ */ new Map(), ve = 3e4;
223
+ function te() {
224
224
  const t = Date.now(), e = Math.random().toString(36).slice(2, 9);
225
225
  return `req_${t}_${e}`;
226
226
  }
227
- function x(t, e = {}, r = fe) {
228
- const i = Y();
227
+ function V(t, e = {}, r = ve) {
228
+ const i = te();
229
229
  return new Promise((n, s) => {
230
230
  const a = setTimeout(() => {
231
231
  E.get(i) && (E.delete(i), s(
@@ -239,10 +239,10 @@ function x(t, e = {}, r = fe) {
239
239
  reject: s,
240
240
  timeout: a,
241
241
  event: t
242
- }), c(t, e, "*", i);
242
+ }), d(t, e, "*", i);
243
243
  });
244
244
  }
245
- function C(t, e, r) {
245
+ function $(t, e, r) {
246
246
  const i = E.get(t);
247
247
  if (!i) {
248
248
  o.warn(`Received response for unknown request: ${t}`);
@@ -250,14 +250,14 @@ function C(t, e, r) {
250
250
  }
251
251
  clearTimeout(i.timeout), E.delete(t), r ? i.reject(new Error(r)) : i.resolve(e);
252
252
  }
253
- function he(t = "SDK cleanup") {
253
+ function be(t = "SDK cleanup") {
254
254
  E.forEach((e, r) => {
255
255
  clearTimeout(e.timeout), e.reject(
256
256
  new Error(`[EmbeddedSDK] Request ${r} cancelled: ${t}`)
257
257
  );
258
258
  }), E.clear();
259
259
  }
260
- function A() {
260
+ function R() {
261
261
  const t = /* @__PURE__ */ new Set();
262
262
  return {
263
263
  subscribe(e) {
@@ -282,18 +282,18 @@ function A() {
282
282
  }
283
283
  };
284
284
  }
285
- const me = "https://api.salla.dev";
286
- class w extends Error {
285
+ const ye = "https://api.salla.dev";
286
+ class A extends Error {
287
287
  constructor(e, r, i) {
288
288
  super(e), this.status = r, this.response = i, this.name = "ApiError";
289
289
  }
290
290
  }
291
- async function pe(t, e = {}) {
292
- const { method: r = "GET", headers: i = {}, body: n, timeout: s = 3e4 } = e, a = `${me}${t}`, l = new AbortController(), f = setTimeout(() => {
291
+ async function Ee(t, e = {}) {
292
+ const { method: r = "GET", headers: i = {}, body: n, timeout: s = 3e4 } = e, a = `${ye}${t}`, l = new AbortController(), f = setTimeout(() => {
293
293
  l.abort();
294
294
  }, s);
295
295
  try {
296
- const d = await fetch(a, {
296
+ const c = await fetch(a, {
297
297
  method: r,
298
298
  headers: {
299
299
  "Content-Type": "application/json",
@@ -304,18 +304,18 @@ async function pe(t, e = {}) {
304
304
  });
305
305
  clearTimeout(f);
306
306
  let m;
307
- if (d.headers.get("content-type")?.includes("application/json") ? m = await d.json() : m = await d.text(), !d.ok)
308
- throw new w(
309
- `API request failed: ${d.statusText}`,
310
- d.status,
307
+ if (c.headers.get("content-type")?.includes("application/json") ? m = await c.json() : m = await c.text(), !c.ok)
308
+ throw new A(
309
+ `API request failed: ${c.statusText}`,
310
+ c.status,
311
311
  m
312
312
  );
313
313
  return m;
314
- } catch (d) {
315
- throw clearTimeout(f), d instanceof w ? d : d instanceof Error ? d.name === "AbortError" ? new w(`Request timeout after ${s}ms`) : new w(`Request failed: ${d.message}`) : new w("Unknown error occurred");
314
+ } catch (c) {
315
+ throw clearTimeout(f), c instanceof A ? c : c instanceof Error ? c.name === "AbortError" ? new A(`Request timeout after ${s}ms`) : new A(`Request failed: ${c.message}`) : new A("Unknown error occurred");
316
316
  }
317
317
  }
318
- function k(t) {
318
+ function x(t) {
319
319
  return {
320
320
  isVerified: !1,
321
321
  isError: !0,
@@ -323,18 +323,18 @@ function k(t) {
323
323
  data: null
324
324
  };
325
325
  }
326
- async function ge(t) {
326
+ async function Te(t) {
327
327
  const { token: e, appId: r, refreshOnError: i = !0 } = t;
328
328
  if (!e) {
329
329
  const n = "Token is required. Provide it as a parameter or in URL as ?token=XXX";
330
- return o.error("Error in introspect:", n), k(n);
330
+ return o.error("Error in introspect:", n), x(n);
331
331
  }
332
332
  if (!r) {
333
333
  const n = "App ID is required. Provide it as a parameter or in URL as ?app_id=XXX";
334
- return o.error("Error in introspect:", n), k(n);
334
+ return o.error("Error in introspect:", n), x(n);
335
335
  }
336
336
  try {
337
- const n = await pe(
337
+ const n = await Ee(
338
338
  "/exchange-authority/v1/introspect",
339
339
  {
340
340
  method: "POST",
@@ -357,12 +357,12 @@ async function ge(t) {
357
357
  data: s ? n.data : null
358
358
  };
359
359
  } catch (n) {
360
- i && (J().ui.toast.error(n?.toString() ?? "Introspect error"), c(X.REFRESH, {})), o.error("Error in introspect:", n);
360
+ i && (ie().ui.toast.error(n?.toString() ?? "Introspect error"), d(Z.REFRESH, {})), o.error("Error in introspect:", n);
361
361
  const s = n instanceof Error ? n.message : n;
362
- return k(s);
362
+ return x(s);
363
363
  }
364
364
  }
365
- function ve(t) {
365
+ function Ie(t) {
366
366
  return {
367
367
  /**
368
368
  * Get the token from the URL query parameter.
@@ -397,7 +397,7 @@ function ve(t) {
397
397
  * ```
398
398
  */
399
399
  refresh() {
400
- c(X.REFRESH, {});
400
+ d(Z.REFRESH, {});
401
401
  },
402
402
  /**
403
403
  * Introspect (verify) a short-lived token with Salla's API.
@@ -408,7 +408,7 @@ function ve(t) {
408
408
  */
409
409
  async introspect(e = {}) {
410
410
  const r = e.token ?? this.getToken() ?? "", i = e.appId ?? this.getAppId() ?? "";
411
- return ge({
411
+ return Te({
412
412
  token: r,
413
413
  appId: i,
414
414
  refreshOnError: e.refreshOnError
@@ -417,43 +417,49 @@ function ve(t) {
417
417
  };
418
418
  }
419
419
  const F = ["success", "error", "warning", "info"];
420
- function be(t) {
420
+ function Se(t) {
421
421
  const e = [];
422
422
  return t.type === void 0 || t.type === null ? e.push("Toast type is required") : (typeof t.type != "string" || !F.includes(t.type)) && e.push(
423
423
  `Invalid toast type "${t.type}". Expected: ${F.join(" | ")}`
424
424
  ), 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 };
425
425
  }
426
- function H(t, e) {
426
+ function G(t, e) {
427
427
  const r = [];
428
428
  return typeof t != "object" || t === null ? (r.push(`${e} must be an object`), r) : ((typeof t.type != "string" || t.type.trim() === "") && r.push(`${e} must have a valid type`), (typeof t.slug != "string" || t.slug.trim() === "") && r.push(`${e} must have a valid slug`), t.quantity !== void 0 && (typeof t.quantity != "number" || t.quantity < 1) && r.push(`${e} must have a quantity >= 1`), r);
429
429
  }
430
- function ye(t) {
430
+ const Ae = /^[A-Za-z0-9_-]+$/, B = 64, U = "checkout_id cannot be combined with item or items";
431
+ function we(t) {
432
+ return typeof t != "string" || t === "" ? ["checkout_id must be a non-empty string"] : t.length > B ? [`checkout_id must be at most ${B} characters`] : Ae.test(t) ? [] : ["checkout_id may only contain letters, numbers, '_' and '-'"];
433
+ }
434
+ function X(t) {
431
435
  const e = [];
432
436
  if (typeof t != "object" || t === null)
433
437
  return e.push("Checkout options must be an object"), { valid: !1, errors: e };
434
438
  const r = "item" in t, i = "items" in t;
439
+ if ("checkout_id" in t)
440
+ return r || i ? (e.push(U), { valid: !1, errors: e }) : (e.push(...we(t.checkout_id)), { valid: e.length === 0, errors: e });
435
441
  if (!r && !i)
436
442
  return e.push("Checkout requires either 'item' or 'items'"), { valid: !1, errors: e };
437
443
  if (r) {
438
- const n = H(
444
+ const n = G(
439
445
  t.item,
440
446
  "Item"
441
447
  );
442
448
  return e.push(...n), { valid: e.length === 0, errors: e };
443
449
  }
444
450
  return Array.isArray(t.items) ? t.items.length === 0 ? (e.push("At least one item is required"), { valid: !1, errors: e }) : (t.items.forEach((n, s) => {
445
- const a = H(
451
+ const a = G(
446
452
  n,
447
453
  `Item at index ${s}`
448
454
  );
449
455
  e.push(...a);
450
456
  }), { valid: e.length === 0, errors: e }) : (e.push("Checkout items must be an array"), { valid: !1, errors: e });
451
457
  }
452
- function Ee(t) {
458
+ function Re(t) {
453
459
  const e = [];
454
460
  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 };
455
461
  }
456
- function Te(t) {
462
+ function _e(t) {
457
463
  const e = [];
458
464
  if (t.url === void 0 || t.url === null)
459
465
  e.push("Redirect URL is required");
@@ -469,7 +475,7 @@ function Te(t) {
469
475
  }
470
476
  return { valid: e.length === 0, errors: e };
471
477
  }
472
- function Ie(t) {
478
+ function Ne(t) {
473
479
  const e = [];
474
480
  return t.title ? typeof t.title != "string" && e.push("Nav action title must be a string") : e.push("Nav action title is required"), t.value ? typeof t.value != "string" && e.push("Nav action value must be a string") : e.push("Nav action value is required"), 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((r, i) => {
475
481
  if (typeof r != "object" || r === null) {
@@ -490,26 +496,26 @@ function Ie(t) {
490
496
  );
491
497
  }) : e.push("Nav action extendedActions must be an array")), { valid: e.length === 0, errors: e };
492
498
  }
493
- const _ = 50;
494
- function R(t, e) {
499
+ const D = 50;
500
+ function _(t, e) {
495
501
  return t ? `${t}.${e}` : e;
496
502
  }
497
- function Se(t, e) {
503
+ function Ce(t, e) {
498
504
  const r = [];
499
505
  ["title", "value", "url"].forEach((s) => {
500
506
  (typeof t[s] != "string" || !String(t[s]).trim()) && r.push(
501
- `Sub-nav addItem "${R(e, s)}" must be non-empty string`
507
+ `Sub-nav addItem "${_(e, s)}" must be non-empty string`
502
508
  );
503
509
  });
504
- const i = R(e, "disabled"), n = R(e, "active");
510
+ const i = _(e, "disabled"), n = _(e, "active");
505
511
  return t.disabled !== void 0 && typeof t.disabled != "boolean" && r.push(`Sub-nav addItem "${i}" must be boolean`), t.active !== void 0 && typeof t.active != "boolean" && r.push(`Sub-nav addItem "${n}" must be boolean`), r;
506
512
  }
507
513
  function z(t, e, r) {
508
514
  if (typeof t != "object" || t === null)
509
515
  return [`Sub-nav addItem "${e || "item"}" must be an object`];
510
- const i = t, n = Se(i, e), s = R(e, "children");
511
- return i.children === void 0 ? n : r ? Array.isArray(i.children) ? i.children.length > _ ? (n.push(
512
- `Sub-nav addItem "${s}" must have at most ${_} items`
516
+ const i = t, n = Ce(i, e), s = _(e, "children");
517
+ return i.children === void 0 ? n : r ? Array.isArray(i.children) ? i.children.length > D ? (n.push(
518
+ `Sub-nav addItem "${s}" must have at most ${D} items`
513
519
  ), n) : (i.children.forEach((a, l) => {
514
520
  const f = e ? `${e}.children[${l}]` : `children[${l}]`;
515
521
  n.push(...z(a, f, !1));
@@ -517,24 +523,24 @@ function z(t, e, r) {
517
523
  `Sub-nav addItem "${s}" is not supported on submenu rows (only one dropdown level)`
518
524
  ), n);
519
525
  }
520
- function Q(t, e, r, i) {
526
+ function re(t, e, r, i) {
521
527
  if (typeof t != "object" || t === null)
522
528
  return i;
523
529
  const n = t;
524
530
  return typeof n.value == "string" && n.value.trim() && i.push(n.value.trim()), !r || !Array.isArray(n.children) || n.children.forEach((s, a) => {
525
531
  const l = e ? `${e}.children[${a}]` : `children[${a}]`;
526
- Q(s, l, !1, i);
532
+ re(s, l, !1, i);
527
533
  }), i;
528
534
  }
529
- function we(t) {
530
- const e = Q(t, "", !0, []), r = /* @__PURE__ */ new Set(), i = [];
535
+ function $e(t) {
536
+ const e = re(t, "", !0, []), r = /* @__PURE__ */ new Set(), i = [];
531
537
  for (const n of e)
532
538
  r.has(n) && i.push(n), r.add(n);
533
539
  return i.length === 0 ? [] : [
534
540
  `Sub-nav addItem "value" must be globally unique within the item tree (duplicate: ${[...new Set(i)].join(", ")})`
535
541
  ];
536
542
  }
537
- function Ae(t) {
543
+ function De(t) {
538
544
  if (typeof t != "object" || t === null)
539
545
  return {
540
546
  valid: !1,
@@ -542,11 +548,11 @@ function Ae(t) {
542
548
  };
543
549
  const e = [
544
550
  ...z(t, "", !0),
545
- ...we(t)
551
+ ...$e(t)
546
552
  ];
547
553
  return { valid: e.length === 0, errors: e };
548
554
  }
549
- function Re(t) {
555
+ function Me(t) {
550
556
  const e = [];
551
557
  if (typeof t != "object" || t === null)
552
558
  return e.push("Nav updateItem expects an object item"), { valid: !1, errors: e };
@@ -563,43 +569,43 @@ function Re(t) {
563
569
  return e.push(
564
570
  "Sub-nav updateItem must include at least one of title, url, disabled, active, children"
565
571
  ), { valid: !1, errors: e };
566
- if (["title", "url"].forEach((d) => {
567
- r[d] !== void 0 && (typeof r[d] != "string" || !r[d].trim()) && e.push(`Sub-nav updateItem "${d}" must be non-empty`);
572
+ if (["title", "url"].forEach((c) => {
573
+ r[c] !== void 0 && (typeof r[c] != "string" || !r[c].trim()) && e.push(`Sub-nav updateItem "${c}" must be non-empty`);
568
574
  }), r.disabled !== void 0 && typeof r.disabled != "boolean" && e.push('Sub-nav updateItem "disabled" must be boolean'), r.active !== void 0 && typeof r.active != "boolean" && e.push('Sub-nav updateItem "active" must be boolean'), r.children !== void 0)
569
575
  if (!Array.isArray(r.children))
570
576
  e.push('Sub-nav updateItem "children" must be an array');
571
- else if (r.children.length > _)
577
+ else if (r.children.length > D)
572
578
  e.push(
573
- `Sub-nav updateItem "children" must have at most ${_} items`
579
+ `Sub-nav updateItem "children" must have at most ${D} items`
574
580
  );
575
581
  else {
576
- const d = /* @__PURE__ */ new Set();
577
- r.children.forEach((m, V) => {
578
- const M = `children[${V}]`;
579
- if (e.push(...z(m, M, !1)), typeof m == "object" && m !== null) {
580
- const O = m.value;
581
- if (typeof O == "string" && O.trim()) {
582
- const L = O.trim();
583
- L === l && e.push(
584
- `Sub-nav updateItem "${M}.value" must not equal the parent row identifier`
585
- ), d.has(L) && e.push(
586
- `Sub-nav updateItem "${M}.value" must be unique within children`
587
- ), d.add(L);
582
+ const c = /* @__PURE__ */ new Set();
583
+ r.children.forEach((m, P) => {
584
+ const O = `children[${P}]`;
585
+ if (e.push(...z(m, O, !1)), typeof m == "object" && m !== null) {
586
+ const L = m.value;
587
+ if (typeof L == "string" && L.trim()) {
588
+ const k = L.trim();
589
+ k === l && e.push(
590
+ `Sub-nav updateItem "${O}.value" must not equal the parent row identifier`
591
+ ), c.has(k) && e.push(
592
+ `Sub-nav updateItem "${O}.value" must be unique within children`
593
+ ), c.add(k);
588
594
  }
589
595
  }
590
596
  });
591
597
  }
592
598
  return { valid: e.length === 0, errors: e };
593
599
  }
594
- function Ne(t) {
600
+ function Oe(t) {
595
601
  const e = [];
596
602
  return (typeof t != "string" || !t.trim()) && e.push("Nav removeItem value must be non-empty string"), { valid: e.length === 0, errors: e };
597
603
  }
598
- const K = ["danger", "warning", "info"];
599
- function $e(t) {
604
+ const W = ["danger", "warning", "info"];
605
+ function Le(t) {
600
606
  const e = [];
601
- 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" || !K.includes(t.variant)) && e.push(
602
- `Invalid confirm variant "${t.variant}". Expected: ${K.join(" | ")}`
607
+ 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" || !W.includes(t.variant)) && e.push(
608
+ `Invalid confirm variant "${t.variant}". Expected: ${W.join(" | ")}`
603
609
  ), { valid: e.length === 0, errors: e };
604
610
  }
605
611
  function p(t, e) {
@@ -609,34 +615,34 @@ function p(t, e) {
609
615
  `)
610
616
  );
611
617
  }
612
- function Ce() {
618
+ function ke() {
613
619
  return { resize: (i) => {
614
620
  }, autoResize: () => {
615
621
  }, stopAutoResize: () => {
616
622
  } };
617
623
  }
618
- function _e() {
619
- const { resize: t, autoResize: e, stopAutoResize: r } = Ce();
624
+ function qe() {
625
+ const { resize: t, autoResize: e, stopAutoResize: r } = ke();
620
626
  return {
621
627
  navigate(i, n) {
622
- const s = Ee({ path: i, ...n });
628
+ const s = Re({ path: i, ...n });
623
629
  if (!s.valid) {
624
630
  p(T.NAVIGATE, s.errors);
625
631
  return;
626
632
  }
627
- c(T.NAVIGATE, {
633
+ d(T.NAVIGATE, {
628
634
  path: i,
629
635
  state: n?.state,
630
636
  replace: n?.replace
631
637
  });
632
638
  },
633
639
  redirect(i) {
634
- const n = Te({ url: i });
640
+ const n = _e({ url: i });
635
641
  if (!n.valid) {
636
642
  p(T.REDIRECT, n.errors);
637
643
  return;
638
644
  }
639
- c(T.REDIRECT, { url: i });
645
+ d(T.REDIRECT, { url: i });
640
646
  },
641
647
  navTo(i, n) {
642
648
  if (i.startsWith("http://") || i.startsWith("https://")) {
@@ -652,26 +658,26 @@ function _e() {
652
658
  ]);
653
659
  return;
654
660
  }
655
- c(T.SET_TITLE, { title: i });
661
+ d(T.SET_TITLE, { title: i });
656
662
  },
657
663
  resize: t,
658
664
  autoResize: e,
659
665
  stopAutoResize: r
660
666
  };
661
667
  }
662
- function De() {
663
- const t = A(), e = A(), r = [];
668
+ function xe() {
669
+ const t = R(), e = R(), r = [];
664
670
  return r.push(
665
- b(h.ACTION_CLICK, (i) => {
671
+ y(h.ACTION_CLICK, (i) => {
666
672
  t.notify(i.payload.value);
667
673
  })
668
674
  ), r.push(
669
- b(h.ADD_ITEM_RESPONSE, (i) => {
675
+ y(h.ADD_ITEM_RESPONSE, (i) => {
670
676
  if (!i.requestId) return;
671
677
  const n = i.payload;
672
678
  if (typeof n.error == "string" && n.error.trim()) {
673
679
  const a = n.error.trim();
674
- o.warn(`embedded::nav.addItem failed: ${a}`), C(
680
+ o.warn(`embedded::nav.addItem failed: ${a}`), $(
675
681
  i.requestId,
676
682
  {},
677
683
  a
@@ -684,11 +690,11 @@ function De() {
684
690
  value: a,
685
691
  id: a
686
692
  };
687
- C(i.requestId, l);
693
+ $(i.requestId, l);
688
694
  }
689
695
  })
690
696
  ), r.push(
691
- b(h.ITEM_CLICK, (i) => {
697
+ y(h.ITEM_CLICK, (i) => {
692
698
  const n = i.payload, s = typeof n.value == "string" && n.value.trim() ? n.value.trim() : typeof n.id == "string" && n.id.trim() ? n.id.trim() : "";
693
699
  if (!s) {
694
700
  e.notify(n);
@@ -702,12 +708,12 @@ function De() {
702
708
  })
703
709
  ), {
704
710
  setAction(i) {
705
- const n = Ie(i);
711
+ const n = Ne(i);
706
712
  if (!n.valid) {
707
713
  p(h.SET_ACTION, n.errors);
708
714
  return;
709
715
  }
710
- c(h.SET_ACTION, {
716
+ d(h.SET_ACTION, {
711
717
  title: i.title,
712
718
  value: i.value,
713
719
  subTitle: i.subTitle,
@@ -723,33 +729,33 @@ function De() {
723
729
  });
724
730
  },
725
731
  clearAction() {
726
- c(h.CLEAR_ACTION, {});
732
+ d(h.CLEAR_ACTION, {});
727
733
  },
728
734
  onActionClick(i) {
729
735
  return t.subscribe(i);
730
736
  },
731
737
  addNavItem(i) {
732
- const n = Ae(i);
733
- return n.valid ? x(h.ADD_ITEM, { item: i }, 2e3) : (p(h.ADD_ITEM, n.errors), Promise.reject(new Error(n.errors[0])));
738
+ const n = De(i);
739
+ return n.valid ? V(h.ADD_ITEM, { item: i }, 2e3) : (p(h.ADD_ITEM, n.errors), Promise.reject(new Error(n.errors[0])));
734
740
  },
735
741
  updateNavItem(i) {
736
- const n = Re(i);
742
+ const n = Me(i);
737
743
  if (!n.valid) {
738
744
  p(h.UPDATE_ITEM, n.errors);
739
745
  return;
740
746
  }
741
- const s = i, a = typeof s.value == "string" ? s.value.trim() : "", l = typeof s.id == "string" ? s.id.trim() : "", f = a || l, { id: d, ...m } = s;
742
- c(h.UPDATE_ITEM, {
747
+ const s = i, a = typeof s.value == "string" ? s.value.trim() : "", l = typeof s.id == "string" ? s.id.trim() : "", f = a || l, { id: c, ...m } = s;
748
+ d(h.UPDATE_ITEM, {
743
749
  item: { ...m, value: f }
744
750
  });
745
751
  },
746
752
  removeNavItem(i) {
747
- const n = Ne(i);
753
+ const n = Oe(i);
748
754
  if (!n.valid) {
749
755
  p(h.REMOVE_ITEM, n.errors);
750
756
  return;
751
757
  }
752
- c(h.REMOVE_ITEM, { value: i });
758
+ d(h.REMOVE_ITEM, { value: i });
753
759
  },
754
760
  onNavItemClick(i) {
755
761
  return e.subscribe(i);
@@ -759,40 +765,40 @@ function De() {
759
765
  }
760
766
  };
761
767
  }
762
- function Me() {
768
+ function Ue() {
763
769
  return {
764
770
  /**
765
771
  * Show loading indicator.
766
772
  */
767
773
  show() {
768
- c(g.LOADING, { action: "show" });
774
+ d(g.LOADING, { action: "show" });
769
775
  },
770
776
  /**
771
777
  * Hide loading indicator.
772
778
  */
773
779
  hide() {
774
- c(g.LOADING, { action: "hide" });
780
+ d(g.LOADING, { action: "hide" });
775
781
  }
776
782
  };
777
783
  }
778
- function Oe() {
784
+ function Ve() {
779
785
  return {
780
786
  hide() {
781
- c(g.BREADCRUMBS, { action: "hide" });
787
+ d(g.BREADCRUMBS, { action: "hide" });
782
788
  },
783
789
  show() {
784
- c(g.BREADCRUMBS, { action: "show" });
790
+ d(g.BREADCRUMBS, { action: "show" });
785
791
  }
786
792
  };
787
793
  }
788
- function Le() {
794
+ function ze() {
789
795
  const t = (e) => {
790
- const r = be(e);
796
+ const r = Se(e);
791
797
  if (!r.valid) {
792
798
  p(g.TOAST, r.errors);
793
799
  return;
794
800
  }
795
- c(g.TOAST, {
801
+ d(g.TOAST, {
796
802
  type: e.type,
797
803
  message: e.message,
798
804
  duration: e.duration
@@ -829,10 +835,10 @@ function Le() {
829
835
  }
830
836
  };
831
837
  }
832
- function qe() {
838
+ function Pe() {
833
839
  return async (t) => {
834
- const e = $e(t);
835
- return e.valid ? x(g.CONFIRM, {
840
+ const e = Le(t);
841
+ return e.valid ? V(g.CONFIRM, {
836
842
  title: t.title,
837
843
  message: t.message,
838
844
  confirmText: t.confirmText ?? "Confirm",
@@ -841,31 +847,41 @@ function qe() {
841
847
  }) : (p(g.CONFIRM, e.errors), Promise.reject(new Error(e.errors.join(", "))));
842
848
  };
843
849
  }
844
- function ke() {
850
+ function je() {
845
851
  return {
846
- loading: Me(),
847
- breadcrumbs: Oe(),
848
- toast: Le(),
849
- confirm: qe()
852
+ loading: Ue(),
853
+ breadcrumbs: Ve(),
854
+ toast: ze(),
855
+ confirm: Pe()
850
856
  };
851
857
  }
852
- function xe() {
853
- const t = A(), e = [];
858
+ const He = "INVALID_CHECKOUT_INPUT";
859
+ function w(t) {
860
+ const e = new Error(t);
861
+ return e.code = He, e;
862
+ }
863
+ const Ke = ["type", "slug", "quantity"];
864
+ function Y(t) {
865
+ return typeof t == "object" && t !== null && !Array.isArray(t) && "checkout_id" in t && t.checkout_id !== void 0;
866
+ }
867
+ function Fe() {
868
+ const t = R(), e = [];
854
869
  return e.push(
855
- b(y.RESPONSE, (r) => {
870
+ y(v.RESPONSE, (r) => {
856
871
  t.notify({
857
872
  success: r.payload.success,
858
873
  order_id: r.payload.order_id,
874
+ checkout_id: r.payload.checkout_id,
859
875
  status: r.payload.status,
860
876
  error: r.payload.error,
861
877
  context: r.payload.context
862
878
  });
863
879
  })
864
880
  ), e.push(
865
- b(
866
- y.GET_ADDONS_RESPONSE,
881
+ y(
882
+ v.GET_ADDONS_RESPONSE,
867
883
  (r) => {
868
- r.requestId && C(r.requestId, {
884
+ r.requestId && $(r.requestId, {
869
885
  success: r.payload.success,
870
886
  addons: r.payload.addons,
871
887
  error: r.payload.error
@@ -874,21 +890,42 @@ function xe() {
874
890
  )
875
891
  ), {
876
892
  create(r, i) {
877
- const n = Array.isArray(r) ? r : [r], s = ye({ items: n });
878
- if (!s.valid)
879
- throw p(y.CREATE, s.errors), new Error(s.errors[0]);
880
- c(
881
- y.CREATE,
893
+ if (Array.isArray(r) && r.some((s) => Y(s)))
894
+ throw w(U);
895
+ let n;
896
+ if (Y(r)) {
897
+ if ("context" in r)
898
+ throw w(
899
+ "context must be passed as the second argument (config.context)"
900
+ );
901
+ if (Ke.some((a) => a in r))
902
+ throw w(U);
903
+ const s = X(
904
+ r
905
+ );
906
+ if (!s.valid)
907
+ throw p(v.CREATE, s.errors), w(s.errors[0]);
908
+ n = { checkout_id: r.checkout_id };
909
+ } else {
910
+ const s = Array.isArray(r) ? r : [r], a = X({ items: s });
911
+ if (!a.valid)
912
+ throw p(v.CREATE, a.errors), w(a.errors[0]);
913
+ n = {
914
+ items: s.map((l) => ({
915
+ type: l.type,
916
+ slug: l.slug,
917
+ quantity: l.quantity ?? 1
918
+ }))
919
+ };
920
+ }
921
+ d(
922
+ v.CREATE,
882
923
  {
883
- items: n.map((a) => ({
884
- type: a.type,
885
- slug: a.slug,
886
- quantity: a.quantity ?? 1
887
- })),
924
+ ...n,
888
925
  ...i?.context !== void 0 && { context: i.context }
889
926
  },
890
927
  "*",
891
- Y()
928
+ te()
892
929
  );
893
930
  },
894
931
  onResult(r) {
@@ -896,8 +933,8 @@ function xe() {
896
933
  },
897
934
  async getAddons() {
898
935
  try {
899
- return await x(
900
- y.GET_ADDONS,
936
+ return await V(
937
+ v.GET_ADDONS,
901
938
  {},
902
939
  3e4
903
940
  );
@@ -912,28 +949,29 @@ function xe() {
912
949
  }
913
950
  },
914
951
  resetCache() {
915
- c(y.RESET_CACHE, {}, "*");
952
+ d(v.RESET_CACHE, {}, "*");
916
953
  },
917
954
  destroy() {
918
955
  e.forEach((r) => r()), e.length = 0, t.clear();
919
956
  }
920
957
  };
921
958
  }
922
- const G = {
959
+ const Q = {
923
960
  theme: "light",
924
961
  dir: "rtl",
925
962
  width: 0,
926
963
  locale: "ar",
927
964
  currency: "SAR"
928
965
  };
929
- class ze {
966
+ class Ge {
930
967
  constructor() {
931
- this.initialized = !1, this.initializing = !1, this.debugMode = !1, this.appReady = !1, this.layout = { ...G }, this.postInitHooks = [], this.themeSubscription = A(), this.initSubscription = A(), this.auth = ve(), this.page = _e(), this.nav = De(), this.ui = ke(), this.checkout = xe(), this.registerPostInitHook((e) => {
968
+ this.initialized = !1, this.initializing = !1, this.debugMode = !1, this.appReady = !1, this.layout = { ...Q }, this.postInitHooks = [], this.themeSubscription = R(), this.initSubscription = R(), this.auth = Ie(), this.page = qe(), this.nav = xe(), this.ui = je(), this.checkout = Fe(), this.registerPostInitHook((e) => {
932
969
  const r = e.payload.pendingCheckoutResult;
933
970
  r && (o.debug("Dispatching pending checkout result:", r), queueMicrotask(() => {
934
- le(y.RESPONSE, {
971
+ ge(v.RESPONSE, {
935
972
  success: r.success,
936
973
  status: r.status,
974
+ checkout_id: r.checkout_id,
937
975
  error: r.error,
938
976
  context: r.context
939
977
  });
@@ -951,10 +989,10 @@ class ze {
951
989
  * Set up core event listeners.
952
990
  */
953
991
  setupListeners() {
954
- b(U.THEME_CHANGE, (e) => {
992
+ y(j.THEME_CHANGE, (e) => {
955
993
  this.layout.theme = e.payload.theme, o.debug("Theme changed:", e.payload.theme), this.themeSubscription.notify(e.payload.theme);
956
- }), b(g.CONFIRM_RESPONSE, (e) => {
957
- o.debug("Received confirm response:", e), e.requestId && C(e.requestId, { confirmed: e.payload.confirmed });
994
+ }), y(g.CONFIRM_RESPONSE, (e) => {
995
+ o.debug("Received confirm response:", e), e.requestId && $(e.requestId, { confirmed: e.payload.confirmed });
958
996
  });
959
997
  }
960
998
  /**
@@ -1003,7 +1041,7 @@ class ze {
1003
1041
  o.warn("Cannot signal ready before init() is called");
1004
1042
  return;
1005
1043
  }
1006
- this.appReady = !0, c(q.READY, {}), o.debug("Sent ready signal to host");
1044
+ this.appReady = !0, d(q.READY, {}), o.debug("Sent ready signal to host");
1007
1045
  }
1008
1046
  /**
1009
1047
  * Initialize the SDK and establish connection with the host.
@@ -1017,19 +1055,19 @@ class ze {
1017
1055
  i(), r({ layout: { ...n.layout } });
1018
1056
  });
1019
1057
  });
1020
- this.initializing = !0, this.debugMode = e.debug ?? !1, ne({ debug: this.debugMode }), ce() || o.warn("Not running in an iframe. Some features may not work."), o.debug("Initializing SDK...");
1058
+ this.initializing = !0, this.debugMode = e.debug ?? !1, ce({ debug: this.debugMode }), pe() || o.warn("Not running in an iframe. Some features may not work."), o.debug("Initializing SDK...");
1021
1059
  try {
1022
- c(q.INIT, {
1060
+ d(q.INIT, {
1023
1061
  height: document.documentElement.scrollHeight
1024
1062
  }), o.debug("Sent iframe.ready message, waiting for context...");
1025
- const r = await ue(
1026
- U.PROVIDE
1063
+ const r = await he(
1064
+ j.PROVIDE
1027
1065
  );
1028
1066
  o.debug("Received context from host:", r);
1029
1067
  const { layout: i } = r.payload;
1030
1068
  return this.layout = {
1031
1069
  theme: i?.theme ?? "light",
1032
- dir: i?.dir ?? ie(i?.locale),
1070
+ dir: i?.dir ?? ue(i?.locale),
1033
1071
  width: i?.width ?? 0,
1034
1072
  locale: i?.locale ?? "ar",
1035
1073
  currency: i?.currency ?? "SAR"
@@ -1048,23 +1086,24 @@ class ze {
1048
1086
  * Destroy the SDK instance and clean up resources.
1049
1087
  */
1050
1088
  destroy() {
1051
- o.debug("Destroying SDK instance"), this.initialized && (c(q.DESTROY, {}), o.debug("Sent destroy event to host")), this.checkout.destroy(), this.nav.destroy(), he("SDK destroyed"), de(), this.themeSubscription.clear(), this.initSubscription.clear(), this.postInitHooks = [], this.initialized = !1, this.initializing = !1, this.appReady = !1, this.layout = { ...G };
1089
+ o.debug("Destroying SDK instance"), this.initialized && (d(q.DESTROY, {}), o.debug("Sent destroy event to host")), this.checkout.destroy(), this.nav.destroy(), be("SDK destroyed"), me(), this.themeSubscription.clear(), this.initSubscription.clear(), this.postInitHooks = [], this.initialized = !1, this.initializing = !1, this.appReady = !1, this.layout = { ...Q };
1052
1090
  }
1053
1091
  }
1054
1092
  let I = null;
1055
- function J() {
1056
- return I || (I = new ze()), I;
1093
+ function ie() {
1094
+ return I || (I = new Ge()), I;
1057
1095
  }
1058
- function Ve() {
1096
+ function Be() {
1059
1097
  I && (I.destroy(), I = null);
1060
1098
  }
1061
- const B = J(), Ue = D;
1062
- typeof window < "u" && (window.salla = window.salla || window.Salla || {}, window.Salla = window.salla, window.salla.embedded || (window.salla.embedded = B), window.Salla.embedded || (window.Salla.embedded = B));
1099
+ const J = ie(), Xe = M;
1100
+ typeof window < "u" && (window.salla = window.salla || window.Salla || {}, window.Salla = window.salla, window.salla.embedded || (window.salla.embedded = J), window.Salla.embedded || (window.Salla.embedded = J));
1063
1101
  export {
1064
- ze as EmbeddedApp,
1065
- B as embedded,
1066
- J as getEmbeddedApp,
1067
- Ve as resetEmbeddedApp,
1068
- Ue as version
1102
+ Ge as EmbeddedApp,
1103
+ He as INVALID_CHECKOUT_INPUT,
1104
+ J as embedded,
1105
+ ie as getEmbeddedApp,
1106
+ Be as resetEmbeddedApp,
1107
+ Xe as version
1069
1108
  };
1070
1109
  //# sourceMappingURL=index.js.map