File size: 10,543 Bytes
d58d97b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/*

   ELYSIA CODE COMPANION v1.2.2 - Utility Functions

   Toast, modals, storage, validation

*/

const Utils = {
    // Toast Notifications
    toast: {
        show(message, type = "info", duration = 3000) {
            const container = document.getElementById("toast-container");
            const toast = document.createElement("div");
            toast.className = `toast ${type}`;

            const icons = {
                success: "βœ…",
                error: "❌",
                warning: "⚠️",
                info: "ℹ️"
            };

            toast.innerHTML = `

                <span class="toast-icon">${icons[type]}</span>

                <span class="toast-message">${message}</span>

                <button class="toast-close">Γ—</button>

            `;

            container.appendChild(toast);

            const close = () => {
                toast.style.animation = "slideOut 0.3s ease";
                setTimeout(() => toast.remove(), 300);
            };

            toast.querySelector(".toast-close").onclick = close;
            if (duration > 0) setTimeout(close, duration);

            return toast;
        },

        success: (msg, duration) => Utils.toast.show(msg, "success", duration),
        error: (msg, duration) => Utils.toast.show(msg, "error", duration),
        warning: (msg, duration) => Utils.toast.show(msg, "warning", duration),
        info: (msg, duration) => Utils.toast.show(msg, "info", duration)
    },

    // Modal Management
    modal: {
        _initialized: false, // Flag to prevent memory leaks from duplicate listeners

        open(modalId) {
            const modal = document.getElementById(modalId);
            if (modal) {
                modal.classList.add("active");
                document.body.style.overflow = "hidden";
            }
        },

        close(modalId) {
            const modal = document.getElementById(modalId);
            if (modal) {
                modal.classList.remove("active");
                document.body.style.overflow = "";
            }
        },

        init() {
            // Prevent duplicate listeners (memory leak)
            if (this._initialized) return;
            this._initialized = true;

            // Close on click outside
            document.querySelectorAll(".modal").forEach(modal => {
                modal.addEventListener("click", e => {
                    if (e.target === modal) {
                        Utils.modal.close(modal.id);
                    }
                });
            });

            // Close buttons
            document.querySelectorAll(".modal-close, [data-modal]").forEach(btn => {
                btn.addEventListener("click", () => {
                    const modalId = btn.getAttribute("data-modal");
                    if (modalId) Utils.modal.close(modalId);
                });
            });
        }
    },

    // Local Storage Wrapper
    storage: {
        get(key, defaultValue = null) {
            try {
                const value = localStorage.getItem(key);
                return value ? JSON.parse(value) : defaultValue;
            } catch {
                return defaultValue;
            }
        },

        set(key, value) {
            try {
                localStorage.setItem(key, JSON.stringify(value));
                return true;
            } catch {
                return false;
            }
        },

        remove(key) {
            localStorage.removeItem(key);
        },

        clear() {
            localStorage.clear();
        }
    },

    // API Key Validation
    validateApiKey(key) {
        if (!key) {
            return { valid: false, error: "API key is required" };
        }

        if (!key.startsWith("sk-or-")) {
            return { valid: false, error: "Invalid OpenRouter API key format (must start with 'sk-or-')" };
        }

        if (key.length < 20) {
            return { valid: false, error: "API key too short" };
        }

        return { valid: true };
    },

    // File Size Formatter
    formatFileSize(bytes) {
        if (bytes === 0) return "0 B";
        const k = 1024;
        const sizes = ["B", "KB", "MB", "GB"];
        const i = Math.floor(Math.log(bytes) / Math.log(k));
        return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + " " + sizes[i];
    },

    // Get File Extension
    getFileExtension(filename) {
        const parts = filename.split(".");
        return parts.length > 1 ? parts[parts.length - 1].toLowerCase() : "";
    },

    // Get Language from Extension
    getLanguageFromExtension(ext) {
        const map = {
            js: "javascript",
            jsx: "javascript",
            ts: "typescript",
            tsx: "typescript",
            py: "python",
            rb: "ruby",
            java: "java",
            cpp: "cpp",
            c: "c",
            h: "c",
            hpp: "cpp",
            cs: "csharp",
            go: "go",
            rs: "rust",
            php: "php",
            html: "html",
            htm: "html",
            css: "css",
            scss: "scss",
            less: "less",
            sass: "sass",
            json: "json",
            xml: "xml",
            md: "markdown",
            sql: "sql",
            sh: "bash",
            bash: "bash",
            zsh: "bash",
            yml: "yaml",
            yaml: "yaml",
            toml: "toml",
            vue: "markup",
            svelte: "markup",
            astro: "markup",
            swift: "swift",
            kt: "kotlin",
            kts: "kotlin",
            dart: "dart",
            lua: "lua",
            r: "r",
            scala: "scala",
            groovy: "groovy",
            txt: "none",
            log: "none",
            env: "bash",
            dockerfile: "docker",
            makefile: "makefile"
        };
        return map[ext] || "none";
    },

    // Debounce Function
    debounce(func, wait) {
        let timeout;
        const debounced = function executedFunction(...args) {
            const later = () => {
                timeout = null;
                func(...args);
            };
            if (timeout) clearTimeout(timeout);
            timeout = setTimeout(later, wait);
        };
        // Cleanup method for when component unmounts
        debounced.cancel = () => {
            if (timeout) clearTimeout(timeout);
        };
        return debounced;
    },

    // Copy to Clipboard
    async copyToClipboard(text) {
        try {
            await navigator.clipboard.writeText(text);
            Utils.toast.success("Copied to clipboard!");
            return true;
        } catch (err) {
            Utils.toast.error("Failed to copy to clipboard");
            return false;
        }
    },

    // Format Date/Time
    formatDateTime(date) {
        const d = new Date(date);
        const pad = n => n.toString().padStart(2, "0");
        return `${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`;
    },

    // Escape HTML
    escapeHtml(text) {
        const div = document.createElement("div");
        div.textContent = text;
        return div.innerHTML;
    },

    // Truncate Text
    truncate(text, maxLength) {
        return text.length > maxLength ? text.substring(0, maxLength) + "..." : text;
    },

    // Check if File is Text
    isTextFile(filename) {
        const textExtensions = [
            "txt",
            "md",
            "js",
            "jsx",
            "ts",
            "tsx",
            "json",
            "html",
            "css",
            "scss",
            "less",
            "sass",
            "py",
            "rb",
            "java",
            "cpp",
            "c",
            "h",
            "hpp",
            "cs",
            "go",
            "rs",
            "php",
            "xml",
            "yml",
            "yaml",
            "toml",
            "sql",
            "sh",
            "bash",
            "zsh",
            "bat",
            "ps1",
            "vue",
            "svelte",
            "astro",
            "swift",
            "kt",
            "kts",
            "dart",
            "lua",
            "r",
            "scala",
            "groovy",
            "dockerfile",
            "makefile",
            "cmake",
            "env",
            "gitignore",
            "editorconfig"
        ];
        const ext = Utils.getFileExtension(filename);
        // Also check for files without extensions like Dockerfile, Makefile
        const baseName = filename.toLowerCase();
        return (
            textExtensions.includes(ext) ||
            ["dockerfile", "makefile", "gemfile", "rakefile", "procfile"].includes(baseName)
        );
    },

    // Check if File is Code
    isCodeFile(filename) {
        const codeExtensions = [
            "js",
            "jsx",
            "ts",
            "tsx",
            "py",
            "rb",
            "java",
            "cpp",
            "c",
            "cs",
            "go",
            "rs",
            "php",
            "vue",
            "svelte"
        ];
        const ext = Utils.getFileExtension(filename);
        return codeExtensions.includes(ext);
    },

    // Loading Overlay
    loading: {
        show(text = "Processing...") {
            const overlay = document.getElementById("loading-overlay");
            const loadingText = document.getElementById("loading-text");
            if (overlay && loadingText) {
                loadingText.textContent = text;
                overlay.style.display = "flex";
            }
        },

        hide() {
            const overlay = document.getElementById("loading-overlay");
            if (overlay) {
                overlay.style.display = "none";
            }
        },

        update(text) {
            const loadingText = document.getElementById("loading-text");
            if (loadingText) {
                loadingText.textContent = text;
            }
        }
    }
};

// Initialize modals on load
document.addEventListener("DOMContentLoaded", () => {
    Utils.modal.init();
});

export default Utils;