/**
 * Folder Metadata functionality for Gallery
 * 
 * Handles folder metadata dropdown display and interaction
 */

// Define function immediately (not waiting for DOM)
window.toggleFolderMetadata = function() {
    console.log('toggleFolderMetadata called');
    
    const container = document.querySelector('.folder-metadata-container');
    const content = document.getElementById('folderMetadataContent');
    
    if (!container) {
        console.log('No metadata container found');
        return;
    }
    
    if (!content) {
        console.log('No metadata content found');
        return;
    }
    
    const isExpanded = container.classList.contains('expanded');
    console.log('Current state - expanded:', isExpanded);
    
    if (isExpanded) {
        // Collapse
        container.classList.remove('expanded');
        content.style.display = 'none';
        console.log('Collapsed dropdown');
    } else {
        // Expand
        container.classList.add('expanded');
        content.style.display = 'block';
        console.log('Expanded dropdown');
    }
    
    // Save state
    localStorage.setItem('folderMetadataExpanded', (!isExpanded).toString());
};

// Also define as regular function for alternative access
function toggleFolderMetadata() {
    window.toggleFolderMetadata();
}

// Toggle long text display
window.toggleMetadataText = function(button) {
    const longText = button.previousElementSibling;
    const fullText = button.nextElementSibling;
    
    if (fullText.style.display === 'none') {
        longText.style.display = 'none';
        fullText.style.display = 'inline';
        button.textContent = 'Show less';
    } else {
        longText.style.display = 'inline';
        fullText.style.display = 'none';
        button.textContent = 'Show more';
    }
};

// Initialize folder metadata on page load
document.addEventListener('DOMContentLoaded', function() {
    console.log('Folder metadata script loaded');
    
    // Make sure all containers start collapsed
    const containers = document.querySelectorAll('.folder-metadata-container');
    console.log('Found', containers.length, 'metadata containers');
    
    containers.forEach(container => {
        const content = container.querySelector('.folder-metadata-content');
        if (content) {
            // Force initial hidden state
            container.classList.remove('expanded');
            content.style.display = 'none';
            console.log('Initialized container as collapsed');
        }
    });
});
