(function(){'use strict';var DEFAULT_I18N={select_image:'Select image',loading:'Loading…',no_images_picker:'No images in the library yet.',upload_first_link:'Upload your first image',error_loading_retry:'Could not load images. Please try again.'};function t(key){var i18n=window.AdminPickerI18n||{};return i18n[key]||DEFAULT_I18N[key]||key;}
function escapeHtml(s){if(s==null)return'';return String(s).replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/"/g,'&quot;').replace(/'/g,'&#39;');}
function openImagePicker(callback){var modal=document.createElement('div');modal.className='image-picker-overlay';var mc=document.createElement('div');mc.className='image-picker-modal';mc.innerHTML='<div class="image-picker-header">'+'<h2>'+escapeHtml(t('select_image'))+'</h2>'+'<button type="button" class="image-picker-close" aria-label="Close">&times;</button>'+'</div>'+'<div class="image-picker-grid" id="imageGallery">'+'<div class="image-picker-loading">'+escapeHtml(t('loading'))+'</div>'+'</div>';modal.appendChild(mc);document.body.appendChild(modal);function close(){if(modal&&modal.parentNode){modal.parentNode.removeChild(modal);}
document.removeEventListener('keydown',onKey);}
function onKey(e){if(e.key==='Escape')close();}
mc.querySelector('.image-picker-close').addEventListener('click',close);modal.addEventListener('click',function(e){if(e.target===modal)close();});document.addEventListener('keydown',onKey);var gallery=mc.querySelector('#imageGallery');fetch('/admin/media/api/list',{credentials:'same-origin'}).then(function(res){return res.json();}).then(function(images){if(!images||images.length===0){gallery.innerHTML='<div class="image-picker-empty">'+'<p>'+escapeHtml(t('no_images_picker'))+'</p>'+'<a href="/admin/media/upload" target="_blank">'+
escapeHtml(t('upload_first_link'))+'</a>'+'</div>';return;}
var html='';for(var i=0;i<images.length;i++){var img=images[i];var url=img.url||'';var alt=img.alt_text||'';var w=(img.width!=null)?img.width:'?';var h=(img.height!=null)?img.height:'?';html+='<div class="image-picker-item" '+'data-url="'+escapeHtml(url)+'" '+'data-alt="'+escapeHtml(alt)+'">'+'<img src="'+escapeHtml(url)+'" alt="'+escapeHtml(alt)+'">'+'<div class="image-picker-item__alt">'+escapeHtml(alt)+'</div>'+'<div class="image-picker-item__size">'+
escapeHtml(w+' × '+h+' px')+'</div>'+'</div>';}
gallery.innerHTML=html;gallery.addEventListener('click',function(e){var card=e.target.closest('.image-picker-item');if(!card)return;var url=card.getAttribute('data-url')||'';var alt=card.getAttribute('data-alt')||'';try{callback(url,{alt:alt});}finally{close();}});}).catch(function(err){console.error('Error loading images:',err);gallery.innerHTML='<div class="image-picker-error">'+
escapeHtml(t('error_loading_retry'))+'</div>';});}
function pickImageFor(inputId,altInputId){openImagePicker(function(url,data){var input=document.getElementById(inputId);if(input)input.value=url;if(altInputId&&data&&data.alt){var altField=document.getElementById(altInputId);if(altField&&!altField.value){altField.value=data.alt;}}
var preview=document.getElementById(inputId+'_preview');var wrap=document.getElementById(inputId+'_preview_wrap');if(preview)preview.src=url;if(wrap)wrap.style.display='block';if(typeof window.updateImageHints==='function'){window.updateImageHints();}});}
window.openImagePicker=openImagePicker;window.pickImageFor=pickImageFor;})();