All files / devtools/src dev-tools.js

37.95% Statements 137/361
35.43% Branches 90/254
35.61% Functions 26/73
38.64% Lines 131/339

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001                            1x                                       12x 12x               12x 12x 12x 12x 12x 12x 12x   12x 10x                     4x 1x       3x 3x                   10x   10x 10x   10x 10x     10x                     12x 23x 23x                     72x 72x 72x 61x   72x               25x 25x 1x   24x 1x     23x 23x   23x   23x     23x           23x 23x   23x                                                               10x                                                     10x 80x 10x 10x 10x 80x 16x                   23x 23x                     23x 23x 20x                 23x               23x                 23x     23x         23x 2x                 23x 23x               24043x               24043x 24043x                 24043x 20x 24000x         24000x       24023x 24023x 24023x 20x         20x             24043x             48086x   48086x       48086x 40x 48000x     48046x 48046x 48046x 48046x 48046x 40x   48046x         48046x                     23x 23x                             23x                                                                                                                                                                                                                                                                                     10x 10x 10x   50x   50x           50x   10x 10x 10x           10x                                     10x 2x                   2x 2x                                                     10x 1x                 1x 1x   1x                               1x         1x         1x                                                                                                                                                                                                                                                                                                                                                                                                                                 23x       23x   23x 23x       23x           23x 23x 23x   23x 23x   23x     23x       23x       23x                                                                                                   12x           1x                                                                                                  
/**
 * Development Tools for Coherent.js
 * Provides debugging, profiling, and development utilities
 * Only active in development environment for zero production overhead
 *
 * DevTools never mutates the Coherent instance it is given (ESM namespace
 * objects are frozen, so patching `render` threw): it wraps it instead โ€”
 * render through `devtools.render()` / `devtools.createComponent()`.
 * It installs no process-level handlers unless asked to, and never keeps
 * the process alive or exits it.
 */
 
import { performanceMonitor, validateComponent, isCoherentObject } from '@coherent.js/core';
 
const LOCAL_HOSTNAMES = new Set(['localhost', '127.0.0.1', '[::1]', '::1']);
 
/**
 * Main DevTools class
 */
export class DevTools {
    /**
     * @param {Object} [coherentInstance] - Object exposing `render` (and optionally
     *   `createComponent` / `cache`), e.g. `import * as coherent from '@coherent.js/core'`.
     * @param {Object} [options]
     * @param {boolean} [options.enabled] - Force on or off; defaults to {@link DevTools#shouldEnable}.
     * @param {number} [options.maxEntries=100] - Cap on retained warnings and errors each.
     * @param {boolean} [options.captureConsoleErrors=true] - Record console.error calls.
     * @param {boolean} [options.trackUnhandledRejections=false] - Record unhandled promise
     *   rejections (Node). They still crash the process as they would without DevTools.
     * @param {string} [options.hotReloadUrl] - Browser only: WebSocket URL of a dev server
     *   sending `component-updated` / `full-reload` messages. No connection is made without it.
     * @param {boolean} [options.globalHelpers=true] - Expose `$inspect`, `$history`, โ€ฆ globally.
     */
    constructor(coherentInstance = null, options = {}) {
        this.coherent = coherentInstance;
        this.options = {
            maxEntries: 100,
            captureConsoleErrors: true,
            trackUnhandledRejections: false,
            hotReloadUrl: null,
            globalHelpers: true,
            ...options
        };
        this.isEnabled = typeof options.enabled === 'boolean' ? options.enabled : this.shouldEnable();
        this.renderHistory = [];
        this.componentRegistry = new Map();
        this.warnings = [];
        this.errors = [];
        this.hotReloadEnabled = false;
        this._teardown = [];
 
        if (this.isEnabled) {
            this.initialize();
        }
    }
 
    /**
     * Check if dev tools should be enabled: NODE_ENV=development in Node, a
     * localhost page in the browser. Anything else (a query parameter on a
     * production host, say) needs the explicit `enabled: true` option.
     */
    shouldEnable() {
        // Only enable in development
        if (typeof process !== 'undefined' && process?.env) {
            return process.env.NODE_ENV === 'development';
        }
 
        // Browser development detection
        Eif (typeof window !== 'undefined' && window.location) {
            return LOCAL_HOSTNAMES.has(window.location.hostname);
        }
 
        return false;
    }
 
    /**
     * Initialize dev tools
     */
    initialize() {
        console.log('๐Ÿ› ๏ธ Coherent.js Dev Tools Enabled');
 
        Eif (this.options.globalHelpers) {
            this.setupGlobalHelpers();
        }
        this.setupErrorHandling();
        this.setupHotReload();
 
        // Browser-specific setup
        Iif (typeof window !== 'undefined' && typeof document !== 'undefined') {
            this.setupBrowserDevTools();
        }
    }
 
    /**
     * Undo everything initialize() installed: global helpers, the
     * console.error hook, the unhandled-rejection listener, the hot-reload
     * socket and the browser panel.
     */
    destroy() {
        while (this._teardown.length) {
            try {
                this._teardown.pop()();
            } catch {
                // keep tearing down the rest
            }
        }
    }
 
    /**
     * Append to a bounded list (warnings, errors), dropping the oldest.
     */
    record(list, entry) {
        list.push(entry);
        const max = Math.max(1, this.options.maxEntries);
        if (list.length > max) {
            list.splice(0, list.length - max);
        }
        return entry;
    }
 
    /**
     * Render through the wrapped Coherent instance, recording timing,
     * validation warnings and errors.
     */
    render(component, context = {}, options = {}) {
        const render = this.coherent?.render;
        if (typeof render !== 'function') {
            throw new TypeError('DevTools.render() needs a Coherent instance with a render() function: createDevTools(coherent)');
        }
        if (!this.isEnabled) {
            return render.call(this.coherent, component, context, options);
        }
 
        const renderStart = performance.now();
        const renderId = this.generateRenderId();
 
        try {
            // Pre-render validation and logging
            this.preRenderAnalysis(component, context, renderId);
 
            // Actual render
            const result = render.call(this.coherent, component, context, {
                ...options,
                _devRenderId: renderId
            });
 
            // Post-render analysis
            const renderTime = performance.now() - renderStart;
            this.postRenderAnalysis(component, result, renderTime, renderId);
 
            return result;
 
        } catch (_error) {
            this.handleRenderError(_error, component, context, renderId);
            throw _error;
        }
    }
 
    /**
     * createComponent() of the wrapped instance, registering the result for
     * inspection.
     */
    createComponent(config) {
        const create = this.coherent?.createComponent;
        if (typeof create !== 'function') {
            throw new TypeError('DevTools.createComponent() needs a Coherent instance with createComponent()');
        }
        const component = create.call(this.coherent, config);
        if (this.isEnabled) {
            this.componentRegistry.set(config?.name || 'anonymous', {
                config,
                component,
                registeredAt: Date.now()
            });
        }
        return component;
    }
 
    /**
     * Set up global helper functions
     */
    setupGlobalHelpers() {
        const helpers = {
            // Inspect any component
            $inspect: (component) => this.inspectComponent(component),
 
            // Get render history
            $history: () => this.renderHistory,
 
            // Get performance stats
            $perf: () => this.getPerformanceInsights(),
 
            // Validate component structure
            $validate: (component) => this.validateComponent(component),
 
            // Get component registry
            $registry: () => Array.from(this.componentRegistry.entries()),
 
            // Clear dev data
            $clear: () => this.clearDevData(),
 
            // Enable/disable features
            $toggle: (feature) => this.toggleFeature(feature),
 
            // Get warnings and errors
            $issues: () => ({ warnings: this.warnings, errors: this.errors })
        };
 
        // Expose helpers globally
        const target = globalThis;
        const previous = Object.fromEntries(Object.keys(helpers).map((key) => [key, target[key]]));
        Object.assign(target, helpers);
        this._teardown.push(() => {
            for (const [key, value] of Object.entries(previous)) {
                if (value === undefined) delete target[key];
                else target[key] = value;
            }
        });
    }
 
    /**
     * Pre-render analysis and validation
     */
    preRenderAnalysis(component, context, renderId) {
        // Component structure validation
        const validation = this.deepValidateComponent(component);
        Iif (!validation.isValid) {
            this.record(this.warnings, {
                type: 'validation',
                message: validation.message,
                component: this.serializeComponent(component),
                renderId,
                timestamp: Date.now()
            });
        }
 
        // Performance warnings
        const complexity = this.analyzeComplexity(component);
        if (complexity > 1000) {
            this.record(this.warnings, {
                type: 'performance',
                message: `High complexity component detected (${complexity} nodes)`,
                renderId,
                timestamp: Date.now()
            });
        }
 
        // Context analysis
        this.analyzeContext(context ?? {}, renderId);
    }
 
    /**
     * Post-render analysis
     */
    postRenderAnalysis(component, result, renderTime, renderId) {
        // Store render in history
        const renderRecord = {
            id: renderId,
            timestamp: Date.now(),
            component: this.serializeComponent(component),
            renderTime,
            outputSize: typeof result === 'string' ? result.length : 0,
            complexity: this.analyzeComplexity(component)
        };
 
        this.renderHistory.push(renderRecord);
 
        // Keep only last 50 renders
        Iif (this.renderHistory.length > 50) {
            this.renderHistory.shift();
        }
 
        // Performance analysis
        if (renderTime > 10) {
            this.record(this.warnings, {
                type: 'performance',
                message: `Slow render detected: ${renderTime.toFixed(2)}ms`,
                renderId,
                timestamp: Date.now()
            });
        }
 
        // Log render in development
        Eif (renderTime > 1) {
            console.log(`๐Ÿ”„ Render ${renderId}: ${renderTime.toFixed(2)}ms`);
        }
    }
 
    /**
     * Deep component validation
     */
    deepValidateComponent(component, path = 'root', depth = 0) {
        Iif (depth > 100) {
            return {
                isValid: false,
                message: `Component nesting too deep at ${path}`
            };
        }
 
        // Basic validation
        try {
            validateComponent(component);
        } catch (_error) {
            return {
                isValid: false,
                message: `Invalid component at ${path}: ${_error.message}`
            };
        }
 
        // Recursive validation for objects and arrays
        if (Array.isArray(component)) {
            for (let i = 0; i < component.length; i++) {
                const childValidation = this.deepValidateComponent(
                    component[i],
                    `${path}[${i}]`,
                    depth + 1
                );
                Iif (!childValidation.isValid) {
                    return childValidation;
                }
            }
        E} else if (isCoherentObject(component)) {
            for (const [tag, props] of Object.entries(component)) {
                if (props && typeof props === 'object' && props.children) {
                    const childValidation = this.deepValidateComponent(
                        props.children,
                        `${path}.${tag}.children`,
                        depth + 1
                    );
                    Iif (!childValidation.isValid) {
                        return childValidation;
                    }
                }
            }
        }
 
        return { isValid: true };
    }
 
    /**
     * Analyze component complexity
     */
    analyzeComplexity(component, depth = 0) {
        Iif (depth > 100) return 1000; // Prevent infinite recursion
 
        Iif (typeof component === 'string' || typeof component === 'number') {
            return 1;
        }
 
        if (Array.isArray(component)) {
            return component.reduce((sum, child) =>
                sum + this.analyzeComplexity(child, depth + 1), 0);
        }
 
        Eif (isCoherentObject(component)) {
            let complexity = 1;
            for (const [, props] of Object.entries(component)) {
                Eif (props && typeof props === 'object') {
                    if (props.children) {
                        complexity += this.analyzeComplexity(props.children, depth + 1);
                    }
                    Iif (typeof props.text === 'function') {
                        complexity += 2; // Functions add complexity
                    }
                }
            }
            return complexity;
        }
 
        return 1;
    }
 
    /**
     * Context analysis
     */
    analyzeContext(context, renderId) {
        let contextSize;
        try {
            contextSize = JSON.stringify(context)?.length ?? 0;
        } catch (_error) {
            // Circular reference check
            if (/circular/i.test(_error.message)) {
                this.record(this.warnings, {
                    type: 'context',
                    message: 'Circular reference detected in context',
                    renderId,
                    timestamp: Date.now()
                });
            }
            return;
        }
 
        // Large context warning
        Iif (contextSize > 10000) {
            this.record(this.warnings, {
                type: 'context',
                message: `Large context object: ${contextSize} characters`,
                renderId,
                timestamp: Date.now()
            });
        }
    }
 
    /**
     * Component inspector
     */
    inspectComponent(component) {
        return {
            type: this.getComponentType(component),
            complexity: this.analyzeComplexity(component),
            validation: this.deepValidateComponent(component),
            structure: this.visualizeStructure(component),
            serialized: this.serializeComponent(component),
            recommendations: this.getOptimizationRecommendations(component)
        };
    }
 
    /**
     * Get component type
     */
    getComponentType(component) {
        if (typeof component === 'string') return 'text';
        if (typeof component === 'function') return 'function';
        if (Array.isArray(component)) return 'array';
        if (isCoherentObject(component)) return 'element';
        return 'unknown';
    }
 
    /**
     * Visualize component structure
     */
    visualizeStructure(component, depth = 0, maxDepth = 5) {
        if (depth > maxDepth) return '...';
 
        const indent = '  '.repeat(depth);
 
        if (typeof component === 'string') {
            return `${indent}"${component.substring(0, 20)}${component.length > 20 ? '...' : ''}"`;
        }
 
        if (Array.isArray(component)) {
            const items = component.slice(0, 3).map(child =>
                this.visualizeStructure(child, depth + 1, maxDepth)
            );
            const more = component.length > 3 ? `${indent}  ...${component.length - 3} more` : '';
            return `${indent}[\n${items.join('\n')}${more ? `\n${  more}` : ''}\n${indent}]`;
        }
 
        if (isCoherentObject(component)) {
            const entries = Object.entries(component).slice(0, 3);
            const elements = entries.map(([tag, props]) => {
                let result = `${indent}<${tag}`;
                if (props && props.children) {
                    result += `>\n${this.visualizeStructure(props.children, depth + 1, maxDepth)}\n${indent}</${tag}>`;
                } else if (props && props.text) {
                    result += `>${props.text}</${tag}>`;
                } else {
                    result += ' />';
                }
                return result;
            });
            return elements.join('\n');
        }
 
        return `${indent}${typeof component}`;
    }
 
    /**
     * Get optimization recommendations
     */
    getOptimizationRecommendations(component) {
        const recommendations = [];
        const complexity = this.analyzeComplexity(component);
 
        if (complexity > 500) {
            recommendations.push({
                type: 'complexity',
                message: 'Consider breaking down this component into smaller parts',
                priority: 'high'
            });
        }
 
        // Check for repeated patterns
        const serialized = JSON.stringify(component);
        const patterns = this.findRepeatedPatterns(serialized);
        if (patterns.length > 0) {
            recommendations.push({
                type: 'caching',
                message: 'Consider extracting repeated patterns into cached components',
                priority: 'medium',
                patterns: patterns.slice(0, 3)
            });
        }
 
        return recommendations;
    }
 
    /**
     * Find repeated patterns in component
     */
    findRepeatedPatterns(serialized) {
        const patterns = [];
        const minPatternLength = 20;
 
        for (let i = 0; i < serialized.length - minPatternLength; i++) {
            for (let len = minPatternLength; len <= 100 && i + len < serialized.length; len++) {
                const pattern = serialized.substring(i, i + len);
                const occurrences = (serialized.match(new RegExp(this.escapeRegex(pattern), 'g')) || []).length;
 
                if (occurrences > 2) {
                    patterns.push({ pattern: `${pattern.substring(0, 50)  }...`, occurrences });
                    break; // Found a pattern starting at this position
                }
            }
        }
 
        return patterns.sort((a, b) => b.occurrences - a.occurrences);
    }
 
    /**
     * Escape regex special characters
     */
    escapeRegex(string) {
        return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
    }
 
    /**
     * Record errors. console.error calls are captured (and still printed);
     * unhandled rejections only when `trackUnhandledRejections` is set, and
     * then they still crash the process exactly as they would without us.
     */
    setupErrorHandling() {
        Eif (this.options.captureConsoleErrors) {
            const originalConsoleError = console.error;
            const hooked = (...args) => {
                // Log to dev tools
                this.record(this.errors, {
                    type: 'console',
                    message: args.map((arg) => String(arg)).join(' '),
                    timestamp: Date.now(),
                    stack: new Error().stack
                });
 
                // Call original
                originalConsoleError.apply(console, args);
            };
            console.error = hooked;
            this._teardown.push(() => {
                Eif (console.error === hooked) console.error = originalConsoleError;
            });
        }
 
        // Unhandled rejection handler (Node.js). Adding a listener turns off
        // Node's default crash, so rethrow unless someone else handles it.
        Iif (this.options.trackUnhandledRejections && typeof process !== 'undefined' && typeof process.on === 'function') {
            const listener = (reason) => {
                this.record(this.errors, {
                    type: 'unhandled-rejection',
                    message: reason instanceof Error ? reason.message : String(reason),
                    stack: reason instanceof Error ? reason.stack : undefined,
                    timestamp: Date.now()
                });
                if (process.listenerCount('unhandledRejection') === 1) {
                    throw reason instanceof Error
                        ? reason
                        : new Error(`Unhandled promise rejection: ${String(reason)}`);
                }
            };
            process.on('unhandledRejection', listener);
            this._teardown.push(() => process.off('unhandledRejection', listener));
        }
 
        // Browser error handler
        if (typeof window !== 'undefined' && typeof window.addEventListener === 'function') {
            const listener = (event) => {
                this.record(this.errors, {
                    type: 'browser-error',
                    message: event.message,
                    filename: event.filename,
                    lineno: event.lineno,
                    colno: event.colno,
                    timestamp: Date.now()
                });
            };
            window.addEventListener('error', listener);
            this._teardown.push(() => window.removeEventListener('error', listener));
        }
    }
 
    /**
     * Handle render errors specifically
     */
    handleRenderError(_error, component, context, renderId) {
        this.record(this.errors, {
            type: 'render-error',
            message: _error.message,
            stack: _error.stack,
            component: this.serializeComponent(component),
            context: Object.keys(context),
            renderId,
            timestamp: Date.now()
        });
 
        console.error(`๐Ÿšจ Render Error in ${renderId}:`, _error.message);
        console.error('Component:', this.serializeComponent(component));
    }
 
    /**
     * Connect to a hot-reload WebSocket, only when `hotReloadUrl` is set
     * (browser only).
     */
    setupHotReload() {
        if (this.options.hotReloadUrl && typeof window !== 'undefined' && 'WebSocket' in window) {
            this.setupBrowserHotReload(this.options.hotReloadUrl);
        }
    }
 
    /**
     * Browser hot reload setup
     */
    setupBrowserHotReload(url) {
        // Connect to development server WebSocket
        try {
            const ws = new window.WebSocket(url);
 
            ws.onmessage = (event) => {
                let data;
                try {
                    data = JSON.parse(event.data);
                } catch {
                    return;
                }
 
                if (data.type === 'component-updated') {
                    console.log('๐Ÿ”„ Component updated:', data.componentName);
                    this.handleComponentUpdate(data);
                } else if (data.type === 'full-reload') {
                    window.location.reload();
                }
            };
 
            ws.onopen = () => {
                console.log('๐Ÿ”— Connected to Coherent dev server');
                this.hotReloadEnabled = true;
            };
 
            ws.onclose = () => {
                console.log('๐Ÿ”Œ Disconnected from dev server');
                this.hotReloadEnabled = false;
            };
 
            this._teardown.push(() => ws.close());
        } catch {
            // Dev server not available
        }
    }
 
    /**
     * Handle component updates
     */
    handleComponentUpdate(updateData) {
        // Clear related caches
        if (this.coherent?.cache?.invalidatePattern) {
            this.coherent.cache.invalidatePattern(updateData.componentName);
        }
 
        // Update component registry
        this.componentRegistry.set(updateData.componentName, {
            ...updateData,
            lastUpdated: Date.now()
        });
 
        // Trigger re-render if needed
        if (typeof window !== 'undefined' && window.location.search.includes('auto-reload=true')) {
            window.location.reload();
        }
    }
 
    /**
     * Setup browser-specific dev tools
     */
    setupBrowserDevTools() {
        // Add dev tools panel to page
        this.createDevPanel();
 
        // Add keyboard shortcuts
        const onKeyDown = (e) => {
            // Ctrl+Shift+C = Toggle dev panel
            if (e.ctrlKey && e.shiftKey && e.code === 'KeyC') {
                this.toggleDevPanel();
                e.preventDefault();
            }
 
            // Ctrl+Shift+P = Performance report
            if (e.ctrlKey && e.shiftKey && e.code === 'KeyP') {
                console.table(this.getPerformanceInsights());
                e.preventDefault();
            }
        };
        document.addEventListener('keydown', onKeyDown);
        this._teardown.push(() => document.removeEventListener('keydown', onKeyDown));
    }
 
    /**
     * Create development panel in browser
     */
    createDevPanel() {
        // Create floating dev panel
        const panel = document.createElement('div');
        panel.id = 'coherent-dev-panel';
        panel.style.cssText = `
      position: fixed;
      top: 10px;
      right: 10px;
      width: 300px;
      background: #1a1a1a;
      color: #fff;
      font-family: monospace;
      font-size: 12px;
      border-radius: 8px;
      box-shadow: 0 4px 20px rgba(0,0,0,0.5);
      z-index: 999999;
      display: none;
      max-height: 80vh;
      overflow-y: auto;
    `;
 
        document.body.appendChild(panel);
        this.devPanel = panel;
        this._teardown.push(() => {
            panel.remove();
            this.devPanel = null;
        });
        this.updateDevPanel();
    }
 
    /**
     * Toggle dev panel visibility
     */
    toggleDevPanel() {
        if (this.devPanel) {
            const isVisible = this.devPanel.style.display === 'block';
            this.devPanel.style.display = isVisible ? 'none' : 'block';
            if (!isVisible) {
                this.updateDevPanel();
            }
        }
    }
 
    /**
     * Update dev panel content
     */
    updateDevPanel() {
        if (!this.devPanel) return;
 
        const stats = this.getPerformanceInsights();
        const recentRenders = this.renderHistory.slice(-5);
        const recentWarnings = this.warnings.slice(-3);
 
        this.devPanel.innerHTML = `
      <div style="padding: 15px; border-bottom: 1px solid #333;">
        <strong>๐Ÿ› ๏ธ Coherent.js Dev Tools</strong>
        <button onclick="this.parentElement.parentElement.style.display='none'"
                style="float: right; background: none; border: none; color: #fff; cursor: pointer;">ร—</button>
      </div>
 
      <div style="padding: 10px;">
        <h4 style="margin: 0 0 10px 0; color: #4CAF50;">Performance</h4>
        <div style="font-size: 11px;">
          <div>Avg Render: ${stats.averageRenderTime || 0}ms</div>
          <div>Cache Hit Rate: ${((stats.cacheHits || 0) / Math.max(stats.totalRenders || 1, 1) * 100).toFixed(1)}%</div>
          <div>Memory Usage: ${(performance.memory?.usedJSHeapSize / 1024 / 1024 || 0).toFixed(1)}MB</div>
        </div>
      </div>
 
      <div style="padding: 10px;">
        <h4 style="margin: 0 0 10px 0; color: #2196F3;">Recent Renders</h4>
        ${recentRenders.map(r => `
          <div style="font-size: 10px; margin-bottom: 5px; padding: 3px; background: #333; border-radius: 3px;">
            ${r.id}: ${r.renderTime.toFixed(1)}ms (${r.complexity} nodes)
          </div>
        `).join('')}
      </div>
 
      ${recentWarnings.length > 0 ? `
        <div style="padding: 10px;">
          <h4 style="margin: 0 0 10px 0; color: #FF9800;">Warnings</h4>
          ${recentWarnings.map(w => `
            <div style="font-size: 10px; margin-bottom: 5px; padding: 3px; background: #4a2c0a; border-radius: 3px; color: #FFB74D;">
              ${w.type}: ${w.message}
            </div>
          `).join('')}
        </div>
      ` : ''}
 
      <div style="padding: 10px; font-size: 10px; color: #888;">
        Press Ctrl+Shift+P for performance details
      </div>
    `;
    }
 
    /**
     * Print development summary
     */
    printDevSummary() {
        console.log('\n๐Ÿ› ๏ธ Coherent.js Development Summary');
        console.log('=================================');
        console.log(`Total Renders: ${this.renderHistory.length}`);
        console.log(`Total Warnings: ${this.warnings.length}`);
        console.log(`Total Errors: ${this.errors.length}`);
 
        if (this.renderHistory.length > 0) {
            const avgTime = this.renderHistory.reduce((sum, r) => sum + r.renderTime, 0) / this.renderHistory.length;
            console.log(`Average Render Time: ${avgTime.toFixed(2)}ms`);
        }
 
        console.log('=================================\n');
    }
 
    /**
     * Get performance insights
     */
    getPerformanceInsights() {
        const insights = {
            totalRenders: this.renderHistory.length,
            averageRenderTime: 0,
            slowestRender: null,
            fastestRender: null,
            cacheHits: 0,
            totalWarnings: this.warnings.length,
            totalErrors: this.errors.length
        };
 
        if (this.renderHistory.length > 0) {
            const times = this.renderHistory.map(r => r.renderTime);
            insights.averageRenderTime = times.reduce((a, b) => a + b, 0) / times.length;
            insights.slowestRender = Math.max(...times);
            insights.fastestRender = Math.min(...times);
        }
 
        // Get cache stats if available
        if (this.coherent?.cache?.getStats) {
            const cacheStats = this.coherent.cache.getStats();
            insights.cacheHits = cacheStats.hits;
            insights.cacheHitRate = cacheStats.hitRate;
        }
 
        // Add performance monitor data if available
        if (performanceMonitor && performanceMonitor.getStats) {
            const perfStats = performanceMonitor.getStats();
            insights.performanceMonitorStats = perfStats;
        }
 
        return insights;
    }
 
    /**
     * Utility methods
     */
    generateRenderId() {
        return `render_${Date.now()}_${Math.random().toString(36).substr(2, 5)}`;
    }
 
    serializeComponent(component, maxDepth = 3, currentDepth = 0) {
        Iif (currentDepth > maxDepth) return '...';
 
        try {
            Iif (typeof component === 'function') {
                return `[Function: ${component.name || 'anonymous'}]`;
            }
 
            Iif (Array.isArray(component)) {
                return component.slice(0, 3).map(c =>
                    this.serializeComponent(c, maxDepth, currentDepth + 1)
                ).concat(component.length > 3 ? [`...(${component.length - 3} more)`] : []);
            }
 
            Eif (component && typeof component === 'object') {
                const serialized = {};
                const keys = Object.keys(component).slice(0, 10);
 
                for (const key of keys) {
                    Iif (key === 'children' && component[key]) {
                        serialized[key] = this.serializeComponent(component[key], maxDepth, currentDepth + 1);
                    I} else if (typeof component[key] === 'function') {
                        serialized[key] = `[Function: ${component[key].name || 'anonymous'}]`;
                    } else {
                        serialized[key] = component[key];
                    }
                }
 
                Iif (Object.keys(component).length > 10) {
                    serialized['...'] = `(${Object.keys(component).length - 10} more properties)`;
                }
 
                return serialized;
            }
 
            return component;
 
        } catch (_error) {
            return `[Serialization Error: ${_error.message}]`;
        }
    }
 
    clearDevData() {
        this.renderHistory = [];
        this.warnings = [];
        this.errors = [];
        this.componentRegistry.clear();
        console.log('๐Ÿงน Dev data cleared');
    }
 
    toggleFeature(feature) {
        switch (feature) {
            case 'cache':
                if (this.coherent?.cache) {
                    this.coherent.cache.enabled = !this.coherent.cache.enabled;
                    console.log(`Cache ${this.coherent.cache.enabled ? 'enabled' : 'disabled'}`);
                }
                break;
            case 'monitoring':
                if (performanceMonitor) {
                    performanceMonitor.enabled = !performanceMonitor.enabled;
                    console.log(`Monitoring ${performanceMonitor.enabled ? 'enabled' : 'disabled'}`);
                }
                break;
            case 'hot-reload':
                this.hotReloadEnabled = !this.hotReloadEnabled;
                console.log(`Hot reload ${this.hotReloadEnabled ? 'enabled' : 'disabled'}`);
                break;
            default:
                console.log(`Unknown feature: ${feature}`);
        }
    }
 
    validateComponent(component) {
        return this.deepValidateComponent(component);
    }
}
 
/**
 * Create a lightweight dev tools instance
 */
export function createDevTools(coherentInstance, options) {
    return new DevTools(coherentInstance, options);
}
 
/**
 * Global dev tools utilities (available even when DevTools is disabled)
 */
export const devUtils = {
    /**
     * Quick component inspection
     */
    inspect: (component) => {
        console.log('๐Ÿ” Component Inspection:');
        console.log('Type:', typeof component);
        console.log('Structure:', component);
 
        if (isCoherentObject(component)) {
            const tags = Object.keys(component);
            console.log('Tags:', tags);
 
            for (const tag of tags) {
                const props = component[tag];
                if (props && typeof props === 'object') {
                    console.log(`${tag} props:`, Object.keys(props));
                }
            }
        }
    },
 
    /**
     * Simple validation
     */
    validate: (component) => {
        try {
            validateComponent(component);
            console.log('โœ… Component is valid');
            return true;
        } catch (_error) {
            console.error('โŒ Component validation failed:', _error.message);
            return false;
        }
    },
 
    /**
     * Performance timing helper
     */
    time: (label, fn) => {
        const start = performance.now();
        const result = fn();
        const end = performance.now();
        console.log(`โฑ๏ธ ${label}: ${(end - start).toFixed(2)}ms`);
        return result;
    }
};
 
export default DevTools;