ladrillosjs 1.0.2 → 2.0.0-beta.1

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.
Files changed (37) hide show
  1. package/README.md +533 -105
  2. package/dist/cache/functionCache.d.ts +15 -0
  3. package/dist/cache/index.d.ts +16 -0
  4. package/dist/core/componentParser.d.ts +29 -0
  5. package/dist/core/componentSource.d.ts +12 -0
  6. package/dist/core/css/cssParser.d.ts +3 -0
  7. package/dist/core/eventBus.d.ts +41 -0
  8. package/dist/core/html/htmlRenderer.d.ts +18 -0
  9. package/dist/core/html/htmlparser.d.ts +16 -0
  10. package/dist/core/js/scriptParser.d.ts +3 -0
  11. package/dist/core/main.d.ts +4 -26
  12. package/dist/core/webcomponent.d.ts +2 -2
  13. package/dist/index-CXHidyhO.js +8 -0
  14. package/dist/index-CXHidyhO.js.map +1 -0
  15. package/dist/index-VkDZJVOR.mjs +361 -0
  16. package/dist/index-VkDZJVOR.mjs.map +1 -0
  17. package/dist/index.d.ts +56 -6
  18. package/dist/ladrillosjs.cjs.js +2 -1
  19. package/dist/ladrillosjs.cjs.js.map +1 -0
  20. package/dist/ladrillosjs.es.js +12 -4
  21. package/dist/ladrillosjs.es.js.map +1 -0
  22. package/dist/ladrillosjs.umd.js +76 -18
  23. package/dist/ladrillosjs.umd.js.map +1 -0
  24. package/dist/types/LadrilloTypes.d.ts +47 -14
  25. package/dist/utils/logger.d.ts +14 -11
  26. package/dist/utils/regex.d.ts +2 -0
  27. package/dist/webcomponent-CJ3lZBZb.mjs +703 -0
  28. package/dist/webcomponent-CJ3lZBZb.mjs.map +1 -0
  29. package/dist/webcomponent-i9W7LUiv.js +70 -0
  30. package/dist/webcomponent-i9W7LUiv.js.map +1 -0
  31. package/package.json +8 -4
  32. package/dist/core/store.d.ts +0 -6
  33. package/dist/index-D_dHFObN.mjs +0 -215
  34. package/dist/index-X9YN_DbT.js +0 -3
  35. package/dist/utils/stringify.d.ts +0 -7
  36. package/dist/webcomponent-Cga3h8cx.js +0 -16
  37. package/dist/webcomponent-UTcwAakf.mjs +0 -769
@@ -0,0 +1,703 @@
1
+ var j = (t) => {
2
+ throw TypeError(t);
3
+ };
4
+ var k = (t, s, n) => s.has(t) || j("Cannot " + n);
5
+ var E = (t, s, n) => (k(t, s, "read from private field"), n ? n.call(t) : s.get(t)), A = (t, s, n) => s.has(t) ? j("Cannot add the same private member more than once") : s instanceof WeakSet ? s.add(t) : s.set(t, n), R = (t, s, n, e) => (k(t, s, "write to private field"), e ? e.call(t, n) : s.set(t, n), n), F = (t, s, n) => (k(t, s, "access private method"), n);
6
+ var N = (t, s, n) => new Promise((e, r) => {
7
+ var o = (l) => {
8
+ try {
9
+ c(n.next(l));
10
+ } catch (d) {
11
+ r(d);
12
+ }
13
+ }, i = (l) => {
14
+ try {
15
+ c(n.throw(l));
16
+ } catch (d) {
17
+ r(d);
18
+ }
19
+ }, c = (l) => l.done ? e(l.value) : Promise.resolve(l.value).then(o, i);
20
+ c((n = n.apply(t, s)).next());
21
+ });
22
+ import { R as H, e as K, l as Q } from "./index-VkDZJVOR.mjs";
23
+ const Y = (t, s, n) => {
24
+ if (!s) return;
25
+ const e = document.createElement("style");
26
+ e.textContent = s, n ? t.appendChild(e) : document.head.appendChild(e);
27
+ }, tt = (t, s) => {
28
+ t.innerHTML = s;
29
+ const n = et(t), e = nt(t), r = st(t);
30
+ return { bindings: n, twoWayBindings: e, conditionals: r };
31
+ }, et = (t) => {
32
+ const s = document.createTreeWalker(t, NodeFilter.SHOW_TEXT, null), n = [];
33
+ let e;
34
+ for (; e = s.nextNode(); ) {
35
+ const o = [...e.textContent.matchAll(H.bindings)];
36
+ if (o.length > 0) {
37
+ const i = e.textContent, c = o.map((l) => {
38
+ const d = l[1].trim(), u = d.includes("("), b = u ? [d.split("(")[0].trim()] : d.split(".").map((f) => f.trim());
39
+ return { raw: d, path: b, isFunction: u };
40
+ });
41
+ n.push({ node: e, bindings: c, original: i });
42
+ }
43
+ }
44
+ return t.querySelectorAll("*").forEach((o) => {
45
+ for (const i of o.attributes) {
46
+ if (i.name === "$if" || i.name === "$else-if" || i.name === "$else" || i.name === "$bind")
47
+ continue;
48
+ const c = [...i.value.matchAll(H.bindings)];
49
+ if (c.length > 0) {
50
+ const l = i.value, d = c.map((u) => {
51
+ const b = u[1].trim(), f = b.includes("("), w = f ? [b.split("(")[0].trim()] : b.split(".").map((v) => v.trim());
52
+ return { raw: b, path: w, isFunction: f };
53
+ });
54
+ n.push({
55
+ node: o,
56
+ bindings: d,
57
+ original: l,
58
+ isAttribute: !0,
59
+ attributeName: i.name
60
+ });
61
+ }
62
+ }
63
+ }), n;
64
+ }, nt = (t) => {
65
+ const s = [];
66
+ return t.querySelectorAll("[\\$bind]").forEach((e) => {
67
+ const r = e.getAttribute("$bind");
68
+ if (!r) return;
69
+ const o = r.trim(), i = o.split(".").map((c) => c.trim());
70
+ e instanceof HTMLInputElement || e instanceof HTMLTextAreaElement || e instanceof HTMLSelectElement ? (s.push({
71
+ element: e,
72
+ path: i,
73
+ raw: o,
74
+ isContentEditable: !1
75
+ }), e.removeAttribute("$bind")) : e instanceof HTMLElement && (e.hasAttribute("contenteditable") || e.isContentEditable) && (s.push({
76
+ element: e,
77
+ path: i,
78
+ raw: o,
79
+ isContentEditable: !0
80
+ }), e.removeAttribute("$bind"));
81
+ }), s;
82
+ }, st = (t) => {
83
+ const s = [], n = /* @__PURE__ */ new Set();
84
+ return t.querySelectorAll("[\\$if]").forEach((r) => {
85
+ if (n.has(r)) return;
86
+ const o = [];
87
+ let i = r;
88
+ for (; i; ) {
89
+ const c = i.hasAttribute("$if"), l = i.hasAttribute("$else-if"), d = i.hasAttribute("$else");
90
+ if (!c && !l && !d) break;
91
+ n.add(i);
92
+ let u, b = "";
93
+ c ? (u = "if", b = i.getAttribute("$if") || "", i.removeAttribute("$if")) : l ? (u = "else-if", b = i.getAttribute("$else-if") || "", i.removeAttribute("$else-if")) : (u = "else", i.removeAttribute("$else"));
94
+ const f = document.createComment(
95
+ `conditional:${u}:${b}`
96
+ ), w = i.parentElement || t, v = i.nextSibling;
97
+ w.insertBefore(f, i);
98
+ const $ = {
99
+ element: i,
100
+ condition: b.trim(),
101
+ type: u,
102
+ placeholder: f,
103
+ group: [],
104
+ // Will be set after the group is complete
105
+ originalParent: w,
106
+ nextSibling: v
107
+ };
108
+ o.push($);
109
+ const m = i.nextElementSibling;
110
+ if (i.remove(), i = m, m && !m.hasAttribute("$else-if") && !m.hasAttribute("$else"))
111
+ break;
112
+ }
113
+ o.forEach((c) => {
114
+ c.group = o;
115
+ }), s.push(o);
116
+ }), s;
117
+ }, ot = (t) => {
118
+ const s = /* @__PURE__ */ new Set();
119
+ return t.forEach((n) => {
120
+ n.forEach((e) => {
121
+ let r = e.condition;
122
+ r = r.replace(/\{([^}]+)\}/g, "$1");
123
+ const o = /\b([a-zA-Z_$][a-zA-Z0-9_$]*(?:\.[a-zA-Z_$][a-zA-Z0-9_$]*)*)\b/g, i = /* @__PURE__ */ new Set([
124
+ "true",
125
+ "false",
126
+ "null",
127
+ "undefined",
128
+ "typeof",
129
+ "instanceof",
130
+ "new",
131
+ "return",
132
+ "if",
133
+ "else",
134
+ "for",
135
+ "while",
136
+ "do",
137
+ "switch",
138
+ "case",
139
+ "break",
140
+ "continue"
141
+ ]);
142
+ let c;
143
+ for (; (c = o.exec(r)) !== null; ) {
144
+ const d = c[1].split(".")[0];
145
+ i.has(d) || s.add(d);
146
+ }
147
+ });
148
+ }), s;
149
+ }, _ = /* @__PURE__ */ new Map(), it = 100, rt = (t) => {
150
+ const s = _.get(t);
151
+ if (s)
152
+ return _.delete(t), _.set(t, s), s;
153
+ const n = new Function(
154
+ "component",
155
+ `with(component) { return ${t}; }`
156
+ );
157
+ if (_.size >= it) {
158
+ const e = _.keys().next().value;
159
+ e && _.delete(e);
160
+ }
161
+ return _.set(t, n), n;
162
+ }, ct = (t, s) => s.reduce((n, e) => {
163
+ if (!(n == null || typeof n != "object"))
164
+ return n[e];
165
+ }, t), O = (t, s, n) => {
166
+ if (s.length === 0) return;
167
+ const e = s[s.length - 1], r = s.slice(0, -1);
168
+ let o = t;
169
+ for (const i of r)
170
+ (o[i] === void 0 || o[i] === null) && (o[i] = {}), o = o[i];
171
+ o[e] = n;
172
+ }, q = (t, s, n) => {
173
+ for (const e of t) {
174
+ let r = e.original;
175
+ for (const { raw: o, path: i, isFunction: c } of e.bindings) {
176
+ let l;
177
+ if (c)
178
+ try {
179
+ const u = i[0];
180
+ typeof (n == null ? void 0 : n[u]) == "function" ? l = rt(o)(n) : l = void 0;
181
+ } catch (u) {
182
+ console.error(`Error executing function binding {${o}}:`, u), l = void 0;
183
+ }
184
+ else
185
+ l = ct(s, i);
186
+ if (l === void 0) continue;
187
+ const d = String(l != null ? l : "");
188
+ r = r.replace(`{${o}}`, d);
189
+ }
190
+ if (e.node.nodeType === Node.TEXT_NODE) {
191
+ const o = e.node, i = o.parentElement, c = r.trim().startsWith("<") && r.includes(">");
192
+ i && c ? i.innerHTML = r : o.textContent = r;
193
+ } else {
194
+ const o = e.node;
195
+ e.isAttribute && e.attributeName && o.setAttribute(e.attributeName, r);
196
+ }
197
+ }
198
+ }, at = (t, s, n) => {
199
+ if (!t) return !0;
200
+ let e = t.trim();
201
+ e = e.replace(/\{([^}]+)\}/g, "$1");
202
+ try {
203
+ return new Function(
204
+ "context",
205
+ "component",
206
+ `
207
+ with (context) {
208
+ try {
209
+ return Boolean(${e});
210
+ } catch (e) {
211
+ return false;
212
+ }
213
+ }
214
+ `
215
+ )(s, n);
216
+ } catch (r) {
217
+ return console.error(`Error evaluating condition "${t}":`, r), !1;
218
+ }
219
+ }, I = (t, s, n) => {
220
+ var e;
221
+ for (const r of t) {
222
+ let o = !1;
223
+ for (const i of r) {
224
+ const { element: c, condition: l, type: d, placeholder: u, originalParent: b } = i;
225
+ let f = !1;
226
+ d === "else" ? f = !o : o || (f = at(l, s, n), f && (o = !0));
227
+ const w = c.parentNode !== null;
228
+ f && !w ? (e = u.parentNode) == null || e.insertBefore(c, u.nextSibling) : !f && w && c.remove();
229
+ }
230
+ }
231
+ }, M = (t) => t instanceof ShadowRoot ? t.host : t, lt = (t, s) => {
232
+ const n = /* @__PURE__ */ new Set(), e = [];
233
+ s.forEach(({ path: o }) => {
234
+ const i = o[0];
235
+ n.add(i);
236
+ }), n.forEach((o) => {
237
+ new RegExp(
238
+ `(?:const|let|var)\\s+${o}\\s*=`,
239
+ "g"
240
+ ).test(t) && e.push(
241
+ `⚠️ Variable "${o}" is already bound via $bind and cannot be redeclared. Remove the declaration or use a different variable name.`
242
+ );
243
+ });
244
+ const r = [];
245
+ return n.forEach((o) => {
246
+ r.push(`
247
+ if (!Object.getOwnPropertyDescriptor(component, '${o}')) {
248
+ Object.defineProperty(component, '${o}', {
249
+ get() { return this.state.${o}; },
250
+ set(val) { this.state.${o} = val; },
251
+ enumerable: true,
252
+ configurable: true
253
+ });
254
+ }
255
+ `);
256
+ }), {
257
+ injectedCode: "",
258
+ // No longer needed in script scope
259
+ componentInjections: r.join(`
260
+ `),
261
+ bindVarNames: n,
262
+ errors: e
263
+ };
264
+ }, ut = (t, s, n = /* @__PURE__ */ new Set()) => {
265
+ const e = [], r = /* @__PURE__ */ new Set();
266
+ s.forEach((c) => {
267
+ c.bindings.forEach((l) => {
268
+ const d = l.path[0];
269
+ r.add(d);
270
+ });
271
+ }), n.forEach((c) => {
272
+ r.add(c);
273
+ });
274
+ const o = /(?:const|let|var)\s+(\w+)\s*=\s*([^;]+);?/g;
275
+ let i;
276
+ for (; (i = o.exec(t)) !== null; ) {
277
+ const c = i[1];
278
+ r.has(c) && e.push(`component.state.${c} = ${c};`);
279
+ }
280
+ return { stateBindings: e, boundVarNames: r };
281
+ }, dt = (t, s) => {
282
+ if (s.size === 0) return t;
283
+ let n = t;
284
+ return s.forEach((e) => {
285
+ const r = new RegExp(
286
+ `(?<!const\\s|let\\s|var\\s|\\.)\\b(\\+\\+|\\-\\-)${e}\\b|\\b${e}(\\+\\+|\\-\\-)`,
287
+ "g"
288
+ );
289
+ n = n.replace(r, (c) => {
290
+ if (c.startsWith("++") || c.startsWith("--"))
291
+ return `${c.substring(0, 2)}component.state.${e}`;
292
+ {
293
+ const l = c.substring(c.length - 2);
294
+ return `component.state.${e}${l}`;
295
+ }
296
+ });
297
+ const o = new RegExp(
298
+ `(?<!const\\s|let\\s|var\\s|\\.)\\b${e}\\s*(\\+=|\\-=|\\*=|\\/=|%=|\\*\\*=|<<=|>>=|>>>=|&=|\\|=|\\^=)`,
299
+ "g"
300
+ );
301
+ n = n.replace(
302
+ o,
303
+ `component.state.${e}$1`
304
+ );
305
+ const i = new RegExp(
306
+ `(?<!const\\s|let\\s|var\\s|\\.|\\+|\\-|\\*|\\/|%|<|>|&|\\||\\^)\\b${e}\\s*=\\s*([^=])`,
307
+ "g"
308
+ );
309
+ n = n.replace(
310
+ i,
311
+ `component.state.${e} = $1`
312
+ );
313
+ }), n;
314
+ }, P = (t, s, n, e, r, o = /* @__PURE__ */ new Set()) => {
315
+ try {
316
+ const { injectedCode: i, componentInjections: c, bindVarNames: l, errors: d } = lt(t, n);
317
+ d.length > 0 && (console.error("❌ $bind Variable Conflicts Detected:"), d.forEach((a) => console.error(a)));
318
+ const u = /function\s+(\w+)|const\s+(\w+)\s*=\s*(?:\([^)]*\)|[\w\s]*)\s*=>/g, b = [];
319
+ let f;
320
+ for (; (f = u.exec(t)) !== null; ) {
321
+ const a = f[1] || f[2];
322
+ a && b.push(a);
323
+ }
324
+ const w = b.map(
325
+ (a) => `component.${a} = typeof ${a} !== 'undefined' ? ${a}.bind(component) : undefined;`
326
+ ).join(`
327
+ `), { stateBindings: v, boundVarNames: $ } = ut(
328
+ t,
329
+ s,
330
+ o
331
+ ), m = /* @__PURE__ */ new Set([...$, ...l]), X = v.join(`
332
+ `), C = dt(
333
+ t,
334
+ m
335
+ ), Z = `
336
+ (function() {
337
+ // Create component scope with direct access to state
338
+ const component = this;
339
+ const $state = component.state;
340
+
341
+ // Define $bind variables on component object for event handler access
342
+ ${c}
343
+
344
+ // Provide framework utilities with $ prefix to avoid naming conflicts
345
+ const $setState = (updates) => component.setState(updates);
346
+
347
+ // Event bus methods for component communication
348
+ const $emit = (eventName, data) => arguments[2].emit(eventName, data);
349
+ const $listen = (eventName, callback) => {
350
+ const unsubscribe = arguments[2].listen(eventName, callback);
351
+ // Store unsubscribe function for cleanup on disconnect
352
+ if (!component.__eventUnsubscribers) {
353
+ component.__eventUnsubscribers = [];
354
+ }
355
+ component.__eventUnsubscribers.push(unsubscribe);
356
+ return unsubscribe;
357
+ };
358
+
359
+ // Also attach to component for event handler access
360
+ component.$emit = $emit;
361
+ component.$listen = $listen;
362
+
363
+ // Override querySelector/querySelectorAll to query within the component's host
364
+ const host = arguments[1];
365
+ const $querySelector = (selector) => host.querySelector(selector);
366
+ const $querySelectorAll = (selector) => host.querySelectorAll(selector);
367
+
368
+ // Execute script content within component scope so $bind variables are accessible
369
+ with(component) {
370
+ ${C}
371
+
372
+ // Auto-bind variables to component state (e.g., const name = "value" → this.state.name = "value")
373
+ ${X}
374
+
375
+ // Auto-attach all detected functions to component for onclick access
376
+ ${w}
377
+ }
378
+ }).call(arguments[0], arguments[0], arguments[1], arguments[2])
379
+ `;
380
+ new Function(Z)(r, e, K);
381
+ } catch (i) {
382
+ console.error("Script execution failed:", i);
383
+ }
384
+ }, U = (t, s) => {
385
+ [
386
+ "click",
387
+ "dblclick",
388
+ "mousedown",
389
+ "mouseup",
390
+ "mouseover",
391
+ "mouseout",
392
+ "mousemove",
393
+ "mouseenter",
394
+ "mouseleave",
395
+ "keydown",
396
+ "keyup",
397
+ "keypress",
398
+ "focus",
399
+ "blur",
400
+ "change",
401
+ "input",
402
+ "submit",
403
+ "reset",
404
+ "scroll",
405
+ "resize",
406
+ "load",
407
+ "unload",
408
+ "touchstart",
409
+ "touchend",
410
+ "touchmove",
411
+ "touchcancel",
412
+ "dragstart",
413
+ "drag",
414
+ "dragend",
415
+ "dragenter",
416
+ "dragover",
417
+ "dragleave",
418
+ "drop"
419
+ ].forEach((e) => {
420
+ const r = `on${e}`;
421
+ (t instanceof ShadowRoot ? t.querySelectorAll(`[${r}]`) : s.querySelectorAll(`[${r}]`)).forEach((i) => {
422
+ const c = `${r}`;
423
+ if (i[`__processed_${c}`]) return;
424
+ const l = i.getAttribute(r);
425
+ l && (i.removeAttribute(r), i.addEventListener(
426
+ e,
427
+ function(d) {
428
+ new Function(
429
+ "event",
430
+ "component",
431
+ `
432
+ with(component) {
433
+ ${l}
434
+ }
435
+ `
436
+ ).call(this, d, s);
437
+ }
438
+ ), i[`__processed_${c}`] = !0);
439
+ });
440
+ });
441
+ }, ft = (o, i, c, ...l) => N(null, [o, i, c, ...l], function* (t, s, n, e = [], r = /* @__PURE__ */ new Set()) {
442
+ if (!(s != null && s.length)) return;
443
+ const d = M(t);
444
+ for (const u of s)
445
+ u.content && P(
446
+ u.content,
447
+ n,
448
+ e,
449
+ t,
450
+ d,
451
+ r
452
+ );
453
+ U(t, d);
454
+ }), ht = (t, s) => new Promise((n, e) => {
455
+ const r = document.querySelector(
456
+ `script[src="${t}"]`
457
+ );
458
+ if (r) {
459
+ if (r.dataset.loaded === "true") {
460
+ n();
461
+ return;
462
+ }
463
+ r.addEventListener("load", () => n(), { once: !0 }), r.addEventListener(
464
+ "error",
465
+ () => e(new Error(`Failed to load external script: ${t}`)),
466
+ { once: !0 }
467
+ );
468
+ return;
469
+ }
470
+ const o = document.createElement("script");
471
+ o.src = t, s && (o.type = s), o.onload = () => {
472
+ o.dataset.loaded = "true", n();
473
+ }, o.onerror = () => e(new Error(`Failed to load external script: ${t}`)), document.head.appendChild(o);
474
+ }), bt = (i, c, l, ...d) => N(null, [i, c, l, ...d], function* (t, s, n, e = [], r = /* @__PURE__ */ new Set(), o) {
475
+ var b;
476
+ const u = M(t);
477
+ for (const f of s) {
478
+ let w;
479
+ o ? o.startsWith("http://") || o.startsWith("https://") ? w = o : w = new URL(
480
+ o,
481
+ window.location.href
482
+ ).href : w = window.location.href;
483
+ const v = new URL(f.src, w).href;
484
+ if (f.external)
485
+ yield ht(v, f.type);
486
+ else if (f.type === "module") {
487
+ const $ = u.tagName.toLowerCase();
488
+ window.__ladrilloContexts || (window.__ladrilloContexts = /* @__PURE__ */ new Map()), window.__ladrilloContexts.set($, {
489
+ host: t,
490
+ shadowRoot: t instanceof ShadowRoot ? t : null,
491
+ element: u,
492
+ state: u.state,
493
+ setState: (b = u.setState) == null ? void 0 : b.bind(u)
494
+ });
495
+ const m = document.createElement("script");
496
+ m.type = "module", m.src = v, m.setAttribute("data-component", $), document.head.appendChild(m);
497
+ } else
498
+ yield fetch(v).then(($) => {
499
+ if (!$.ok)
500
+ throw new Error(`HTTP ${$.status}: ${$.statusText}`);
501
+ const m = $.headers.get("content-type");
502
+ if (m && !m.includes("javascript") && !m.includes("text/plain"))
503
+ throw new Error(`Expected JavaScript but got ${m}`);
504
+ return $.text();
505
+ }).then(($) => {
506
+ P(
507
+ $,
508
+ n,
509
+ e,
510
+ t,
511
+ u,
512
+ r
513
+ );
514
+ }).catch(($) => {
515
+ console.error(`Failed to load external script: ${f.src}`, $);
516
+ });
517
+ }
518
+ U(t, u);
519
+ }), gt = (t, s) => {
520
+ var d, u, b, f, w, v, z, m, D;
521
+ const { tagName: n, template: e, scripts: r, externalScripts: o, styles: i, sourcePath: c } = t, C = class C extends HTMLElement {
522
+ constructor() {
523
+ super();
524
+ A(this, m);
525
+ A(this, d, []);
526
+ A(this, u, []);
527
+ A(this, b, []);
528
+ A(this, f, []);
529
+ A(this, w, c);
530
+ s && this.attachShadow({ mode: "open" });
531
+ const a = {};
532
+ this.state = new Proxy(a, {
533
+ set: (h, p, g) => {
534
+ const y = h[p];
535
+ return Object.is(y, g) || (h[p] = g, Object.getOwnPropertyDescriptor(this, p) || Object.defineProperty(this, p, {
536
+ get() {
537
+ return this.state[p];
538
+ },
539
+ set(L) {
540
+ this.state[p] = L;
541
+ },
542
+ enumerable: !0,
543
+ configurable: !0
544
+ }), q(E(this, d), this.state, this), I(E(this, b), this.state, this), F(this, m, D).call(this)), !0;
545
+ }
546
+ });
547
+ }
548
+ /**
549
+ * Updates component state with one or more key-value pairs
550
+ * @param updates - Object containing state updates
551
+ * @example
552
+ * component.setState({ count: 5, name: 'John' })
553
+ */
554
+ setState(a) {
555
+ Object.assign(this.state, a);
556
+ }
557
+ // Invoked when element is added to the DOM
558
+ connectedCallback() {
559
+ return N(this, null, function* () {
560
+ const a = s ? this.shadowRoot : this, { bindings: h, twoWayBindings: p, conditionals: g } = tt(
561
+ a,
562
+ e
563
+ );
564
+ R(this, d, h), R(this, u, p), R(this, b, g);
565
+ const y = ot(g);
566
+ Y(a, i, s), this._initializeStateFromAttributes(), this._setupTwoWayBindings(), yield bt(
567
+ a,
568
+ o,
569
+ E(this, d),
570
+ E(this, u),
571
+ y,
572
+ E(this, w)
573
+ ), yield ft(
574
+ a,
575
+ r,
576
+ E(this, d),
577
+ E(this, u),
578
+ y
579
+ ), q(E(this, d), this.state, this), I(E(this, b), this.state, this), this._setupAttributeObserver();
580
+ });
581
+ }
582
+ // Invoked when element is removed from the DOM
583
+ disconnectedCallback() {
584
+ this.__attributeObserver && (this.__attributeObserver.disconnect(), this.__attributeObserver = null), E(this, f).forEach((h) => {
585
+ try {
586
+ h();
587
+ } catch (p) {
588
+ console.error("Error cleaning up two-way binding:", p);
589
+ }
590
+ }), R(this, f, []);
591
+ const a = this.__eventUnsubscribers;
592
+ a && Array.isArray(a) && (a.forEach((h) => {
593
+ try {
594
+ h();
595
+ } catch (p) {
596
+ console.error("Error unsubscribing from event:", p);
597
+ }
598
+ }), this.__eventUnsubscribers = []);
599
+ }
600
+ // Set up observer to watch for attribute changes
601
+ _setupAttributeObserver() {
602
+ const a = new MutationObserver((h) => {
603
+ h.forEach((p) => {
604
+ if (p.type === "attributes" && p.attributeName) {
605
+ const g = this.getAttribute(p.attributeName);
606
+ this._handleAttributeChange(p.attributeName, g);
607
+ }
608
+ });
609
+ });
610
+ a.observe(this, {
611
+ attributes: !0,
612
+ attributeOldValue: !0
613
+ }), this.__attributeObserver = a;
614
+ }
615
+ // initializes the state from the attributes
616
+ _initializeStateFromAttributes() {
617
+ this.getAttributeNames().forEach((a) => {
618
+ const h = this.getAttribute(a);
619
+ this._handleAttributeChange(a, h);
620
+ });
621
+ }
622
+ // Invoked when attributes are changed.
623
+ _handleAttributeChange(a, h) {
624
+ var g;
625
+ if (a) {
626
+ const y = "this.state.";
627
+ a.startsWith(y) && (a = a.slice(y.length));
628
+ }
629
+ const p = F(g = C, v, z).call(g, h);
630
+ this.state[a] = p;
631
+ }
632
+ // Setup two-way data bindings for input elements with $bind
633
+ _setupTwoWayBindings() {
634
+ E(this, u).forEach(
635
+ ({ element: a, path: h, raw: p, isContentEditable: g }) => {
636
+ var L, W;
637
+ if (this._getNestedValue(h) === void 0 && O(this.state, h, ""), g) {
638
+ const x = a;
639
+ x.textContent = (L = this._getNestedValue(h)) != null ? L : "";
640
+ const S = (T) => {
641
+ const B = T.target.textContent || "";
642
+ O(this.state, h, B);
643
+ };
644
+ x.addEventListener("input", S);
645
+ const V = () => {
646
+ x.removeEventListener("input", S);
647
+ };
648
+ E(this, f).push(V);
649
+ } else {
650
+ const x = a;
651
+ x.value = (W = this._getNestedValue(h)) != null ? W : "";
652
+ const S = (T) => {
653
+ const B = T.target.value;
654
+ O(this.state, h, B);
655
+ };
656
+ x.addEventListener("input", S);
657
+ const V = () => {
658
+ x.removeEventListener("input", S);
659
+ };
660
+ if (E(this, f).push(V), x instanceof HTMLSelectElement || x instanceof HTMLInputElement && ["checkbox", "radio", "file"].includes(x.type)) {
661
+ x.addEventListener("change", S);
662
+ const T = () => {
663
+ x.removeEventListener("change", S);
664
+ };
665
+ E(this, f).push(T);
666
+ }
667
+ }
668
+ }
669
+ );
670
+ }
671
+ // Helper to get nested value from state
672
+ _getNestedValue(a) {
673
+ return a.reduce((h, p) => h == null ? void 0 : h[p], this.state);
674
+ }
675
+ };
676
+ d = new WeakMap(), u = new WeakMap(), b = new WeakMap(), f = new WeakMap(), w = new WeakMap(), v = new WeakSet(), z = function(a) {
677
+ if (a === null || a === "") return null;
678
+ if (a !== "undefined")
679
+ try {
680
+ return JSON.parse(a);
681
+ } catch (h) {
682
+ return a;
683
+ }
684
+ }, m = new WeakSet(), // Update two-way bound elements when state changes
685
+ D = function() {
686
+ E(this, u).forEach(({ element: a, path: h, isContentEditable: p }) => {
687
+ const g = this._getNestedValue(h);
688
+ if (p) {
689
+ const y = a;
690
+ y.textContent !== g && (y.textContent = g != null ? g : "");
691
+ } else {
692
+ const y = a;
693
+ y.value !== g && (y.value = g != null ? g : "");
694
+ }
695
+ });
696
+ }, A(C, v);
697
+ let l = C;
698
+ customElements.define(n, l), Q.log(`Web component defined: <${n}></${n}>`);
699
+ };
700
+ export {
701
+ gt as defineWebComponent
702
+ };
703
+ //# sourceMappingURL=webcomponent-CJ3lZBZb.mjs.map