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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
import { resetState } from './state.js';
import { formatBytes, getPDFDocument } from './utils/helpers.js';
import { tesseractLanguages } from './config/tesseract-languages.js';
import { renderPagesProgressively, cleanupLazyRendering } from './utils/render-utils.js';
import { icons, createIcons } from 'lucide';
import Sortable from 'sortablejs';
import { getRotationState, updateRotationState } from './utils/rotation-state.js';
import * as pdfjsLib from 'pdfjs-dist';
import { t } from './i18n/i18n';

pdfjsLib.GlobalWorkerOptions.workerSrc = new URL('pdfjs-dist/build/pdf.worker.min.mjs', import.meta.url).toString();


// Centralizing DOM element selection
export const dom = {
    gridView: document.getElementById('grid-view'),
    toolGrid: document.getElementById('tool-grid'),
    toolInterface: document.getElementById('tool-interface'),
    toolContent: document.getElementById('tool-content'),
    backToGridBtn: document.getElementById('back-to-grid'),
    loaderModal: document.getElementById('loader-modal'),
    loaderText: document.getElementById('loader-text'),
    alertModal: document.getElementById('alert-modal'),
    alertTitle: document.getElementById('alert-title'),
    alertMessage: document.getElementById('alert-message'),
    alertOkBtn: document.getElementById('alert-ok'),
    heroSection: document.getElementById('hero-section'),
    featuresSection: document.getElementById('features-section'),
    toolsHeader: document.getElementById('tools-header'),
    dividers: document.querySelectorAll('.section-divider'),
    hideSections: document.querySelectorAll('.hide-section'),
    shortcutsModal: document.getElementById('shortcuts-modal'),
    closeShortcutsModalBtn: document.getElementById('close-shortcuts-modal'),
    shortcutsList: document.getElementById('shortcuts-list'),
    shortcutSearch: document.getElementById('shortcut-search'),
    resetShortcutsBtn: document.getElementById('reset-shortcuts-btn'),
    importShortcutsBtn: document.getElementById('import-shortcuts-btn'),
    exportShortcutsBtn: document.getElementById('export-shortcuts-btn'),
    openShortcutsBtn: document.getElementById('open-shortcuts-btn'),
    warningModal: document.getElementById('warning-modal'),
    warningTitle: document.getElementById('warning-title'),
    warningMessage: document.getElementById('warning-message'),
    warningCancelBtn: document.getElementById('warning-cancel-btn'),
    warningConfirmBtn: document.getElementById('warning-confirm-btn'),
};

export const showLoader = (text = t('common.loading'), progress?: number) => {
    if (dom.loaderText) dom.loaderText.textContent = text;

    // Add or update progress bar if progress is provided
    const loaderModal = dom.loaderModal;
    if (loaderModal) {
        let progressBar = loaderModal.querySelector('.loader-progress-bar') as HTMLElement;
        let progressContainer = loaderModal.querySelector('.loader-progress-container') as HTMLElement;

        if (progress !== undefined && progress >= 0) {
            // Create progress container if it doesn't exist
            if (!progressContainer) {
                progressContainer = document.createElement('div');
                progressContainer.className = 'loader-progress-container w-64 mt-4';
                progressContainer.innerHTML = `
                    <div class="bg-gray-700 rounded-full h-2 overflow-hidden">
                        <div class="loader-progress-bar bg-indigo-500 h-full transition-all duration-300" style="width: 0%"></div>
                    </div>
                    <p class="loader-progress-text text-xs text-gray-400 mt-1 text-center">0%</p>
                `;
                loaderModal.querySelector('.bg-gray-800')?.appendChild(progressContainer);
                progressBar = progressContainer.querySelector('.loader-progress-bar') as HTMLElement;
            }

            // Update progress
            if (progressBar) {
                progressBar.style.width = `${progress}%`;
            }
            const progressText = progressContainer.querySelector('.loader-progress-text');
            if (progressText) {
                progressText.textContent = `${Math.round(progress)}%`;
            }
            progressContainer.classList.remove('hidden');
        } else {
            // Hide progress bar if no progress provided
            if (progressContainer) {
                progressContainer.classList.add('hidden');
            }
        }

        loaderModal.classList.remove('hidden');
    }
};

export const hideLoader = () => {
    if (dom.loaderModal) dom.loaderModal.classList.add('hidden');
};

export const showAlert = (title: any, message: any, type: string = 'error', callback?: () => void) => {
    if (dom.alertTitle) dom.alertTitle.textContent = title;
    if (dom.alertMessage) dom.alertMessage.textContent = message;
    if (dom.alertModal) dom.alertModal.classList.remove('hidden');

    if (dom.alertOkBtn) {
        const newOkBtn = dom.alertOkBtn.cloneNode(true) as HTMLElement;
        dom.alertOkBtn.replaceWith(newOkBtn);
        dom.alertOkBtn = newOkBtn;

        newOkBtn.addEventListener('click', () => {
            hideAlert();
            if (callback) callback();
        });
    }
};

export const hideAlert = () => {
    if (dom.alertModal) dom.alertModal.classList.add('hidden');
};

export const switchView = (view: any) => {
    if (view === 'grid') {
        dom.gridView.classList.remove('hidden');
        dom.toolInterface.classList.add('hidden');
        // show hero and features and header
        dom.heroSection.classList.remove('hidden');
        dom.featuresSection.classList.remove('hidden');
        dom.toolsHeader.classList.remove('hidden');
        // show dividers
        dom.dividers.forEach((divider) => {
            divider.classList.remove('hidden');
        });
        // show hideSections
        dom.hideSections.forEach((section) => {
            section.classList.remove('hidden');
        });

        resetState();
    } else {
        dom.gridView.classList.add('hidden');
        dom.toolInterface.classList.remove('hidden');
        dom.featuresSection.classList.add('hidden');
        dom.heroSection.classList.add('hidden');
        dom.toolsHeader.classList.add('hidden');
        dom.dividers.forEach((divider) => {
            divider.classList.add('hidden');
        });
        dom.hideSections.forEach((section) => {
            section.classList.add('hidden');
        });
    }
};

const thumbnailState = {
    sortableInstances: {},
};

function initializeOrganizeSortable(containerId: any) {
    const container = document.getElementById(containerId);
    if (!container) return;

    if (thumbnailState.sortableInstances[containerId]) {
        thumbnailState.sortableInstances[containerId].destroy();
    }

    thumbnailState.sortableInstances[containerId] = Sortable.create(container, {
        animation: 150,
        ghostClass: 'sortable-ghost',
        chosenClass: 'sortable-chosen',
        dragClass: 'sortable-drag',
        filter: '.delete-page-btn',
        preventOnFilter: true,
        onStart: function (evt: any) {
            evt.item.style.opacity = '0.5';
        },
        onEnd: function (evt: any) {
            evt.item.style.opacity = '1';
        },
    });
}

/**
 * Renders page thumbnails for tools like 'Organize' and 'Rotate'.
 * @param {string} toolId The ID of the active tool.
 * @param {object} pdfDoc The loaded pdf-lib document instance.
 */
export const renderPageThumbnails = async (toolId: any, pdfDoc: any) => {
    const containerId = toolId === 'organize' ? 'page-organizer' : toolId === 'delete-pages' ? 'delete-pages-preview' : 'page-rotator';
    const container = document.getElementById(containerId);
    if (!container) return;

    container.innerHTML = '';

    // Cleanup any previous lazy loading observers
    cleanupLazyRendering();

    const currentRenderId = Date.now();
    container.dataset.renderId = currentRenderId.toString();

    showLoader(t('multiTool.renderingTitle'));

    const pdfData = await pdfDoc.save();
    const pdf = await getPDFDocument({ data: pdfData }).promise;

    // Function to create wrapper element for each page
    const createWrapper = (canvas: HTMLCanvasElement, pageNumber: number) => {
        const wrapper = document.createElement('div');
        // @ts-expect-error TS(2322) FIXME: Type 'number' is not assignable to type 'string'.
        wrapper.dataset.pageIndex = pageNumber - 1;

        const imgContainer = document.createElement('div');
        imgContainer.className =
            'w-full h-36 bg-gray-900 rounded-lg flex items-center justify-center overflow-hidden border-2 border-gray-600';

        const img = document.createElement('img');
        img.src = canvas.toDataURL();
        img.className = 'max-w-full max-h-full object-contain';

        imgContainer.appendChild(img);

        if (toolId === 'organize') {
            wrapper.className = 'page-thumbnail relative group';
            wrapper.appendChild(imgContainer);

            const pageNumSpan = document.createElement('span');
            pageNumSpan.className =
                'absolute top-1 left-1 bg-gray-900 bg-opacity-75 text-white text-xs rounded-full px-2 py-1';
            pageNumSpan.textContent = pageNumber.toString();

            const deleteBtn = document.createElement('button');
            deleteBtn.className =
                'delete-page-btn absolute top-1 right-1 bg-red-600 text-white rounded-full w-6 h-6 flex items-center justify-center';
            deleteBtn.innerHTML = '&times;';
            deleteBtn.addEventListener('click', (e) => {
                (e.currentTarget as HTMLElement).parentElement.remove();

                // Renumber remaining pages
                const pages = container.querySelectorAll('.page-thumbnail');
                pages.forEach((page, index) => {
                    const numSpan = page.querySelector('span');
                    if (numSpan) {
                        numSpan.textContent = (index + 1).toString();
                    }
                });

                initializeOrganizeSortable(containerId);
            });

            wrapper.append(pageNumSpan, deleteBtn);
        } else if (toolId === 'rotate') {
            wrapper.className = 'page-rotator-item flex flex-col items-center gap-2 relative group';

            // Read rotation from state (handles "Rotate All" on lazy-loaded pages)
            const rotationStateArray = getRotationState();
            const pageIndex = pageNumber - 1;
            const initialRotation = rotationStateArray[pageIndex] || 0;

            wrapper.dataset.rotation = initialRotation.toString();
            img.classList.add('transition-transform', 'duration-300');

            // Apply initial rotation if any
            if (initialRotation !== 0) {
                img.style.transform = `rotate(${initialRotation}deg)`;
            }

            wrapper.appendChild(imgContainer);

            // Page Number Overlay (Top Left)
            const pageNumSpan = document.createElement('span');
            pageNumSpan.className =
                'absolute top-2 left-2 bg-gray-900 bg-opacity-75 text-white text-xs font-medium rounded-md px-2 py-1 shadow-sm z-10 pointer-events-none';
            pageNumSpan.textContent = pageNumber.toString();
            wrapper.appendChild(pageNumSpan);

            const controlsDiv = document.createElement('div');
            controlsDiv.className = 'flex flex-col lg:flex-row items-center justify-center w-full gap-2 px-1';

            // Custom Stepper Component
            const stepperContainer = document.createElement('div');
            stepperContainer.className = 'flex items-center border border-gray-600 rounded-md bg-gray-800 overflow-hidden w-24 h-8';

            const decrementBtn = document.createElement('button');
            decrementBtn.className = 'px-2 h-full text-gray-400 hover:text-white hover:bg-gray-700 border-r border-gray-600 transition-colors flex items-center justify-center';
            decrementBtn.innerHTML = '<i data-lucide="minus" class="w-3 h-3"></i>';

            const angleInput = document.createElement('input');
            angleInput.type = 'number';
            angleInput.className = 'no-spinner w-full h-full bg-transparent text-white text-xs text-center focus:outline-none appearance-none m-0 p-0 border-none';
            angleInput.value = initialRotation.toString();
            angleInput.placeholder = "0";

            const incrementBtn = document.createElement('button');
            incrementBtn.className = 'px-2 h-full text-gray-400 hover:text-white hover:bg-gray-700 border-l border-gray-600 transition-colors flex items-center justify-center';
            incrementBtn.innerHTML = '<i data-lucide="plus" class="w-3 h-3"></i>';

            // Helper to update rotation
            const updateRotation = (newRotation: number) => {
                const card = wrapper; // Closure capture
                const imgEl = card.querySelector('img');
                const pageIndex = pageNumber - 1;

                // Update UI
                angleInput.value = newRotation.toString();
                card.dataset.rotation = newRotation.toString();
                imgEl.style.transform = `rotate(${newRotation}deg)`;

                // Update State
                updateRotationState(pageIndex, newRotation);
            };

            // Event Listeners
            decrementBtn.addEventListener('click', (e) => {
                e.stopPropagation();
                const current = parseInt(angleInput.value) || 0;
                updateRotation(current - 1);
            });

            incrementBtn.addEventListener('click', (e) => {
                e.stopPropagation();
                const current = parseInt(angleInput.value) || 0;
                updateRotation(current + 1);
            });

            angleInput.addEventListener('change', (e) => {
                e.stopPropagation();
                const val = parseInt((e.target as HTMLInputElement).value) || 0;
                updateRotation(val);
            });
            angleInput.addEventListener('click', (e) => e.stopPropagation());

            stepperContainer.append(decrementBtn, angleInput, incrementBtn);

            const rotateBtn = document.createElement('button');
            rotateBtn.className = 'rotate-btn btn bg-gray-700 hover:bg-gray-600 p-1.5 rounded-md text-gray-200 transition-colors flex-shrink-0';
            rotateBtn.title = 'Rotate +90°';
            rotateBtn.innerHTML = '<i data-lucide="rotate-cw" class="w-4 h-4"></i>';
            rotateBtn.addEventListener('click', (e) => {
                e.stopPropagation();
                const current = parseInt(angleInput.value) || 0;
                updateRotation(current + 90);
            });

            controlsDiv.append(stepperContainer, rotateBtn);
            wrapper.appendChild(controlsDiv);
        } else if (toolId === 'delete-pages') {
            wrapper.className = 'page-thumbnail relative group cursor-pointer transition-all duration-200';
            wrapper.dataset.pageNumber = pageNumber.toString();

            const innerContainer = document.createElement('div');
            innerContainer.className = 'relative w-full h-36 bg-gray-900 rounded-lg flex items-center justify-center overflow-hidden border-2 border-gray-600 transition-colors duration-200';
            innerContainer.appendChild(img);
            wrapper.appendChild(innerContainer);

            const pageNumSpan = document.createElement('span');
            pageNumSpan.className =
                'absolute top-2 left-2 bg-gray-900 bg-opacity-75 text-white text-xs font-medium rounded-md px-2 py-1 shadow-sm z-10 pointer-events-none';
            pageNumSpan.textContent = pageNumber.toString();
            wrapper.appendChild(pageNumSpan);

            wrapper.addEventListener('click', () => {
                const input = document.getElementById('pages-to-delete') as HTMLInputElement;
                if (!input) return;

                const currentVal = input.value;
                let pages = currentVal.split(',').map(s => s.trim()).filter(s => s);
                const pageStr = pageNumber.toString();

                if (pages.includes(pageStr)) {
                    pages = pages.filter(p => p !== pageStr);
                } else {
                    pages.push(pageStr);
                }

                pages.sort((a, b) => {
                    const numA = parseInt(a.split('-')[0]);
                    const numB = parseInt(b.split('-')[0]);
                    return numA - numB;
                });

                input.value = pages.join(', ');

                input.dispatchEvent(new Event('input'));
            });
        }

        return wrapper;
    };

    try {
        // Render pages progressively with lazy loading
        await renderPagesProgressively(
            pdf,
            container,
            createWrapper,
            {
                batchSize: 8,
                useLazyLoading: true,
                lazyLoadMargin: '300px',
                onProgress: (current, total) => {
                    showLoader(`Rendering page previews: ${current}/${total}`);
                },
                onBatchComplete: () => {
                    createIcons({ icons });
                },
                shouldCancel: () => {
                    return container.dataset.renderId !== currentRenderId.toString();
                }
            }
        );

        if (toolId === 'organize') {
            initializeOrganizeSortable(containerId);
        } else if (toolId === 'delete-pages') {
            // No sortable needed for delete pages
        }

        // Reinitialize lucide icons for dynamically added elements
        createIcons({ icons });
    } catch (error) {
        console.error('Error rendering page thumbnails:', error);
        showAlert(t('multiTool.error'), t('multiTool.errorRendering'));
    } finally {
        hideLoader();
    }
};

/**
 * Renders a list of uploaded files in the specified container.
 * @param {HTMLElement} container The DOM element to render the list into.
 * @param {File[]} files The array of file objects.
 */
export const renderFileDisplay = (container: any, files: any) => {
    container.textContent = '';
    if (files.length > 0) {
        files.forEach((file: any) => {
            const fileDiv = document.createElement('div');
            fileDiv.className =
                'flex items-center justify-between bg-gray-700 p-3 rounded-lg text-sm';

            const nameSpan = document.createElement('span');
            nameSpan.className = 'truncate font-medium text-gray-200';
            nameSpan.textContent = file.name;

            const sizeSpan = document.createElement('span');
            sizeSpan.className = 'flex-shrink-0 ml-4 text-gray-400';
            sizeSpan.textContent = formatBytes(file.size);

            fileDiv.append(nameSpan, sizeSpan);
            container.appendChild(fileDiv);
        });
    }
};

const createFileInputHTML = (options = {}) => {
    // @ts-expect-error TS(2339) FIXME: Property 'multiple' does not exist on type '{}'.
    const multiple = options.multiple ? 'multiple' : '';
    // @ts-expect-error TS(2339) FIXME: Property 'accept' does not exist on type '{}'.
    const acceptedFiles = options.accept || 'application/pdf';
    // @ts-expect-error TS(2339) FIXME: Property 'showControls' does not exist on type '{}... Remove this comment to see the full error message
    const showControls = options.showControls || false; // NEW: Add this parameter

    return `
        <div id="drop-zone" class="relative flex flex-col items-center justify-center w-full h-48 border-2 border-dashed border-gray-600 rounded-xl cursor-pointer bg-gray-900 hover:bg-gray-700 transition-colors duration-300">
            <div class="flex flex-col items-center justify-center pt-5 pb-6">
                <i data-lucide="upload-cloud" class="w-10 h-10 mb-3 text-gray-400"></i>
                <p class="mb-2 text-sm text-gray-400"><span class="font-semibold">${t('upload.clickToSelect')}</span> ${t('upload.orDragAndDrop')}</p>
                <p class="text-xs text-gray-500">${multiple ? t('upload.pdfOrImages') : 'A single PDF file'}</p>
                <p class="text-xs text-gray-500">${t('upload.filesNeverLeave')}</p>
            </div>
            <input id="file-input" type="file" class="absolute top-0 left-0 w-full h-full opacity-0 cursor-pointer" ${multiple} accept="${acceptedFiles}">
        </div>
        
        ${showControls
            ? `
            <!-- NEW: Add control buttons for multi-file uploads -->
            <div id="file-controls" class="hidden mt-4 flex gap-3">
                <button id="add-more-btn" class="btn bg-indigo-600 hover:bg-indigo-700 text-white font-semibold px-4 py-2 rounded-lg flex items-center gap-2">
                    <i data-lucide="plus"></i> ${t('upload.addMore')}
                </button>
                <button id="clear-files-btn" class="btn bg-gray-700 hover:bg-gray-600 text-white font-semibold px-4 py-2 rounded-lg flex items-center gap-2">
                    <i data-lucide="trash-2"></i> ${t('upload.clearAll')}
                </button>
            </div>
        `
            : ''
        }
    `;
};