/**
 * Event listeners setup
 */

/**
 * Set up all event listeners for the application
 */
function setupEventListeners() {
    // Add Local Directory button (was id="select-directory" / "refresh-directory")
    const selectDirectoryBtn = document.getElementById('addDirectoryBtn');
    if (selectDirectoryBtn) {
        selectDirectoryBtn.addEventListener('click', openDirectory);
    }

    // Refresh button (now in header, was in file-nav pane)
    const refreshDirectoryBtn = document.getElementById('refreshHeaderBtn');
    if (refreshDirectoryBtn) {
        refreshDirectoryBtn.addEventListener('click', refreshDirectory);
    }

    // New file (root) button
    const newFileRootBtn = document.getElementById('new-file-root');
    if (newFileRootBtn) {
        newFileRootBtn.addEventListener('click', () => {
            if (directoryHandle) {
                createNewFile('');
            }
        });
    }

    // Save All button
    const saveAllBtn = document.getElementById('save-all');
    if (saveAllBtn) {
        saveAllBtn.addEventListener('click', saveAllFiles);
    }

    // Warn when leaving with unsaved changes
    window.addEventListener('beforeunload', function (e) {
        let hasUnsavedChanges = false;

        editorInstances.forEach((instanceData) => {
            if (instanceData.isDirty) {
                hasUnsavedChanges = true;
            }
        });

        if (hasUnsavedChanges) {
            e.preventDefault();
            return 'You have unsaved changes. If you leave now, your changes will be lost.';
        }
    });
}

/**
 * Set up TOC depth selector
 */
function setupTocDepthSelector() {
    const depthSelector = document.getElementById('toc-depth-selector');
    if (!depthSelector) return;

    depthSelector.value = tocMaxDepth.toString();

    depthSelector.addEventListener('change', function () {
        tocMaxDepth = parseInt(this.value, 10);

        if (currentFileHandle && currentFileHandle.name.match(/\.(md|markdown)$/i)) {
            const filePath = currentFileHandle.name;
            const instance = editorInstances.get(filePath);

            if (instance && instance.editor && instance.tocContainer) {
                const content = instance.editor.getMarkdown();

                try {
                    updateToc(content, instance.tocContainer, instance.editor, tocMaxDepth);
                } catch (error) {
                    console.error('Error updating TOC depth:', error);
                }
            }
        }
    });
}
