/**
 * HDR Media Library Uploader
 *
 * Handles HDR/EXR file selection from WordPress Media Library
 * and populates the HDR URL input field.
 *
 * @package ProductCustomizer
 */

jQuery(document).ready(function($) {
    var mediaUploader;

    $('#upload_hdr_button').click(function(e) {
        e.preventDefault();

        // If uploader already exists, open it
        if (mediaUploader) {
            mediaUploader.open();
            return;
        }

        // Create media uploader
        mediaUploader = wp.media.frames.file_frame = wp.media({
            title: 'Select HDR Environment Map',
            button: {
                text: 'Use this HDR'
            },
            multiple: false,
            library: {
                type: []  // Allow all file types; we filter on upload_mimes via PHP
            }
        });

        // Handle selection
        mediaUploader.on('select', function() {
            var attachment = mediaUploader.state().get('selection').first().toJSON();
            $('#dprcu_hdr_url').val(attachment.url);
        });

        // Open uploader
        mediaUploader.open();
    });
});
