Jake Vanderwerf
2026-01-01 58dccc86754deda247eb49310c266f6cba86d36a
assets/js/concise/UploadManager.js
@@ -1,104 +1,1074 @@
/**
 * UploadManager - Refactored for clarity
 *
 * Architecture:
 * - DataStores (fieldStore, uploadStore) = Recovery cache only, cleared after successful upload
 * - Maps (uploadElements, fieldElements) = Runtime DOM references
 * - Upload data flows: File → Process → Queue → Server → Clean up stores
 */
class UploadManager {
   constructor() {
      // Load dependencies
      this.queue = window.jvbQueue;
      this.a11y = window.jvbA11y;
      this.queue = window.jvbQueue;
      this.error = window.jvbError;
      // RECOVERY STORES - Cleared after successful upload
      this.fieldStore = window.jvbStore.register(
         'upload_fields',
         {
            storeName: 'fieldStates',
            keyPath: 'id',
            version: 2,
            indexes: [
               { name: 'fieldId', keyPath: 'fieldId' },
               { name: 'timestamp', keyPath: 'timestamp' },
               { name: 'content', keyPath: 'content' },
               { name: 'itemId', keyPath: 'itemId' },
               { name: 'status', keyPath: 'status' }
            ],
            TTL: 86400000 * 7, // 1 week
            delayFetch: true
         });
      this.uploadStore = window.jvbStore.register(
         'uploads',
         {
            name: 'uploads',
            storeName: 'uploads',
            keyPath: 'id',
            storeBlobs: true,
            indexes: [
               { name: 'fieldId', keyPath: 'fieldId' },
               { name: 'status', keyPath: 'status' },
               { name: 'groupId', keyPath: 'groupId' },
               { name: 'attachmentId', keyPath: 'attachmentId' }
            ],
            delayFetch: true
         });
      window.jvbUploadBlobs = this.uploadStore;
      // Subscribe to store events
      this.fieldStore.subscribe(this.handleFieldStoreEvent.bind(this));
      this.uploadStore.subscribe(this.handleUploadStoreEvent.bind(this));
      // RUNTIME DATA - DOM references and ephemeral state
      this.uploadElements = new Map();  // uploadId → { element, preview, location }
      this.fieldElements = new Map();   // fieldId → { element, ui, config }
      this.groupElements = new Map();   // groupId → { element, grid, fieldId }
      // Selection and UI state
      this.selected = new Map();
      this.selectionHandlers = new Map();
      this.previewUrls = new Set();
      this.sortableInstances = new Map();
      // Worker for image processing
      this.initWorker();
      // Notification subscribers
      this.subscribers = new Set();
      // Controllers
      this.dragController = null;
      this.initStores();
      this.initWorker();
      // Selectors
      //Maps for DOM references
      this.fields = new Map();
      this.uploads = new Map();
      this.groups = new Map();
      this.selected = new Map();
      this.selectionHandlers = new Map();
      this.sortables = new Map();
      this.previewUrls = new Set();
      this.initElements();
      this.initListeners();
   }
   initStores() {
      const {uploads, groups} = window.jvbStore.register(
         'uploads',
         [
            {
               storeName: 'uploads',
               keyPath: 'id',
               indexes: [
                  { name: 'field', keyPath: 'field' },
                  { name: 'status', keyPath: 'status' },
                  { name: 'group', keyPath: 'group' },
                  { name: 'src', keyPath: 'src' }
               ],
            },
            {
               storeName: 'groups',
               keyPath: 'id',
               indexes: [
                  { name: 'field', keyPath: 'field' },
                  { name: 'src', keyPath: 'src' }
               ]
            }
         ]
      );
      this.stores = {
         uploads: uploads,
         groups: groups,
         ready: []
      };
      this.stores.uploads.subscribe(this.handleStores.bind(this, 'uploads'));
      this.stores.groups.subscribe(this.handleStores.bind(this, 'groups'));
      this.queue.subscribe((event, operation) => {
         if (!['uploads', 'uploads/meta', 'uploads/groups'].includes(operation.endpoint)) {
            return;
         }
         const fieldId = operation.data instanceof FormData
            ? operation.data.get('fieldId')
            : operation.data?.fieldId;
         if (!fieldId) {
            return;
         }
         switch (event) {
            case 'cancel-operation':
               this.handleOperationCancelled(fieldId).then(()=>{});
               break;
            case 'operation-status':
               this.handleFieldStatus(fieldId, operation).then(()=>{});
               break;
            case 'operation-completed':
               this.handleOperationComplete(operation, fieldId).then(()=>{});
               break;
            case 'operation-failed':
            case 'operation-failed-permanent':
               this.handleOperationFailed(operation, fieldId).then(()=>{});
               break;
         }
      });
   }
   storesReady() {
      return this.stores.ready.length === 2;
   }
   handleStores(storeName, event) {
      if (event === 'data-ready') {
         this.stores.ready.push(storeName);
         if (this.storesReady()) {
            this.checkRecovery();
         }
      }
   }
   initWorker() {
      this.worker = null;
      this.workerState = {
         worker: null,
         tasks: new Map(),
         restart: { count: 0, max: 3 },
         settings: {
            timeout: 3000,
            maxConcurrent: 3,
            restartAfterTimeout: true
         }
      };
   }
   initElements() {
      this.selectors = {
         field: {
         fields: {
            field: '[data-upload-field]',
            input: 'input[type="file"]',
            dropZone: '.file-upload-container',
            preview: '.item-grid.preview',
            progress: '.image-progress'
            preview: '.preview-wrap',
            grid: '.item-grid.preview',
            progress: {
               progress: '.file-upload-container .progress',
               fill: '.file-upload-container .progress .fill',
               details: '.file-upload-container .progress .details',
               icon: '.file-upload-container .progress .icon'
            },
            selectAll: '[name="select-all-uploads"]',
            actions: '.selection-actions',
            count: '.selection-count',
            hidden: 'input[type="hidden"]'
         },
         // groups = selectors that affect groups as a whole
         groups: {
            container: '.upload-group',
            grid: '.item-grid.group',
            header: '.group-header',
            container: '.group-display',
            grid: '.item-grid.groups',
            empty: '.empty-group',
            header: '.sidebar .header',
         },
         // group = selectors that affect individual groups
         group: {
            item: '.upload-group',
            actions: '.selection-actions',
            selectAll: '[name="select-all-group"]',
            actions: '.group-actions',
            count: '.selection-controls .info'
            count: '.group-header .info',
            fields: 'details .fields',
            grid: '.item-grid.group',
            total: '.group-content .group-count'
         },
         items: {
            item: '[data-upload-id]',
            checkbox: '[name*="select-item"]',
            featured: '[name="featured"]',
            details: 'details'
            image: 'img',
            details: 'details',
            progress: {
               progress: '.progress',
               fill: '.fill',
               details: '.details',
               icon: '.icon'
            }
         }
      };
   }
   initListeners() {
      this.clickHandler = this.handleClick.bind(this);
      this.changeHandler = this.handleChange.bind(this);
      this.dragEnterHandler = this.handleDragEnter.bind(this);
      this.dragLeaveHandler = this.handleDragLeave.bind(this);
      this.dragOverHandler = this.handleDragOver.bind(this);
      this.dropHandler = this.handleDrop.bind(this);
      document.addEventListener('click', this.clickHandler);
      document.addEventListener('change', this.changeHandler);
      document.addEventListener('dragenter', this.dragEnterHandler);
      document.addEventListener('dragleave', this.dragLeaveHandler);
      document.addEventListener('dragover', this.dragOverHandler);
      document.addEventListener('drop', this.dropHandler);
      window.addEventListener('beforeunload', () => {
         this.cleanupAllPreviewUrls();
      });
   }
   async setUpload(uploadId, data) {
      const defaults = {
         id: uploadId,
         attachment: null,
         group: null,
         field: null,
         src: window.location.href,
         blob: null,
         status: 'local_processing',
         operationId: null,
         fields: {}
      };
      const upload = { ...defaults, ...data };
      Object.preventExtensions(upload);
      await this.stores.uploads.save(upload);
      return upload;
   }
   /*********************************************************************
    UTILITY
   *********************************************************************/
   createPreviewUrl(file) {
      const url = URL.createObjectURL(file);
      this.previewUrls.add(url);
      return url;
   }
   revokePreviewUrl(url) {
      if (url?.startsWith('blob:')) {
         URL.revokeObjectURL(url);
         this.previewUrls.delete(url);
      }
   }
   formatFile(upload) {
      if (!upload.blob) return null;
      return new File([upload.blob], upload.fields.originalName || 'file', {
         type: upload.fields.type || upload.blob.type,
         lastModified: upload.fields.lastModified || Date.now()
      });
   }
   /*********************************************************************
    LISTENERS
   *********************************************************************/
   handleClick(e) {
      //Open the file input if it's a dropzone
      let dropZone = window.targetCheck(e, this.selectors.fields.dropZone);
      if (dropZone && !e.target.matches('input, button, a')){
         dropZone.querySelector(this.selectors.fields.input)?.click();
      }
      //Handle action buttons
      const button = window.targetCheck(e, '[data-action]');
      if (button) this.handleAction(button);
   }
      handleAction(button) {
         const action = button.dataset.action;
         const fieldId = this.getFieldIdFromElement(button);
         switch (action) {
            case 'add-to-group':
               this.handleAddToGroup(fieldId).then(()=>{});
               break;
            case 'delete-group':
               this.handleDeleteGroup(button);
               break;
            case 'delete-upload':
            case 'remove-from-group':
               this.handleRemoveItem(button).then(()=>{});
               break;
            case 'upload':
               this.queueUploads('uploads/groups',fieldId).then(()=>{});
               break;
            case 'restore':
               this.handleRestoreSelected().then(()=>{});
               break;
            case 'restore-all':
               this.handleRestoreAll().then(()=>{});
               break;
            case 'clear-cache':
               this.handleClearCache().then(()=>{});
               break;
         }
      }
   handleChange(e) {
      let fieldId = this.getFieldIdFromElement(e.target);
      if (!fieldId) return;
      if (e.target.matches(this.selectors.fields.input)) {
         const files = Array.from(e.target.files);
         if (files.length > 0) this.processFiles(fieldId, files).then(()=>{});
         return;
      }
      // Skip selection-related inputs
      if (e.target.matches(this.selectors.items.checkbox) ||
         e.target.matches(this.selectors.items.featured) ||
         e.target.matches('[name*="select-"]')) {
         return;
      }
      let field = this.fields.get(fieldId);
      if (!field || !field.config.autoUpload) return;
      if (field.config.destination === 'post_group') {
         this.handleGroupMetaChange(e.target);
      } else {
         this.queueUploadMeta(e).then(()=>{});
      }
   }
      handleGroupMetaChange(input) {
         const element = input.closest(this.selectors.group.fields);
         if (!element) return;
         const groupId = element.dataset.groupId;
         const group = this.stores.groups.get(groupId);  // Changed from this.groups
         if (!group) return;
         window.debouncer.schedule(`group-meta-${groupId}`, async (input, groupId) => {
            let name = input.name
               .replace(`${groupId}_`, '')
               .replace(`${groupId}[`, '')
               .replace(']', '');
            group.fields[name] = input.value;
            await this.setGroup(groupId, group);
         }, 300);
      }
   handleDragEnter(e) {
      if (!e.dataTransfer.types.includes('Files')) return;
      const dropZone = e.target.closest(this.selectors.fields.dropZone);
      if (dropZone) {
         e.preventDefault();
         dropZone.classList.add('dragover');
      }
   }
   handleDragLeave(e) {
      const dropZone = e.target.closest(this.selectors.fields.dropZone);
      if (dropZone && !dropZone.contains(e.relatedTarget)) {
         dropZone.classList.remove('dragover');
      }
   }
   handleDragOver(e) {
      if (!e.dataTransfer.types.includes('Files')) return;
      const dropZone = e.target.closest(this.selectors.fields.dropZone);
      if (dropZone) {
         e.preventDefault();
         e.dataTransfer.dropEffect = 'copy';
      }
   }
   handleDrop(e) {
      const dropZone = e.target.closest(this.selectors.fields.dropZone);
      if (!dropZone) return;
      e.preventDefault();
      dropZone.classList.remove('dragover');
      dropZone.classList.add('uploading');
      const files = Array.from(e.dataTransfer.files);
      if (files.length === 0) return;
      const fieldId = this.getFieldIdFromElement(dropZone);
      if (fieldId) {
         this.processFiles(fieldId, files).then(()=>{});
         this.a11y.announce(`${files.length} file(s) dropped for upload`);
      }
   }
   async queueUploads(endpoint, fieldId) {
      let data = new FormData();
      const field = this.fields.get(fieldId);
      if (!field) return;
      let uploads = this.stores.uploads.filterByIndex({field: fieldId});
      if (uploads.length === 0) return;
      const [ isUpload, isGroups] =
         [ endpoint === 'uploads', endpoint === 'uploads/groups'];
      data.append('fieldId', field.id);
      data.append('content', field.config.content);
      if (isUpload) {
         data.append('mode', field.config.mode);
         data.append('field_name', field.config.name);
         data.append('fieldId', field.id);
         data.append('field_type', field.config.type);
         data.append('subtype', field.config.subtype);
         data.append('item_id', field.config.itemID);
         data.append('destination', field.config.destination);
      }
      let posts, uploadMap, files;
      if (isGroups) {
         ({posts, uploadMap, files} = this.collectGroups(fieldId));
      } else if (isUpload) {
         ({uploadMap, files} = this.collectUploads(fieldId));
      }
      if (isGroups) {
         data.append('posts', JSON.stringify(posts));
      }
      files.forEach(file => {
         data.append('files[]', file);
      });
      data.append('upload_ids', JSON.stringify(uploadMap));
      let title, popup;
      if (isUpload) {
         title = `Uploading ${uploads.length} file${uploads.length>1?'s':''} to server...`;
         popup = `Uploading ${uploads.length} file${uploads.length>1?'s':''}...`;
      } else if (isGroups) {
         title = `Creating ${posts.length} ${field.config.content}${posts.length > 1 ? 's' : ''} from uploads...`;
         popup = `Creating ${posts.length} post${posts.length>1?'s':''}...`;
      }
      await this.setBulkUpload(uploads, 'status', 'queued');
      let operationId = this.sendToQueue(endpoint, data, title, popup);
      if (endpoint === 'uploads/groups') {
         let details = field.element.closest('details');
         if (details) {
            details.open = false;
         }
      }
      if (operationId) {
         field.operationId = operationId;
         await this.setBulkUpload(uploads, 'operationId', operationId);
         await this.setBulkUpload(uploads, 'status', 'uploading');
         await this.setBulkGroup(fieldId, 'operationId', operationId);
         this.fields.set(field.id, field);
      } else {
         await this.setBulkUpload(uploads, 'status', 'failed');
      }
      this.notify('sent-to-queue', fieldId);
      return operationId;
   }
   async sendToQueue(endpoint, data, title = '', popup = '', mergable = false) {
      if (popup === '') {
         popup = title;
      }
      const operation = {
         endpoint: endpoint,
         method: 'POST',
         data: data,
         title: title,
         popup: popup,
         canMerge: mergable,
         sendNow: endpoint === 'uploads/groups',
         headers: {
            'action_nonce': window.auth.getNonce('dash')
         },
         append: '_upload'
      }
      try {
         return await this.queue.addToQueue(operation);
      } catch (error) {
         this.error.log(error, {
            component: 'UploadManager',
            action: 'sentToQueue'
         });
         return false;
      }
   }
   collectGroups(fieldId) {
      let uploads = this.stores.uploads.filterByIndex({field: fieldId});
      let groups = this.stores.groups.filterByIndex({field: fieldId});
      let posts = [];
      let uploadMap = [];
      let files = [];
      for (const group of groups) {
         const post = {
            images: [],
            fields: group.fields??{}
         };
         const groupUploads = uploads.filter(u => u.group === group.id);
         for (const upload of groupUploads) {
            const file = this.formatFile(upload);
            if (file) {
               files.push(file);
               const imageData = {
                  upload_id: upload.id,
                  index: uploadMap.length
               };
               let uploadEl = this.uploads.get(upload.id);
               if (uploadEl.ui?.featured?.checked) {
                  post.fields.featured = upload.id;
               }
               post.images.push(imageData);
               uploadMap.push(upload.id);
            }
         }
         posts.push(post);
      }
      const remaining = uploads.filter(u => !u.group);
      for (const upload of remaining) {
         const post = {
            images: [],
            fields: {}
         };
         const file = this.formatFile(upload);
         if (file) {
            files.push(file);
            const imageData = {
               upload_id: upload.id,
               index: uploadMap.length
            };
            post.images.push(imageData);
            uploadMap.push(upload.id);
         }
         posts.push(post);
      }
      return {posts, uploadMap, files};
   }
   collectUploads(fieldId) {
      let uploads = this.stores.uploads.filterByIndex({field: fieldId});
      if (uploads.length === 0) return;
      let uploadMap = [];
      let files = [];
      for (const upload of uploads) {
         const file = this.formatFile(upload);
         if (file) {
            files.push(file);
            uploadMap.push(upload.id);
         }
      }
      return { uploadMap, files };
   }
   async queueUploadMeta(e) {
      const uploadId = e.target.closest(this.selectors.items.item)?.dataset.uploadId;
      const upload = this.stores.uploads.get(uploadId);
      if (!uploadId || !upload) return;
      const field = this.fields.get(upload.field);
      if (!field) return;
      let data = {};
      data[e.target.name] = e.target.value;
      upload.fields = { ...upload.fields, ...data };
      await this.setUpload(upload.id, upload);
      let queueData = {};
      queueData[upload.attachmentId ?? upload.id] = upload.fields;
      return await this.sendToQueue('uploads/meta', queueData, 'Uploading Meta', '', true);
   }
   async handleOperationComplete(operation, fieldId) {
      const response = operation.response;
      // Handle direct upload results (from uploads endpoint)
      if (response?.data) {
         const results = Array.isArray(response.data) ? response.data : Object.values(response.data);
         for (const result of results) {
            if (result.upload_id && result.attachment_id) {
               const upload = this.stores.uploads.get(result.upload_id);
               if (upload) {
                  upload.attachmentId = result.attachment_id;
                  upload.status = 'completed';
                  await this.stores.uploads.save(upload);
               }
            }
         }
      }
      // Clear completed uploads and groups
      const uploads = this.stores.uploads.filterByIndex({field: fieldId});
      const groups = this.stores.groups.filterByIndex({field: fieldId});
      await Promise.all([
         ...uploads
            .filter(upload => upload.status === 'completed')
            .map(upload => this.clearUpload(upload.id)),
         ...groups.map(group => this.stores.groups.delete(group.id))
      ]);
      this.notify('uploads-complete', { fieldId, response });
   }
   /*********************************************************************
    FIELD LOGIC
   *********************************************************************/
   scanFields(container, autoUpload = true) {
      const fields = container.querySelectorAll(this.selectors.fields.field);
      fields.forEach(uploader => this.registerField(uploader, autoUpload));
   }
   registerField(element, autoUpload = true, id = null) {
      const data = {
         element: element,
         id: (id) ? id : this.determineFieldId(element),
         config: this.extractFieldConfig(element, autoUpload),
         uploads: new Set(),
         operationId: null,
         groups: [],
         ui: window.uiFromSelectors(this.selectors.fields, element),
         groupUI: window.uiFromSelectors(this.selectors.groups, element)
      };
      this.fields.set(data.id, data);
      element.dataset.uploader = data.id;
      this.getSelectionHandler(data.id);
      if (data.config.type !== 'single') {
         this.initSortable(data.id);
      }
      return data.id;
   }
   extractFieldConfig(fieldElement, autoUpload) {
      return {
         autoUpload: autoUpload,
         destination: fieldElement.dataset.destination || 'meta', //TODO: why do we need this?
         content: this.extractFieldContent(fieldElement),
         mode: fieldElement.dataset.mode || 'direct',
         type: fieldElement.dataset.type || 'single',
         name: fieldElement.dataset.field,
         itemID: this.extractFieldItemId(fieldElement)??0,
         maxFiles: parseInt(fieldElement.dataset.maxFiles)??25,
         subType: fieldElement.dataset.subtype?? 'image'
      };
   }
   extractFieldContent(fieldElement) {
      return fieldElement.dataset.content ||
         fieldElement.closest('dialog')?.dataset.content ||
         fieldElement.closest('form')?.dataset.save || null;
   }
   extractFieldItemId(fieldElement) {
      return fieldElement.dataset.itemId ||
      fieldElement.closest('dialog')?.dataset.itemId || null;
   }
   determineFieldId(fieldElement) {
      let content = this.extractFieldContent(fieldElement);
      content = (content === null) ? '' : content+'_';
      let itemID = this.extractFieldItemId(fieldElement);
      itemID = (itemID === null) ? '' : itemID+'_';
      const field = fieldElement.dataset.field || '';
      return `${content}${itemID}${field}`;
   }
   getFieldIdFromElement(el) {
      const field = el.closest(this.selectors.fields.field);
      return field?.dataset.uploader || null;
   }
   updateFieldProgress(fieldId, current, total, message) {
      const field = this.fields.get(fieldId);
      if (!field) return;
      window.showProgress(field.ui.progress,current, total, message);
   }
   /*********************************************************************
    IMAGE PROCESSING FILE PROCESSING
   *********************************************************************/
   getWorker() {
      if (!this.workerState.worker && typeof OffscreenCanvas !== 'undefined') {
         this.workerState.worker = new Worker('worker.js');
         this.workerState.worker.onmessage = (e) => this.handleWorkerMessage(e);
         this.workerState.worker.onerror = (e) => this.handleWorkerError(e);
      }
      return this.workerState.worker;
   }
   handleWorkerMessage(e) {
      const { id, blob } = e.data;
      const task = this.workerState.tasks.get(id);
      if (task) {
         clearTimeout(task.timeoutId);
         task.resolve(blob);
         this.workerState.tasks.delete(id);
      }
   }
   handleWorkerError(e) {
      // Reject all pending tasks
      this.workerState.tasks.forEach(task => {
         clearTimeout(task.timeoutId);
         task.reject(e);
      });
      this.workerState.tasks.clear();
      this.restartWorker();
   }
   restartWorker() {
      if (this.workerState.worker) {
         this.workerState.worker.terminate();
         this.workerState.worker = null;
      }
      this.workerState.restart.count++;
   }
   async processImages(files, maxWidth = 2200, maxHeight = 2200){
      const results = [];
      const queue = [...files];
      const concurrency = this.workerState.settings.maxConcurrent;
      const processNext = async () => {
         while (queue.length > 0) {
            const file = queue.shift();
            results.push(await this.processImage(file, maxWidth, maxHeight));
         }
      };
      this.statusMapping = {
      await Promise.all(
         Array.from({length: Math.min(concurrency, files.length)}, () => processNext())
      );
      return results;
   }
   async processImage(file, maxWidth = 2200, maxHeight = 2200, timeout = 3000){
      if (typeof OffscreenCanvas=== 'undefined') {
         return this.resizeImage(file,maxWidth,maxHeight);
      }
      try {
         return await this.withTimeout(
            this.workerImage(file, maxWidth, maxHeight),
            timeout
         );
      } catch (e) {
         return this.resizeImage(file, maxWidth, maxHeight);
      }
   }
   withTimeout(promise, ms) {
      return Promise.race([
         promise,
         new Promise((_, reject) =>
            setTimeout(() => reject(new Error('Timeout')), ms)
         )
      ]);
   }
   async workerImage(file, maxWidth = 2200, maxHeight = 2200) {
      const { settings, restart } = this.workerState;
      if (restart.count >= restart.max) {
         throw new Error('Worker max restarts exceeded');
      }
      const bitmap = await createImageBitmap(file);
      let { width, height } = bitmap;
      if (width > maxWidth || height > maxHeight) {
         const ratio = Math.min(maxWidth / width, maxHeight / height);
         width = Math.round(width * ratio);
         height = Math.round(height * ratio);
      }
      const worker = this.getWorker();
      const id = crypto.randomUUID();
      return new Promise((resolve, reject) => {
         const timeoutId = setTimeout(() => {
            this.workerState.tasks.delete(id);
            if (settings.restartAfterTimeout) {
               this.restartWorker();
            }
            reject(new Error('Timeout'));
         }, settings.timeout);
         this.workerState.tasks.set(id, { resolve, reject, timeoutId });
         worker.postMessage(
            { id, imageBitmap: bitmap, width, height, type: file.type, quality: 0.9 },
            [bitmap]
         );
      });
   }
   resizeImage(file, maxWidth, maxHeight) {
      return new Promise((resolve) => {
         const img = new Image();
         img.onload = () => {
            URL.revokeObjectURL(img.src);
            // Calculate new dimensions keeping aspect ratio
            let { width, height } = img;
            if (width > maxWidth || height > maxHeight) {
               const ratio = Math.min(maxWidth / width, maxHeight / height);
               width = Math.round(width * ratio);
               height = Math.round(height * ratio);
            }
            // Draw to canvas at new size
            const canvas = document.createElement('canvas');
            canvas.width = width;
            canvas.height = height;
            canvas.getContext('2d').drawImage(img, 0, 0, width, height);
            // Export as blob for upload
            canvas.toBlob(resolve, file.type, 0.9);
         };
         img.src = URL.createObjectURL(file);
      });
   }
   async processFiles(fieldId, files) {
      let field = this.fields.get(fieldId);
      if (!field) return;
      if (field.groupUI.container) {
         field.groupUI.container.hidden = false;
      }
      const totalFiles = files.length;
      let processed = 0;
      this.updateFieldProgress(fieldId, 0, totalFiles, 'Processing files...');
      // Create upload records for all files first
      const uploadEntries = await Promise.all(
         files.map(async (file) => {
            const uploadId = window.generateID('upload');
            const upload = await this.setUpload(uploadId, {
               id: uploadId,
               field: fieldId,
               status: 'local_processing',
               blob: null,
               fields: {
                  originalName: file.name,
                  originalSize: file.size,
                  type: file.type,
                  lastModified: file.lastModified
               }
            });
            const element = await this.createUpload(uploadId, file, fieldId);
            this.uploads.set(uploadId, {
               element: element,
               ui: window.uiFromSelectors(this.selectors.items, element)
            });
            await this.addToGroup(uploadId, null);
            return { uploadId, upload, file };
         })
      );
      // Batch process images with concurrency control
      const imageEntries = uploadEntries.filter(e => e.file.type.startsWith('image/'));
      const otherEntries = uploadEntries.filter(e => !e.file.type.startsWith('image/'));
      // Process images in batches
      const processedBlobs = await this.processImages(
         imageEntries.map(e => e.file)
      );
      // Update image uploads with processed blobs
      for (let i = 0; i < imageEntries.length; i++) {
         const { uploadId, upload } = imageEntries[i];
         upload.blob = processedBlobs[i];
         upload.fields.size = processedBlobs[i].size;
         upload.status = 'queued';
         await this.setUpload(uploadId, upload);
         processed++;
         this.updateFieldProgress(fieldId, processed, totalFiles, 'Processing files...');
      }
      // Handle non-image files (no processing needed)
      for (const { uploadId, upload, file } of otherEntries) {
         upload.blob = file;
         upload.status = 'queued';
         await this.setUpload(uploadId, upload);
         processed++;
         this.updateFieldProgress(fieldId, processed, totalFiles, 'Processing files...');
      }
      this.maybeLockUploads(fieldId);
      if (field.config.autoUpload && field.config.destination !== 'post_group') {
         await this.queueUploads('uploads', fieldId);
      }
   }
   /*************************************************************
    RECOVERY
   *************************************************************/
   async checkRecovery() {
      const pendingUploads = this.stores.uploads.filterByIndex({status: ['local_processing', 'queued', 'uploading']});
      if (pendingUploads.length === 0) return;
      let notification = window.getTemplate('restoreNotification');
      if (!notification) {
         this.error.log(
            'No restore notification',
            {
               component: 'UploadManager',
               src: window.location.href
            }
         );
         return;
      }
      // Group by source page
      const bySource = new Map();
      pendingUploads.forEach(upload => {
         const src = upload.src || 'unknown';
         if (!bySource.has(src)) bySource.set(src, []);
         bySource.get(src).push(upload);
      });
      const currentSrc = window.location.href;
      let source = bySource.size > 1 ? ` across ${bySource.size} pages` : '';
      let upload = pendingUploads.length > 1 ? 'uploads' : 'upload';
      let message = `${pendingUploads.length} ${upload} can be recovered${source}`;
      let details = notification.querySelector('.details');
      if (details) {
         details.textContent = message;
      }
      let i = 1;
      for (const [src, uploads] of bySource) {
         let template = window.getTemplate('restoreField');
         if (!template) continue;
         let fieldId = this.registerField(template,false, 'recovery_'+i);
         let field = this.fields.get(fieldId);
         i++;
         let isCurrent = src === currentSrc;
         let [
            h3,
            a,
            grid
         ] = [
            template.querySelector('h3'),
            template.querySelector('h3 a'),
            template.querySelector('.item-grid')
         ];
         template.open = isCurrent;
         if (!isCurrent) {
            [a.href, a.title,a.textContent] =
               [src, 'Navigate to Page and Restore', src];
         } else {
            a.remove();
            h3.textContent = 'From this page:';
         }
         let filteredGroupIds = [...new Set(uploads.map(upload => upload.group??'preview'))];
         for (let groupId of filteredGroupIds) {
            let group = (groupId === 'preview') ? true : this.stores.groups.get(groupId);
            if (!group) continue;
            let groupElement = await this.createGroupElement(groupId,field.id);
            let groupGrid = groupElement.querySelector('.item-grid');
            let theseUploads = uploads.filter(upload => upload.group === (groupId === 'preview') ? null : groupId);
            for (const [key, value] of Object.entries(group.fields ?? {})) {
               let field = groupElement.querySelector(`input[name*="${key}"]`);
               if (field) field.value = value;
            }
            for (let upload of theseUploads) {
               let item = await this.createUpload(upload.id, this.formatFile(upload), field.id);
               groupGrid.append(item);
            }
            grid.append(groupElement);
         }
         notification.querySelector('.wrap').append(template);
      }
      document.body.append(notification);
      notification = document.querySelector('dialog.restore-uploads');
      this.restoreModal = new window.jvbModal(notification);
      this.restoreSelection = new window.jvbHandleSelection({
         container: notification,
         wrapper: '.restore-uploads .wrap',
         bulkControls: '.selection-actions',
         selectAll: '#select-all-restore',
         count: '.selection-count'
      });
      this.restoreModal.handleOpen();
   }
   async handleRestoreSelected() {
      if (!this.restoreSelection) return;
      let selected = Array.from(this.restoreSelection.selectedItems);
      if (selected.length === 0) {
         return;
      }
      await this.restoreSelectedUploads(selected);
   }
   async handleRestoreAll() {
      if (!this.restoreModal) return;
      const allUploads = Array.from(this.restoreModal.modal.querySelectorAll('.item.upload')).map(item => item.dataset.uploadId);
      await this.restoreSelectedUploads(allUploads);
   }
   async restoreSelectedUploads(selectedUploads) {
      let currentPage = window.location.href;
      let uploads = Array.from(this.stores.uploads.data.values()).filter(
         upload => selectedUploads.includes(upload.id) && upload.src === currentPage
      );
      let groups = [... new Set(uploads.map(upload => upload.group))].filter(Boolean);
      let fieldId = uploads[0].field;
      let field = document.querySelector(`[data-uploader="${fieldId}"]`);
      if (!field) {
         console.log('No field found for '+fieldId);
         return;
      }
      let fieldData = this.fields.get(fieldId);
      if (fieldData.groupUI.container) {
         fieldData.groupUI.container.hidden = false;
      }
      let usedIds = [];
      for (let gr of groups) {
         let group = this.stores.groups.get(gr);
          await this.createGroup(fieldId,gr);
          let element = this.groups.get(gr);
         let theseUploads = uploads.filter(upload => upload.group === gr);
         if (group && this.groups.has(gr)) {
            let fields = group.fields;
            for (const [key, value] of Object.entries(fields)) {
               let fi = element.element.querySelector(`input[name*="${key}"]`);
               if (fi) {
                  fi.value = value;
               }
            }
         }else {
            //Couldn't restore the group for some reason, just add it to the main preview grid instead
            gr = null;
         }
         for (let upload of theseUploads) {
            let item = await this.createUpload(upload.id, this.formatFile(upload), fieldId);
            this.uploads.set(upload.id, {
               element: item,
               ui: window.uiFromSelectors(this.selectors.items, item)
            });
            await this.addToGroup(upload.id, gr);
            usedIds.push(upload.id);
         }
      }
      let remaining = uploads.filter(upload => !usedIds.includes(upload.id));
      for (let upload of remaining) {
         let item = await this.createUpload(upload.id, this.formatFile(upload), fieldId);
         this.uploads.set(upload.id, {
            element: item,
            ui: window.uiFromSelectors(this.selectors.items, item)
         });
         await this.addToGroup(upload.id, null);
      }
      this.cleanupRestore();
   }
   cleanupRestore() {
      this.restoreModal.handleClose();
      this.restoreSelection.destroy();
      this.restoreSelection = null;
      this.restoreModal.destroy();
      this.restoreModal.modal.remove();
      this.restoreModal = null;
   }
   /*******************************************************************************
    STATUS MANAGEMENT
   *******************************************************************************/
   getStatusText(status) {
      let map = {
         'received': 'Image Received',
         'local_processing': 'Processing Image...',
         'queued': 'Waiting to upload...',
@@ -110,2345 +1080,10 @@
         'failed_permanent': 'Upload failed permanently'
      };
      // Sortable configuration
      this.sortableConfig = {
         animation: 150,
         draggable: '.item',
         handle: '.select-item, img',
         ghostClass: 'sortable-ghost',
         chosenClass: 'sortable-chosen',
         dragClass: 'sortable-drag',
         onEnd: (evt) => this.handleReorder(evt)
      };
      this.init();
   }
   async init() {
      this.initializeFields();
      this.initListeners();
      // Queue integration - handle completion/failure
      this.queue.subscribe((event, operation) => {
         if (!['uploads', 'uploads/meta', 'uploads/groups'].includes(operation.endpoint)) {
            return;
         }
         const fieldId = operation.data instanceof FormData
            ? operation.data.get('fieldId')
            : operation.data?.fieldId;
         switch(event) {
            case 'cancel-operation':
               if (fieldId) this.handleOperationCancelled(fieldId);
               break;
            case 'operation-status':
               if (fieldId) this.updateFieldStatus(fieldId, operation.status);
               break;
            case 'operation-complete':
               this.handleOperationComplete(operation, fieldId);
               break;
            case 'operation-failed':
            case 'operation-failed-permanent':
               this.handleOperationFailed(operation, fieldId);
               break;
         }
      });
      window.addEventListener('beforeunload', () => {
         this.cleanupAllPreviewUrls();
      });
   }
   initWorker() {
      this.worker = {
         worker: null,
         timeout: null,
         tasks: new Map(),
         restart: { count: 0, max: 3 },
         settings: {
            timeout: 10000,
            batchSize: 1,
            maxConcurrent: 3,
            restartAfterTimeout: true
         }
      };
   }
   /*******************************************************************************
    * FIELD MANAGEMENT
    *******************************************************************************/
   initializeFields() {
      const fields = document.querySelectorAll(this.selectors.field.field);
      fields.forEach(uploader => this.registerUploader(uploader));
   }
   scanFields(container) {
      const fields = container.querySelectorAll(this.selectors.field.field);
      fields.forEach(uploader => this.registerUploader(uploader));
   }
   registerUploader(uploader) {
      const fieldId = this.determineFieldId(uploader);
      const config = this.extractFieldConfig(uploader);
      const ui = this.buildFieldUI(uploader);
      // Store field data with Sets for runtime
      const fieldData = {
         id: fieldId,
         config: config,
         uploads: new Set(),
         groups: [],
         state: 'ready',
         timestamp: Date.now()
      };
      // Save to store (will convert Sets to Arrays automatically)
      this.fieldStore.save(fieldData);
      // Store DOM references separately
      this.fieldElements.set(fieldId, { element: uploader, ui, config });
      uploader.dataset.uploader = fieldId;
      this.addFieldSelectionHandler(fieldId);
      if (config.destination === 'post_group' && !this.dragController) {
         this.initGroupFeatures();
      }
      if (config.type !== 'single') {
         this.initSortable(fieldId);
      }
      return fieldId;
   }
   extractFieldConfig(fieldElement) {
      return {
         destination: fieldElement.dataset.destination || 'meta',
         content: fieldElement.dataset.content || null,
         mode: fieldElement.dataset.mode || 'direct',
         type: fieldElement.dataset.type || 'single',
         name: fieldElement.dataset.field,
         itemID: fieldElement.dataset.itemId || 0,
         maxFiles: parseInt(fieldElement.dataset.maxFiles) || 999,
         subtype: fieldElement.dataset.subtype || 'image'
      };
   }
   buildFieldUI(fieldElement) {
      let UI = {
         field: fieldElement,
         input: fieldElement.querySelector(this.selectors.field.input),
         dropZone: fieldElement.querySelector(this.selectors.field.dropZone),
         preview: fieldElement.querySelector(this.selectors.field.preview),
         progress: {
            progress: fieldElement.querySelector(this.selectors.field.progress),
            bar: fieldElement.querySelector('.bar'),
            fill: fieldElement.querySelector('.fill'),
            details: fieldElement.querySelector('.details'),
            text: fieldElement.querySelector('.details .text'),
            count: fieldElement.querySelector('.details .count')
         }
      };
      let display = fieldElement.querySelector('.group-display');
      if (display) {
         UI.groups = {
            display: display,
            container: fieldElement.querySelector('.item-grid.groups'),
            empty: fieldElement.querySelector('.empty-group'),
            groups: new Map()
         };
      }
      return UI;
   }
   /*******************************************************************************
    * SORTABLE INITIALIZATION
    *******************************************************************************/
   initSortable(fieldId) {
      if (!window.Sortable) return;
      const fieldEl = this.fieldElements.get(fieldId);
      if (!fieldEl) return;
      // Find all sortable grids (preview + group grids, but not restore grids)
      const grids = fieldEl.element.querySelectorAll('.item-grid.preview, .item-grid.group');
      grids.forEach((grid) => {
         // Skip if already initialized
         if (grid.sortableInstance) return;
         const isGroupGrid = grid.classList.contains('group');
         const gridId = isGroupGrid
            ? `${fieldId}-group-${grid.closest('.upload-group')?.dataset.groupId}`
            : `${fieldId}-preview`;
         const sortableInstance = new Sortable(grid, {
            ...this.sortableConfig,
            group: {
               name: fieldId,
               pull: true,
               put: true
            },
            onAdd: (evt) => {
               // Re-enable when items added
               this.updateSortableState(evt.to);
            },
            onRemove: (evt) => {
               // Disable source if now empty
               this.updateSortableState(evt.from);
            }
         });
         // Store reference on element for easy access
         grid.sortableInstance = sortableInstance;
         this.sortableInstances.set(gridId, sortableInstance);
      });
   }
   /**
    * Update sortable enabled/disabled state based on item count
    */
   updateSortableState(grid) {
      const sortable = grid?.sortableInstance;
      if (!sortable) return;
      const hasItems = grid.querySelectorAll('.item').length > 0;
      sortable.option('disabled', !hasItems);
   }
   /**
    * Refresh sortable for a field (call after adding/removing items dynamically)
    */
   refreshSortable(fieldId) {
      const fieldEl = this.fieldElements.get(fieldId);
      if (!fieldEl) return;
      const grids = fieldEl.element.querySelectorAll('.item-grid.preview, .item-grid.group');
      grids.forEach(grid => this.updateSortableState(grid));
   }
   handleReorder(evt) {
      const grid = evt.to;
      const fieldWrapper = grid.closest('.field, .upload');
      if (!fieldWrapper) return;
      let hiddenInput = fieldWrapper.querySelector('input[type="hidden"]');
      let items = Array.from(fieldWrapper.querySelectorAll('.item')).map(upload => upload.dataset.id);
      console.log(items);
      hiddenInput.value = items.join(',');
      if (window.jvbA11y) {
         window.jvbA11y.announce('Item reordered');
      }
      fieldWrapper.dispatchEvent(new CustomEvent('jvb-items-reordered', {
         detail: { from: evt.from, to: evt.to, oldIndex: evt.oldIndex, newIndex: evt.newIndex },
         bubbles: true
      }));
   }
   /*******************************************************************************
    * DRAG & DROP INITIALIZATION
    *******************************************************************************/
   initGroupFeatures() {
      this.dragController = new window.jvbDragHandler({
         draggableSelector: this.selectors.items.item,
         dropTargetSelector: `${this.selectors.field.preview}, ${this.selectors.groups.grid}, .empty-group`,
         ignoreSelector: 'input:not(.upload-select), button, select, textarea, details, summary, a',
         previewElement: 'img, video, .icon',
         getItemId: (element) => element.dataset.uploadId,
         getSelectedItems: (element) => {
            const fieldId = this.getFieldIdFromElement(element);
            const uploadId = element.dataset.uploadId;
            const selected = this.getCurrentSelection(fieldId);
            return (selected && selected.includes(uploadId)) ? selected : [uploadId];
         },
         validateDrop: (itemIds, targetElement) => {
            const targetFieldId = this.getFieldIdFromElement(targetElement);
            const itemElement = document.querySelector(`[data-upload-id="${itemIds[0]}"]`);
            const itemFieldId = this.getFieldIdFromElement(itemElement);
            return targetFieldId === itemFieldId;
         },
         onDrop: (itemIds, targetElement) => {
            this.handleItemDrop(itemIds, targetElement);
            targetElement.scrollIntoView({behavior:'smooth', block:'center'});
         },
         onDragEnd: (itemIds, success) => {
            if (success) {
               const itemElement = document.querySelector(`[data-upload-id="${itemIds[0]}"]`);
               const fieldId = this.getFieldIdFromElement(itemElement);
               const handler = this.selectionHandlers.get(fieldId);
               handler?.clearSelection();
            }
         },
         previewOptions: {
            multiOffset: { x: -60, y: -80 },
            singleOffset: { x: -50, y: -60 },
            showCount: true
         }
      });
   }
   /*******************************************************************************
    * FILE DROP HANDLERS
    *******************************************************************************/
   initListeners() {
      this.clickHandler = this.handleClick.bind(this);
      this.changeHandler = this.handleChange.bind(this);
      document.addEventListener('click', this.clickHandler);
      document.addEventListener('change', this.changeHandler);
      this.dragEnterHandler = this.handleExternalDragEnter.bind(this);
      this.dragLeaveHandler = this.handleExternalDragLeave.bind(this);
      this.dragOverHandler = this.handleExternalDragOver.bind(this);
      this.dropHandler = this.handleExternalDrop.bind(this);
      document.addEventListener('dragenter', this.dragEnterHandler);
      document.addEventListener('dragleave', this.dragLeaveHandler);
      document.addEventListener('dragover', this.dragOverHandler);
      document.addEventListener('drop', this.dropHandler);
   }
   handleExternalDragLeave(e) {
      const dropZone = e.target.closest(this.selectors.field.dropZone);
      if (dropZone && !dropZone.contains(e.relatedTarget)) {
         dropZone.classList.remove('dragover');
      }
   }
   handleExternalDragEnter(e) {
      if (!e.dataTransfer.types.includes('Files')) return;
      const dropZone = e.target.closest(this.selectors.field.dropZone);
      if (dropZone) {
         e.preventDefault();
         dropZone.classList.add('dragover');
      }
   }
   handleExternalDragOver(e) {
      if (!e.dataTransfer.types.includes('Files')) return;
      const dropZone = e.target.closest(this.selectors.field.dropZone);
      if (dropZone) {
         e.preventDefault();
         e.dataTransfer.dropEffect = 'copy';
      }
   }
   handleExternalDrop(e) {
      const dropZone = e.target.closest(this.selectors.field.dropZone);
      if (!dropZone) return;
      e.preventDefault();
      dropZone.classList.remove('dragover');
      const files = Array.from(e.dataTransfer.files);
      if (files.length === 0) return;
      const fieldId = this.getFieldIdFromElement(dropZone);
      if (fieldId) {
         this.processFiles(fieldId, files);
         this.a11y.announce(`${files.length} file(s) dropped for upload`);
      }
   }
   /*******************************************************************************
    * ITEM DROP HANDLER (for rearranging)
    *******************************************************************************/
   handleItemDrop(itemIds, targetElement) {
      const isPreviewDrop = targetElement.classList.contains('preview');
      let actualTarget = targetElement;
      // Handle drop on empty group placeholder
      if (targetElement.classList.contains('empty-group')) {
         const fieldId = this.getFieldIdFromElement(targetElement);
         const group = this.createGroup(fieldId);
         if (!group) return;
         actualTarget = group.grid;
      }
      // Move each item
      itemIds.forEach(uploadId => {
         if (isPreviewDrop) {
            this.removeFromGroup(uploadId);
         } else {
            this.addToGroup(uploadId, actualTarget);
         }
      });
      const fieldId = this.getFieldIdFromElement(targetElement);
      this.schedulePersistance(fieldId);
      const message = itemIds.length > 1 ? `Moved ${itemIds.length} items` : 'Moved item';
      this.a11y.announce(message);
   }
   /*******************************************************************************
    * CLICK & CHANGE HANDLERS
    *******************************************************************************/
   handleClick(e) {
      // Trigger file input
      if (e.target.matches(this.selectors.field.dropZone) ||
         e.target.closest(this.selectors.field.dropZone)) {
         const dropZone = e.target.closest(this.selectors.field.dropZone);
         if (dropZone && !e.target.matches('input, button, a')) {
            const input = dropZone.querySelector(this.selectors.field.input);
            input?.click();
         }
      }
      // Group actions
      const actionButton = e.target.closest('[data-action]');
      if (actionButton) {
         this.handleAction(actionButton);
      }
   }
   handleChange(e) {
      const fieldId = this.getFieldIdFromElement(e.target);
      // File input change
      if (e.target.matches(this.selectors.field.input)) {
         const files = Array.from(e.target.files);
         if (files.length > 0 && fieldId) {
            this.processFiles(fieldId, files);
         }
      }
      // Meta field changes
      if (fieldId) {
         const fieldData = this.getFieldData(fieldId);
         if (fieldData?.config.destination === 'post_group') {
            this.handleGroupMetaChange(e.target);
         } else {
            this.queueUploadMeta(e);
         }
      }
   }
   /*******************************************************************************
    * FILE PROCESSING
    *******************************************************************************/
   async processFiles(fieldId, files) {
      const fieldData = this.getFieldData(fieldId);
      const fieldEl = this.fieldElements.get(fieldId);
      if (!fieldData || !fieldEl) return;
      // Show group display, hide upload zone
      if (fieldEl.ui.dropZone) {
         fieldEl.ui.dropZone.hidden = true;
      }
      if (fieldEl.ui.groups?.display) {
         fieldEl.ui.groups.display.hidden = false;
      }
      const totalFiles = files.length;
      let processedCount = 0;
      this.updateUploadProgress(fieldId, 0, totalFiles, 'Processing files...');
      const processPromises = Array.from(files).map(async (file) => {
         try {
            const uploadId = `upload_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
            // Create upload data (without blob initially)
            const uploadData = {
               id: uploadId,
               attachmentId: null,
               fieldId: fieldId,
               status: 'local_processing',
               groupId: null,
               meta: {
                  originalName: file.name,
                  size: file.size,
                  type: file.type
               }
            };
            // Process file
            const preview = this.createPreviewUrl(file);
            const processedFile = file.type.startsWith('image/')
               ? await this.processImage(file, fieldData.config.subtype)
               : file;
            // Store blob data as ArrayBuffer
            await this.saveBlobData(uploadId, processedFile || file);
            // Create DOM element
            const subtype = this.getSubtypeFromMime(file.type);
            const element = this.createUploadElement({
               id: uploadId,
               preview: preview,
               meta: uploadData.meta,
               subtype: subtype
            }, fieldData.config.destination === 'post_group');
            // Show progress
            this.showUploadProgress(uploadId, true);
            this.updateUploadItemProgress(uploadId, 50, 'local_processing');
            // Add to preview grid
            if (fieldEl.ui.preview) {
               fieldEl.ui.preview.appendChild(element);
               // Store runtime element data
               this.uploadElements.set(uploadId, {
                  element: element,
                  preview: preview,
                  location: fieldEl.ui.preview
               });
            }
            // Store persistent data
            uploadData.status = 'processed';
            await this.uploadStore.save(uploadData);
            // Add to field
            fieldData.uploads.add(uploadId);
            await this.saveFieldData(fieldData);
            // Update progress
            processedCount++;
            this.updateUploadProgress(fieldId, processedCount, totalFiles, 'Processing files...');
            this.updateUploadItemProgress(uploadId, 100, 'processed');
            // Fade out progress
            setTimeout(() => this.showUploadProgress(uploadId, false), 1000);
            return uploadId;
         } catch (error) {
            console.error('Error processing file:', file.name, error);
            processedCount++;
            this.updateUploadProgress(fieldId, processedCount, totalFiles, 'Processing files...');
            return null;
         }
      });
      await Promise.all(processPromises);
      this.updateFieldState(fieldId);
      this.refreshSortable(fieldId);
      // Queue for upload if in direct mode
      if (fieldData.config.destination !== 'post_group') {
         await this.queueUpload(fieldId);
         this.maybeLockUploads(fieldId);
      }
   }
   /*******************************************************************************
    * IMAGE PROCESSING (simplified - keeping existing logic)
    *******************************************************************************/
   async processImage(file, uploadId) {
      const timeout = this.worker.settings.timeout;
      return new Promise((resolve, reject) => {
         let timeoutId;
         let taskCompleted = false;
         timeoutId = setTimeout(() => {
            if (!taskCompleted) {
               taskCompleted = true;
               this.worker.tasks.delete(uploadId);
               if (this.worker.settings.restartAfterTimeout) {
                  this.restartCompressionWorker();
               }
               reject(new Error(`Processing timeout for ${file.name}`));
            }
         }, timeout);
         this.worker.tasks.set(uploadId, { file, timeoutId });
         this.handleProcess(file, uploadId)
            .then(result => {
               if (!taskCompleted) {
                  taskCompleted = true;
                  clearTimeout(timeoutId);
                  this.worker.tasks.delete(uploadId);
                  resolve(result);
               }
            })
            .catch(error => {
               if (!taskCompleted) {
                  taskCompleted = true;
                  clearTimeout(timeoutId);
                  this.worker.tasks.delete(uploadId);
                  reject(error);
               }
            });
      });
   }
   async handleProcess(file, uploadId) {
      if (!file.type.startsWith('image/')) {
         return file;
      }
      const maxDimension = this.getMaxDimension();
      const quality = 0.85;
      if (this.shouldUseWorker(file)) {
         try {
            if (!this.worker.worker) {
               this.initCompressionWorker();
            }
            if (this.worker.worker) {
               return await this.processWithWorker(file, uploadId, maxDimension, quality);
            }
         } catch (error) {
            console.warn('Worker processing failed, falling back to main thread:', error);
         }
      }
      return await this.processOnMainThread(file, maxDimension, quality);
   }
   async processOnMainThread(file, maxDimension, quality) {
      return new Promise((resolve, reject) => {
         const img = new Image();
         const canvas = document.createElement('canvas');
         const ctx = canvas.getContext('2d');
         let objectUrl = null;
         const cleanup = () => {
            img.onload = null;
            img.onerror = null;
            if (objectUrl) {
               URL.revokeObjectURL(objectUrl);
               objectUrl = null;
            }
            canvas.width = 1;
            canvas.height = 1;
            ctx.clearRect(0, 0, 1, 1);
         };
         img.onload = () => {
            try {
               const { width, height } = this.calculateOptimalDimensions(img, maxDimension);
               canvas.width = width;
               canvas.height = height;
               ctx.imageSmoothingEnabled = true;
               ctx.imageSmoothingQuality = 'high';
               ctx.drawImage(img, 0, 0, width, height);
               const outputFormat = this.getOptimalFormat(file);
               const outputQuality = this.getOptimalQuality(file, quality);
               canvas.toBlob(
                  (blob) => {
                     cleanup();
                     if (blob) {
                        const processedFile = new File(
                           [blob],
                           this.getProcessedFileName(file, outputFormat),
                           { type: outputFormat, lastModified: Date.now() }
                        );
                        resolve(processedFile);
                     } else {
                        reject(new Error('Canvas toBlob failed'));
                     }
                  },
                  outputFormat,
                  outputQuality
               );
            } catch (error) {
               cleanup();
               reject(new Error(`Canvas processing failed: ${error.message}`));
            }
         };
         img.onerror = () => {
            cleanup();
            reject(new Error(`Failed to load image: ${file.name}`));
         };
         try {
            objectUrl = this.createPreviewUrl(file);
            img.src = objectUrl;
         } catch (error) {
            cleanup();
            reject(new Error(`Failed to create object URL: ${error.message}`));
         }
      });
   }
   getOptimalFormat(file) {
      if (file.type === 'image/gif' || file.type === 'image/svg+xml') {
         return file.type;
      }
      return this.supportsWebP() ? 'image/webp' : 'image/jpeg';
   }
   getOptimalQuality(file, requestedQuality) {
      if (file.size < 500 * 1024) return Math.max(requestedQuality, 0.9);
      if (file.size < 2 * 1024 * 1024) return requestedQuality;
      return Math.min(requestedQuality, 0.8);
   }
   getProcessedFileName(originalFile, outputFormat) {
      const baseName = originalFile.name.replace(/\.[^/.]+$/, '');
      const extensions = {
         'image/webp': '.webp',
         'image/jpeg': '.jpg',
         'image/png': '.png',
         'image/gif': '.gif'
      };
      return baseName + (extensions[outputFormat] || '.jpg');
   }
   getMaxDimension() {
      const screenWidth = window.screen.width;
      const devicePixelRatio = window.devicePixelRatio || 1;
      if (screenWidth * devicePixelRatio > 2560) return 2400;
      if (screenWidth * devicePixelRatio > 1920) return 1920;
      return 1200;
   }
   shouldUseWorker(file) {
      return this.worker.worker &&
         file.size > 1024 * 1024 &&
         typeof OffscreenCanvas !== 'undefined';
   }
   async processWithWorker(file, uploadId, maxDimension, quality) {
      return new Promise((resolve, reject) => {
         if (!this.worker.worker) {
            reject(new Error('Worker not available'));
            return;
         }
         const messageId = `${uploadId}_${Date.now()}`;
         const messageHandler = (e) => {
            if (e.data.messageId !== messageId) return;
            this.worker.worker.removeEventListener('message', messageHandler);
            this.worker.worker.removeEventListener('error', errorHandler);
            if (e.data.success) {
               const processedFile = new File(
                  [e.data.blob],
                  this.getProcessedFileName(file, e.data.format || 'image/webp'),
                  { type: e.data.format || 'image/webp', lastModified: Date.now() }
               );
               resolve(processedFile);
            } else {
               reject(new Error(e.data.error || 'Worker processing failed'));
            }
         };
         const errorHandler = (error) => {
            this.worker.worker.removeEventListener('message', messageHandler);
            this.worker.worker.removeEventListener('error', errorHandler);
            reject(new Error(`Worker error: ${error.message}`));
         };
         this.worker.worker.addEventListener('message', messageHandler);
         this.worker.worker.addEventListener('error', errorHandler);
         this.worker.worker.postMessage({
            messageId,
            file,
            maxDimension,
            quality,
            outputFormat: this.getOptimalFormat(file)
         });
      });
   }
   restartCompressionWorker() {
      if (this.worker.worker) {
         this.worker.worker.terminate();
         this.worker.worker = null;
      }
      this.worker.tasks.clear();
      if (this.worker.restart.count >= this.worker.restart.max) {
         console.error('Max worker restarts reached, disabling worker');
         return;
      }
      this.worker.restart.count++;
      this.initCompressionWorker();
   }
   initCompressionWorker() {
      if (this.worker.worker || typeof Worker === 'undefined') return;
      try {
         const workerScript = `
            self.onmessage = async function(e) {
               const { messageId, file, maxDimension, quality, outputFormat } = e.data;
               try {
                  const bitmap = await createImageBitmap(file);
                  const scale = Math.min(maxDimension / bitmap.width, maxDimension / bitmap.height, 1);
                  const width = Math.round(bitmap.width * scale);
                  const height = Math.round(bitmap.height * scale);
                  const canvas = new OffscreenCanvas(width, height);
                  const ctx = canvas.getContext('2d');
                  ctx.imageSmoothingEnabled = true;
                  ctx.imageSmoothingQuality = 'high';
                  ctx.drawImage(bitmap, 0, 0, width, height);
                  bitmap.close();
                  const blob = await canvas.convertToBlob({ type: outputFormat, quality: quality });
                  self.postMessage({ messageId, success: true, blob: blob, format: outputFormat });
               } catch (error) {
                  self.postMessage({ messageId, success: false, error: error.message });
               }
            };
         `;
         const blob = new Blob([workerScript], { type: 'application/javascript' });
         this.worker.worker = new Worker(this.createPreviewUrl(blob));
      } catch (error) {
         console.warn('Failed to initialize compression worker:', error);
         this.worker.worker = null;
      }
   }
   calculateOptimalDimensions(img, maxDimension) {
      let { width, height } = img;
      if (width <= maxDimension && height <= maxDimension) {
         return { width, height };
      }
      const scale = Math.min(maxDimension / width, maxDimension / height);
      return {
         width: Math.round(width * scale),
         height: Math.round(height * scale)
      };
   }
   supportsWebP() {
      const canvas = document.createElement('canvas');
      return canvas.toDataURL('image/webp').indexOf('data:image/webp') === 0;
   }
   createPreviewUrl(file) {
      const url = URL.createObjectURL(file);
      if (!this.previewUrls) this.previewUrls = new Set();
      this.previewUrls.add(url);
      return url;
   }
   revokePreviewUrl(url) {
      if (url?.startsWith('blob:')) {
         URL.revokeObjectURL(url);
         this.previewUrls?.delete(url);
      }
   }
   /*******************************************************************************
    * QUEUE INTEGRATION
    *******************************************************************************/
   async submitUploads(fieldId) {
      const fieldData = this.getFieldData(fieldId);
      const fieldEl = this.fieldElements.get(fieldId);
      if (!fieldData?.uploads || fieldData.uploads.size === 0) {
         return;
      }
      let uploadIds = Array.from(fieldData.uploads);
      if (uploadIds.length === 0) {
         this.error.log('No uploads to upload', {
            component: 'UploadManager',
            action: 'submitGroupedUploads',
            fieldId: fieldId
         });
         return;
      }
      const fieldGroups = this.getFieldGroups(fieldId);
      if (fieldGroups.length === 0) {
         this.error.log('No groups created for post_group upload', {
            component: 'UploadManager',
            action: 'submitGroupedUploads',
            fieldId: fieldId
         });
         return;
      }
      // Build posts array from groups
      const posts = [];
      const formData = new FormData();
      let uploadMap = [];
      // Process each group
      for (const group of fieldGroups) {
         const post = {
            images: [],
            fields: {}
         };
         // Add group metadata
         for (let [name, value] of Object.entries(group.changes)) {
            post.fields[name] = value;
         }
         // Get uploads for this group
         const groupUploadIds = uploadIds.filter(uploadId => {
            const upload = this.uploadStore.get(uploadId);
            return upload?.groupId === group.id;
         });
         // Add files for this group
         for (const uploadId of groupUploadIds) {
            const file = await this.getBlobData(uploadId);
            if (file) {
               formData.append('files[]', file);
               const imageData = {
                  upload_id: uploadId,
                  index: uploadMap.length
               };
               // Check if featured
               const uploadEl = this.uploadElements.get(uploadId);
               const radioInput = uploadEl?.element?.querySelector('[name="featured"]');
               if (radioInput?.checked) {
                  post.fields.featured = uploadId;
               }
               post.images.push(imageData);
               uploadMap.push(uploadId);
            }
         }
         posts.push(post);
      }
      // Handle remaining uploads (without groupId) - each becomes its own post
      const remainingUploadIds = uploadIds.filter(uploadId => {
         const upload = this.uploadStore.get(uploadId);
         return !upload?.groupId;
      });
      for (const uploadId of remainingUploadIds) {
         const post = {
            images: [],
            fields: {}
         };
         const file = await this.getBlobData(uploadId);
         if (file) {
            formData.append('files[]', file);
            const imageData = {
               upload_id: uploadId,
               index: uploadMap.length
            };
            post.images.push(imageData);
            uploadMap.push(uploadId);
         }
         posts.push(post);
      }
      // Add metadata to FormData
      formData.append('content', fieldData.config.content);
      formData.append('user', fieldData.config.itemID);
      formData.append('posts', JSON.stringify(posts));
      formData.append('upload_ids', JSON.stringify(uploadMap));
      const operation = {
         endpoint: 'uploads/groups',
         method: 'POST',
         data: formData,
         title: `Creating ${posts.length} ${fieldData.config.content}${posts.length > 1 ? 's' : ''} from uploads...`,
         popup: `Creating ${posts.length} post${posts.length > 1 ? 's' : ''}...`,
         canMerge: false,
         headers: {
            'action_nonce': jvbSettings.dash
         },
         append: '_upload',
      };
      try {
         const operationId = await this.queue.addToQueue(operation);
         // Update upload statuses
         uploadIds.forEach(uploadId => {
            const upload = this.uploadStore.get(uploadId);
            if (upload) {
               upload.operationId = operationId;
               upload.status = 'queued';
               this.uploadStore.save(upload);
               this.updateUploadStatus(uploadId, 'queued');
            }
         });
         fieldData.operationId = operationId;
         await this.saveFieldData(fieldData);
         this.a11y.announce(`Creating ${posts.length} post${posts.length > 1 ? 's' : ''} from your uploads`);
         return operationId;
      } catch (error) {
         this.error.log(error, {
            component: 'UploadManager',
            action: 'submitGroupedUploads',
            fieldId: fieldId
         });
         throw error;
      }
   }
   async queueUpload(fieldId) {
      const fieldData = this.getFieldData(fieldId);
      if (!fieldData?.uploads || fieldData.uploads.size === 0) return;
      const uploads = Array.from(fieldData.uploads);
      const data = this.prepareUploadData(fieldData, uploads);
      this.a11y.announce('Queuing for upload');
      const operation = {
         endpoint: 'uploads',
         method: 'POST',
         data: data,
         title: `Uploading ${uploads.length} file${uploads.length > 1 ? 's' : ''} to server...`,
         popup: `Uploading ${uploads.length} file${uploads.length > 1 ? 's' : ''}...`,
         canMerge: false,
         headers: { 'action_nonce': jvbSettings.dash },
         append: '_upload'
      };
      try {
         const operationId = await this.queue.addToQueue(operation);
         // Update upload statuses
         uploads.forEach(uploadId => {
            const upload = this.uploadStore.get(uploadId);
            if (upload) {
               upload.operationId = operationId;
               upload.status = 'queued';
               this.uploadStore.save(upload);
               this.updateUploadStatus(uploadId, 'queued');
            }
         });
         fieldData.operationId = operationId;
         await this.saveFieldData(fieldData);
         return operationId;
      } catch (error) {
         throw error;
      }
   }
   async prepareUploadData(fieldData, uploads) {
      const formData = new FormData();
      formData.append('content', fieldData.config.content);
      formData.append('mode', fieldData.config.mode);
      formData.append('field_name', fieldData.config.name);
      formData.append('fieldId', fieldData.id);
      formData.append('field_type', fieldData.config.type);
      formData.append('subtype', fieldData.config.subtype);
      formData.append('item_id', fieldData.config.itemID);
      formData.append('destination', fieldData.config.destination || 'meta');
      let uploadMap = [];
      const blobPromises = uploads.map(async (uploadId) => {
         const upload = this.uploadStore.get(uploadId);
         if (!upload) return;
         const file = await this.getBlobData(uploadId);
         if (file) {
            formData.append('files[]', file);
            uploadMap.push(upload.id);
         }
      });
      await Promise.all(blobPromises);
      formData.append('upload_ids', JSON.stringify(uploadMap));
      return formData;
   }
   async queueUploadMeta(e) {
      const uploadId = this.getUploadIdFromElement(e.target);
      const upload = this.uploadStore.get(uploadId);
      if (!upload) return;
      const fieldData = this.getFieldData(upload.fieldId);
      if (!fieldData) return;
      let data = {};
      data[e.target.name] = e.target.value;
      upload.meta = { ...upload.meta, ...data };
      await this.uploadStore.save(upload);
      let queueData = {};
      queueData[upload.attachmentId ?? upload.id] = upload.meta;
      const operation = {
         endpoint: 'uploads/meta',
         method: 'POST',
         data: queueData,
         title: 'Updating meta',
         canMerge: true,
         headers: { 'action_nonce': jvbSettings.dash }
      };
      try {
         await this.queue.addToQueue(operation);
      } catch (error) {
         this.error.log(error, {
            component: 'UploadManager',
            action: 'sendMetaUpdate',
            uploadId: upload.id
         });
      }
   }
   /*******************************************************************************
    * QUEUE EVENT HANDLERS - CLEANUP AFTER SUCCESS
    *******************************************************************************/
   /**
    * Handle successful operation completion - CLEAR STORES
    */
   async handleOperationComplete(operation, fieldId) {
      const results = operation.result?.data || operation.serverData?.data || [];
      // Update upload statuses with attachment IDs
      results.forEach(result => {
         const upload = this.uploadStore.get(result.upload_id);
         if (upload) {
            upload.attachmentId = result.attachment_id;
            upload.status = 'completed';
            this.uploadStore.save(upload);
            this.updateUploadStatus(result.upload_id, 'completed');
         }
      });
      if (!fieldId) return;
      const fieldData = this.getFieldData(fieldId);
      if (!fieldData) return;
      // Clean up completed uploads from stores
      const completedUploads = Array.from(fieldData.uploads).filter(uploadId => {
         const upload = this.uploadStore.get(uploadId);
         return upload?.status === 'completed';
      });
      for (const uploadId of completedUploads) {
         await this.clearUpload(uploadId, false);
         fieldData.uploads.delete(uploadId);
      }
      // If all uploads complete, clear entire field from stores
      if (fieldData.uploads.size === 0) {
         await this.clearFieldFromStores(fieldId);
         this.a11y.announce('All uploads completed successfully');
      } else {
         // Otherwise just update field state
         await this.saveFieldData(fieldData);
      }
      this.updateFieldState(fieldId);
   }
   /**
    * Handle operation failure
    */
   handleOperationFailed(operation, fieldId) {
      const uploadIds = operation.data instanceof FormData
         ? JSON.parse(operation.data.get('upload_ids') || '[]')
         : operation.data.upload_ids || [];
      uploadIds.forEach(uploadId => {
         const upload = this.uploadStore.get(uploadId);
         if (upload) {
            upload.status = operation.status === 'operation-failed-permanent'
               ? 'failed_permanent'
               : 'failed';
            this.uploadStore.save(upload);
            this.updateUploadStatus(uploadId, upload.status);
         }
      });
      if (fieldId) {
         this.updateFieldState(fieldId);
      }
   }
   /**
    * Handle operation cancellation
    */
   async handleOperationCancelled(fieldId) {
      const fieldData = this.getFieldData(fieldId);  // ✅
      if (!fieldData) return;
      const uploadsArray = fieldData.uploads instanceof Set
         ? Array.from(fieldData.uploads)
         : fieldData.uploads;
      for (const uploadId of uploadsArray) {
         await this.clearUpload(uploadId, false);
      }
      await this.clearFieldFromStores(fieldId);
      this.updateFieldState(fieldId);
      this.a11y.announce('Upload cancelled');
   }
   getFieldGroups(fieldId) {
      const fieldData = this.getFieldData(fieldId);
      if (!fieldData?.groups) return [];
      return fieldData.groups.map(group => ({
         id: group.id,
         uploads: group.uploads || [],
         changes: group.changes || {}
      }));
   }
   getSelectedRestorationUploads(notificationEl) {
      let selected = [];
      const checkboxes = notificationEl.querySelectorAll('[type=checkbox]:checked');
      checkboxes.forEach(checkbox => {
         const item = checkbox.closest('.item');
         if (item) {
            selected.push({
               uploadId: item.dataset.uploadId,
               fieldId: item.dataset.fieldId
            });
         }
      });
      return selected;
   }
   async restoreSelectedUploads(selectedUploads) {
      const byField = new Map();
      selectedUploads.forEach(item => {
         if (!byField.has(item.fieldId)) {
            byField.set(item.fieldId, []);
         }
         byField.get(item.fieldId).push(item.uploadId);
      });
      for (const [fieldId, uploadIds] of byField.entries()) {
         const fieldState = this.fieldStore.get(fieldId);
         if (fieldState) {
            fieldState.uploads = uploadIds;
            await this.restoreField(fieldState);
         }
      }
   }
   async restoreField(fieldState) {
      const { config, context, uploads, groups, id } = fieldState;
      // If in a modal, open it first
      if (context?.modalType) {
         await this.openModalForRestore(context);
      }
      // Find field element
      let fieldElement = document.querySelector(`.field.upload[data-field="${config.name}"]`);
      if (!fieldElement) {
         const uploaderKey = `${config.content}_${config.itemID}_${config.name}`;
         fieldElement = document.querySelector(`.field.upload[data-uploader="${uploaderKey}"]`);
      }
      if (!fieldElement) {
         console.warn(`Field ${config.name} not found for restoration`, config);
         return;
      }
      // Register the field if not already registered
      let fieldKey = fieldElement.dataset.uploader;
      if (!fieldKey || !this.fieldElements.has(fieldKey)) {
         fieldKey = this.registerUploader(fieldElement);
      }
      const fieldEl = this.fieldElements.get(fieldKey);
      const fieldData = this.getFieldData(fieldKey);
      if (!fieldEl || !fieldData) {
         console.error('Failed to register field for restoration');
         return;
      }
      // Merge saved state back into field
      fieldData.state = fieldState.state || 'ready';
      // Rebuild UI references if needed
      if (!fieldEl.ui) {
         fieldEl.ui = this.buildFieldUI(fieldElement);
      }
      if (fieldEl.ui.groups?.display) {
         fieldEl.ui.groups.display.hidden = false;
      }
      if (fieldEl.ui.dropZone) {
         fieldEl.ui.dropZone.hidden = true;
      }
      // Restore groups first
      if (groups && groups.length > 0) {
         await this.restoreGroups(fieldKey, groups);
      }
      // Handle both Array and Set for uploads
      const uploadsArray = uploads instanceof Set
         ? Array.from(uploads)
         : Array.isArray(uploads)
            ? uploads
            : [];
      // Restore uploads
      for (const uploadId of uploadsArray) {
         // Get upload data from store
         const uploadData = this.uploadStore.get(uploadId);
         if (uploadData) {
            await this.restoreUpload(fieldKey, uploadData);
         }
      }
      // Update field state
      await this.saveFieldData(fieldData);
      this.updateFieldState(fieldKey);
      this.maybeLockUploads(fieldKey);
      this.refreshSortable(fieldKey);
      // Queue for upload if needed
      if (config.mode === 'direct' && config.destination !== 'post_group') {
         await this.queueUpload(fieldKey);
      }
   }
   async restoreUpload(fieldId, uploadData) {
      const fieldEl = this.fieldElements.get(fieldId);
      const fieldData = this.getFieldData(fieldId);
      if (!fieldEl || !fieldData) {
         console.error('Field not found for upload restoration:', fieldId);
         return;
      }
      // Get reconstructed File from blob data
      const file = await this.getBlobData(uploadData.id);
      if (!file) {
         console.warn('Blob data not found for upload:', uploadData.id);
         return;
      }
      // Create preview URL
      const previewUrl = this.createPreviewUrl(file);
      // Recreate DOM element
      const subtype = this.getSubtypeFromMime(file.type);
      const element = this.createUploadElement({
         id: uploadData.id,
         preview: previewUrl,
         meta: uploadData.meta || {
            originalName: file.name,
            size: file.size,
            type: file.type
         },
         subtype: subtype
      }, fieldData.config.destination === 'post_group');
      // Determine correct location
      let location;
      if (uploadData.groupId) {
         // Check if group exists
         const groupEl = this.groupElements.get(uploadData.groupId);
         if (groupEl?.grid) {
            location = groupEl.grid;
            // Add to group's upload list
            const group = fieldData.groups?.find(g => g.id === uploadData.groupId);
            if (group) {
               if (!group.uploads) group.uploads = [];
               if (!group.uploads.includes(uploadData.id)) {
                  group.uploads.push(uploadData.id);
               }
            }
         } else {
            // Group doesn't exist, add to preview
            location = fieldEl.ui.preview;
            uploadData.groupId = null;
         }
      } else {
         // No group, add to preview
         location = fieldEl.ui.preview;
      }
      // Add element to DOM
      if (location) {
         location.appendChild(element);
      } else if (fieldEl.ui.preview) {
         fieldEl.ui.preview.appendChild(element);
         location = fieldEl.ui.preview;
      }
      // Store runtime element data
      this.uploadElements.set(uploadData.id, {
         element: element,
         preview: previewUrl,
         location: location
      });
      // Add to field uploads
      if (!fieldData.uploads) fieldData.uploads = new Set();
      fieldData.uploads.add(uploadData.id);
      // Update upload data in store
      uploadData.status = 'processed';
      await this.uploadStore.save(uploadData);
      // Update sortable state for the grid
      if (location) {
         this.updateSortableState(location);
      }
   }
   async restoreGroups(fieldId, groups) {
      const fieldEl = this.fieldElements.get(fieldId);
      const fieldData = this.getFieldData(fieldId);
      if (!fieldEl || !fieldData) {
         console.error('Field not found for group restoration:', fieldId);
         return;
      }
      for (const groupData of groups) {
         // Create group using existing method which handles all initialization
         const group = this.createGroup(fieldId, groupData.id);
         if (!group) {
            console.warn('Failed to create group:', groupData.id);
            continue;
         }
         // Find the group in fieldData (createGroup already added it)
         const storedGroup = fieldData.groups?.find(g => g.id === groupData.id);
         if (storedGroup) {
            // Restore metadata
            if (groupData.changes) {
               storedGroup.changes = { ...groupData.changes };
            }
            if (groupData.uploads) {
               storedGroup.uploads = [...groupData.uploads];
            }
            // Restore form field values if they exist
            if (groupData.changes) {
               const titleInput = group.element.querySelector('[name*="post_title"]');
               const excerptInput = group.element.querySelector('[name*="post_excerpt"]');
               if (titleInput && groupData.changes.post_title) {
                  titleInput.value = groupData.changes.post_title;
               }
               if (excerptInput && groupData.changes.post_excerpt) {
                  excerptInput.value = groupData.changes.post_excerpt;
               }
            }
         }
      }
      // Save updated field data
      await this.saveFieldData(fieldData);
   }
   async openModalForRestore(context) {
      if (!context) return;
      const { modalType, itemId } = context;
      // Find and click the appropriate button to open the modal
      let trigger = null;
      switch(modalType) {
         case 'create':
            trigger = document.querySelector('[data-action="create"]');
            break;
         case 'edit':
            // Need to find the specific edit button
            if (itemId) {
               trigger = document.querySelector(`[data-action="edit"][data-id="${itemId}"]`);
            }
            break;
         case 'bulkEdit':
            trigger = document.querySelector('[data-action="bulk-edit"]');
            break;
      }
      if (trigger) {
         trigger.click();
         // Wait for modal to open and render
         await new Promise(resolve => setTimeout(resolve, 300));
      } else {
         console.warn('Modal trigger not found for restoration:', context);
      }
   }
   formatBytes(bytes, decimals = 2) {
      if (bytes === 0) return '0 Bytes';
      const k = 1024;
      const dm = decimals < 0 ? 0 : decimals;
      const sizes = ['Bytes', 'KB', 'MB', 'GB'];
      const i = Math.floor(Math.log(bytes) / Math.log(k));
      return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
   }
   /*******************************************************************************
    * CLEANUP METHODS - AGGRESSIVE CLEANUP AFTER SUCCESS
    *******************************************************************************/
   /**
    * Clear individual upload from stores (called after successful upload)
    */
   async clearUpload(uploadId, persist = true) {
      const uploadEl = this.uploadElements.get(uploadId);
      if (uploadEl) {
         this.revokePreviewUrl(uploadEl.preview);
         if (uploadEl.element) {
            const previewUrl = uploadEl.element.dataset.previewUrl;
            this.revokePreviewUrl(previewUrl);
            delete uploadEl.element.dataset.previewUrl;
         }
      }
      // Remove from runtime memory
      this.uploadElements.delete(uploadId);
      // Remove from store (no separate blob store - it's part of the upload object)
      await this.uploadStore.delete(uploadId);
      // Update field if needed
      if (persist) {
         const upload = this.uploadStore.get(uploadId);
         if (upload?.fieldId) {
            await this.schedulePersistance(upload.fieldId);
         }
      }
   }
   /**
    * Clear entire field from stores (called when all uploads complete)
    */
   async clearFieldFromStores(fieldId) {
      const fieldData = this.getFieldData(fieldId);
      // Clear all related uploads
      if (fieldData?.uploads) {
         const uploadsArray = fieldData.uploads instanceof Set
            ? Array.from(fieldData.uploads)
            : fieldData.uploads;
         for (const uploadId of uploadsArray) {
            await this.uploadStore.delete(uploadId);
         }
      }
      // Clear field from store
      await this.fieldStore.delete(fieldId);
      // Keep runtime references (fieldElements, etc) intact for reuse
   }
   cleanupAllPreviewUrls() {
      if (this.previewUrls) {
         this.previewUrls.forEach(url => {
            try {
               URL.revokeObjectURL(url);
            } catch (e) {
               // Ignore errors during cleanup
            }
         });
         this.previewUrls.clear();
      }
   }
   /*******************************************************************************
    * UI UPDATE METHODS
    *******************************************************************************/
   updateFieldState(fieldId) {
      const fieldEl = this.fieldElements.get(fieldId);
      const fieldData = this.getFieldData(fieldId);
      if (!fieldEl || !fieldData) return;
      const container = fieldEl.element;
      const uploadCount = fieldData.uploads?.size || 0;
      const hasGroups = fieldEl.ui.groups?.container?.querySelectorAll('.upload-group').length > 0;
      container.dataset.hasUploads = uploadCount > 0 ? 'true' : 'false';
      container.dataset.uploadCount = uploadCount.toString();
      container.dataset.hasGroups = hasGroups ? 'true' : 'false';
      if (fieldEl.ui.preview) {
         fieldEl.ui.preview.setAttribute('aria-label',
            `Upload preview area with ${uploadCount} item${uploadCount !== 1 ? 's' : ''}`
         );
      }
   }
   updateUploadProgress(fieldId, current, total, message) {
      const fieldEl = this.fieldElements.get(fieldId);
      if (!fieldEl?.ui?.progress?.progress) return;
      const progress = fieldEl.ui.progress;
      const percent = total > 0 ? (current / total) * 100 : 0;
      if (progress.fill) progress.fill.style.width = `${percent}%`;
      if (progress.text) progress.text.textContent = message;
      if (progress.count) progress.count.textContent = `${current}/${total}`;
      progress.progress.hidden = (current === total);
   }
   updateFieldStatus(fieldId, status) {
      const fieldData = this.getFieldData(fieldId);
      if (!fieldData) return;
      fieldData.state = status;
      this.saveFieldData(fieldData);
   }
   updateUploadStatus(uploadId, status) {
      const upload = this.uploadStore.get(uploadId);
      if (!upload) return;
      upload.status = status;
      this.uploadStore.save(upload);
      this.updateUploadUI(uploadId);
   }
   updateUploadUI(uploadId) {
      const uploadEl = this.uploadElements.get(uploadId);
      const upload = this.uploadStore.get(uploadId);
      if (!upload || !uploadEl?.element) return;
      uploadEl.element.className = uploadEl.element.className.replace(/status-[\w-]+/g, '');
      uploadEl.element.classList.add(`status-${upload.status}`);
      const progress = uploadEl.element.querySelector('.progress');
      if (progress) {
         this.updateUploadItemProgress(uploadId,
            this.getStatusProgress(upload.status),
            upload.status
         );
      }
   }
   showUploadProgress(uploadId, show = true) {
      const uploadEl = this.uploadElements.get(uploadId);
      if (!uploadEl?.element) return;
      const progressEl = uploadEl.element.querySelector('.progress');
      if (progressEl) {
         if (show) {
            progressEl.style.removeProperty('animation');
            progressEl.hidden = false;
         } else {
            progressEl.style.animation = 'fadeOut var(--transition-base)';
            setTimeout(() => { progressEl.hidden = true; }, 300);
         }
      }
   }
   updateUploadItemProgress(uploadId, percent, status = null) {
      const uploadEl = this.uploadElements.get(uploadId);
      if (!uploadEl?.element) return;
      const progressEl = uploadEl.element.querySelector('.progress');
      if (!progressEl) return;
      const fill = progressEl.querySelector('.fill');
      const details = progressEl.querySelector('.details');
      const icon = progressEl.querySelector('.icon');
      if (fill) fill.style.width = `${percent}%`;
      if (status && details) details.textContent = this.getStatusText(status);
      if (status && icon) icon.innerHTML = this.getStatusIcon(status).outerHTML;
   }
   maybeLockUploads(fieldId) {
      const fieldEl = this.fieldElements.get(fieldId);
      const fieldData = this.getFieldData(fieldId);
      if (!fieldEl?.ui?.dropZone || !fieldData) return;
      if (fieldData.config.destination === 'post_group') return;
      const uploadCount = fieldData.uploads?.size || 0;
      const maxFiles = fieldData.config?.maxFiles || 999;
      fieldEl.ui.dropZone.hidden = uploadCount >= maxFiles;
      fieldEl.element.classList.toggle('at-max-uploads', uploadCount >= maxFiles);
   }
   /*******************************************************************************
    * GROUP MANAGEMENT
    *******************************************************************************/
   createGroup(fieldId, groupId = null) {
      const fieldData = this.getFieldData(fieldId);
      const fieldEl = this.fieldElements.get(fieldId);
      if (!fieldData || !fieldEl) return null;
      if (!groupId) {
         groupId = `group_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
      }
      const groupElement = this.createGroupElement(groupId, fieldId);
      if (!groupElement) return null;
      // Store in field UI Map
      if (!fieldEl.ui.groups) {
         fieldEl.ui.groups = {
            groups: new Map(),
            container: null,
            empty: null,
            display: null
         };
      }
      fieldEl.ui.groups.groups.set(groupId, groupElement);
      // Insert into DOM
      if (fieldEl.ui.groups.container && fieldEl.ui.groups.empty) {
         fieldEl.ui.groups.container.insertBefore(groupElement, fieldEl.ui.groups.empty);
      } else if (fieldEl.ui.groups.container) {
         fieldEl.ui.groups.container.appendChild(groupElement);
      }
      // Store group element reference
      const grid = groupElement.querySelector('.item-grid.group');
      this.groupElements.set(groupId, {
         element: groupElement,
         grid: grid,
         fieldId: fieldId
      });
      // Add to field groups
      if (!fieldData.groups) fieldData.groups = [];
      fieldData.groups.push({
         id: groupId,
         uploads: [],
         changes: {}
      });
      this.saveFieldData(fieldData);
      // Initialize selection handler and sortable
      this.addGroupSelectionHandler(fieldId, groupId);
      // Initialize sortable for this new group grid
      if (grid) {
         const sortableInstance = new Sortable(grid, {
            ...this.sortableConfig,
            group: { name: fieldId, pull: true, put: true },
            disabled: true, // Empty initially
            onAdd: (evt) => this.updateSortableState(evt.to),
            onRemove: (evt) => this.updateSortableState(evt.from)
         });
         grid.sortableInstance = sortableInstance;
         this.sortableInstances.set(`${fieldId}-group-${groupId}`, sortableInstance);
      }
      return { id: groupId, element: groupElement, grid: grid };
   }
   createGroupElement(groupId, fieldId) {
      let groupElement = window.getTemplate('imageGroup');
      if (!groupElement) return;
      groupElement.dataset.groupId = groupId;
      groupElement.dataset.fieldId = fieldId;
      let fields = window.getTemplate('groupMetadata');
      const fieldsContainer = groupElement.querySelector('.fields');
      if (fieldsContainer && fields) {
         fieldsContainer.append(fields);
         const titleInput = fieldsContainer.querySelector('[name="post_title"]');
         const excerptInput = fieldsContainer.querySelector('[name="post_excerpt"]');
         if (titleInput) {
            titleInput.id = `${groupId}_title`;
            titleInput.name = `${groupId}[post_title]`;
         }
         if (excerptInput) {
            excerptInput.id = `${groupId}_excerpt`;
            excerptInput.name = `${groupId}[post_excerpt]`;
         }
         const fieldData = this.getFieldData(fieldId);
         if (fieldData && fieldData.config.content !== '') {
            let summary = groupElement.querySelector('summary');
            if (summary) summary.textContent = fieldData.config.content + ' Fields';
         }
      } else {
         groupElement.querySelector('details')?.remove();
      }
      const gridContainer = groupElement.querySelector('.item-grid.group');
      if (gridContainer) {
         gridContainer.dataset.groupId = groupId;
      }
      return groupElement;
   }
   deleteGroup(groupId, confirm = true) {
      const groupEl = this.groupElements.get(groupId);
      if (!groupEl) return;
      const fieldData = this.getFieldData(groupEl.fieldId);
      if (!fieldData) return;
      const group = fieldData.groups?.find(g => g.id === groupId);
      let keepUploads = true;
      if (confirm && group?.uploads?.length > 0) {
         keepUploads = !window.confirm('Delete uploads in group?');
      }
      if (confirm && keepUploads && group?.uploads) {
         // Move uploads back to preview
         group.uploads.forEach(uploadId => {
            this.removeFromGroup(uploadId);
         });
      }
      // Remove from field groups
      if (fieldData.groups) {
         fieldData.groups = fieldData.groups.filter(g => g.id !== groupId);
         this.saveFieldData(fieldData);
      }
      // Remove DOM element
      if (groupEl.element) {
         groupEl.element.remove();
         this.a11y.announce('Group removed');
      }
      // Remove from maps
      this.groupElements.delete(groupId);
      // Clean up sortable
      const sortableKey = `${groupEl.fieldId}-group-${groupId}`;
      const sortable = this.sortableInstances.get(sortableKey);
      if (sortable?.destroy) {
         sortable.destroy();
      }
      this.sortableInstances.delete(sortableKey);
      this.schedulePersistance(groupEl.fieldId);
   }
   addToGroup(uploadId, target = null, persist = true) {
      const upload = this.uploadStore.get(uploadId);
      const uploadEl = this.uploadElements.get(uploadId);
      if (!upload || !uploadEl) return;
      const fieldData = this.getFieldData(upload.fieldId);
      const fieldEl = this.fieldElements.get(upload.fieldId);
      if (!fieldData || !fieldEl) return;
      // Already in correct location
      if ((!target && uploadEl.location === fieldEl.ui.preview) || target === uploadEl.location) {
         return;
      }
      // Remove from previous group
      if (upload.groupId) {
         const group = fieldData.groups?.find(g => g.id === upload.groupId);
         if (group) {
            group.uploads = group.uploads.filter(id => id !== uploadId);
            if (group.uploads.length === 0) {
               this.deleteGroup(upload.groupId);
            }
         }
      }
      // Clear selection checkbox
      const checkbox = uploadEl.element.querySelector('[name*="select-item"]');
      if (checkbox) checkbox.checked = false;
      let featured = uploadEl.element.querySelector('[name="featured"]');
      if (featured) featured.hidden = !target;
      // Moving to preview or to group
      if (!target || target.classList.contains('preview')) {
         target = fieldEl.ui.preview;
         upload.groupId = null;
      } else {
         // Moving to group
         const groupId = target.dataset.groupId;
         if (featured) featured.name = groupId + '_' + featured.name;
         const group = fieldData.groups?.find(g => g.id === groupId);
         if (group) {
            if (!group.uploads) group.uploads = [];
            group.uploads.push(uploadId);
            upload.groupId = groupId;
         }
      }
      // Update location
      uploadEl.location = target;
      target.append(uploadEl.element);
      // Update stores
      this.uploadStore.save(upload);
      if (persist) {
         this.saveFieldData(fieldData);
      }
      // Update sortable state
      this.updateSortableState(target);
      if (uploadEl.location && uploadEl.location !== target) {
         this.updateSortableState(uploadEl.location);
      }
   }
   removeFromGroup(uploadId) {
      const upload = this.uploadStore.get(uploadId);
      const uploadEl = this.uploadElements.get(uploadId);
      if (!upload || !uploadEl) return;
      const fieldData = this.getFieldData(upload.fieldId);
      const fieldEl = this.fieldElements.get(upload.fieldId);
      if (!fieldData || !fieldEl) return;
      // Remove from current group
      if (upload.groupId) {
         const group = fieldData.groups?.find(g => g.id === upload.groupId);
         if (group) {
            group.uploads = group.uploads.filter(id => id !== uploadId);
            if (group.uploads.length === 0) {
               this.deleteGroup(upload.groupId, false);
            }
         }
         upload.groupId = null;
      }
      // Move back to preview
      if (fieldEl.ui?.preview) {
         fieldEl.ui.preview.appendChild(uploadEl.element);
         uploadEl.location = fieldEl.ui.preview;
      }
      // Hide featured radio
      const featured = uploadEl.element.querySelector('[name="featured"]');
      if (featured) {
         featured.hidden = true;
         featured.checked = false;
      }
      this.uploadStore.save(upload);
      this.updateSortableState(fieldEl.ui.preview);
   }
   removeUpload(fieldId, uploadId) {
      const fieldData = this.getFieldData(fieldId);
      const upload = this.uploadStore.get(uploadId);
      const uploadEl = this.uploadElements.get(uploadId);
      if (!fieldData || !upload) return;
      // Remove from field
      fieldData.uploads?.delete(uploadId);
      // Remove from group if grouped
      if (upload.groupId) {
         const group = fieldData.groups?.find(g => g.id === upload.groupId);
         if (group) {
            group.uploads = group.uploads.filter(id => id !== uploadId);
            if (group.uploads.length === 0) {
               this.deleteGroup(upload.groupId);
            }
         }
      }
      // Clean up element
      uploadEl?.element?.remove();
      // Clean up memory
      this.clearUpload(uploadId);
      // Update field state
      this.saveFieldData(fieldData);
      this.updateFieldState(fieldId);
      this.maybeLockUploads(fieldId);
      const handler = this.selectionHandlers.get(fieldId);
      if (handler) {
         handler.deselect(uploadId);
      }
      this.a11y.announce('Upload removed');
   }
   handleGroupMetaChange(input) {
      const groupEl = this.getGroupFromElement(input);
      if (!groupEl) return;
      const fieldData = this.getFieldData(groupEl.fieldId);
      const group = fieldData?.groups?.find(g => g.id === groupEl.element.dataset.groupId);
      if (!group) return;
      if (!group.changes) group.changes = {};
      let name = input.name;
      if (name.includes('group')) {
         name = name.replace(`${group.id}_`, '').replace(`${group.id}[`, '').replace(']', '');
      }
      group.changes[name] = input.value;
      this.saveFieldData(fieldData);
      this.schedulePersistance(groupEl.fieldId);
   }
   /*******************************************************************************
    * ACTION HANDLERS
    *******************************************************************************/
   handleAction(button) {
      const action = button.dataset.action;
      const fieldId = this.getFieldIdFromElement(button);
      switch(action) {
         case 'add-to-group':
            this.handleAddToGroup(button);
            break;
         case 'delete-group':
            this.handleDeleteGroup(button);
            break;
         case 'delete-upload':
         case 'remove-from-group':
            this.handleRemoveItem(button);
            break;
         case 'upload':
            const fieldEl = this.fieldElements.get(fieldId);
            if (fieldEl) {
               fieldEl.element.closest('details').open = false;
               document.body.classList.add('uploading');
               this.submitUploads(fieldId);
            }
            break;
         case 'restore':
            this.handleRestoreUploads().then(() => {});
            break;
         case 'clear-cache':
            if (!confirm('Save these uploads for later?')) {
               this.cleanupStoredUploads();
            }
            this.cleanupRestore();
            break;
      }
   }
   handleAddToGroup(button) {
      const fieldElement = button.closest(this.selectors.field.field);
      const fieldId = fieldElement?.dataset.uploader;
      if (!fieldId) return;
      const selected = this.selected.get(fieldId);
      if (!selected || selected.size === 0) {
         this.createGroup(fieldId);
      } else {
         const group = this.createGroup(fieldId);
         if (!group) return;
         selected.forEach(uploadId => {
            this.addToGroup(uploadId, group.grid);
         });
         const handler = this.selectionHandlers.get(fieldId);
         handler?.clearSelection();
         this.a11y.announce(`Created group with ${selected.size} items`);
      }
      this.schedulePersistance(fieldId);
   }
   handleDeleteGroup(button) {
      const group = button.closest(this.selectors.groups.container);
      if (!group) return;
      const groupId = group.dataset.groupId;
      const fieldId = this.getFieldIdFromElement(group);
      if (!confirm('Delete this group? Items will be moved back to the upload area.')) {
         return;
      }
      const items = group.querySelectorAll(this.selectors.items.item);
      items.forEach(item => {
         const uploadId = item.dataset.uploadId;
         this.removeFromGroup(uploadId);
      });
      this.deleteGroup(groupId);
      this.a11y.announce('Group deleted, items returned to upload area');
      this.schedulePersistance(fieldId);
   }
   handleRemoveItem(button) {
      const item = button.closest(this.selectors.items.item);
      if (!item) return;
      const uploadId = item.dataset.uploadId;
      const fieldId = this.getFieldIdFromElement(item);
      if (!confirm('Remove this item?')) return;
      this.removeUpload(fieldId, uploadId);
      this.a11y.announce('Item removed');
      this.schedulePersistance(fieldId);
   }
   /*******************************************************************************
    * SELECTION MANAGEMENT
    *******************************************************************************/
   addFieldSelectionHandler(fieldId) {
      if (this.selectionHandlers.has(fieldId)) {
         return this.selectionHandlers.get(fieldId);
      }
      const fieldEl = this.fieldElements.get(fieldId);
      if (!fieldEl?.element) return;
      const handler = new window.jvbHandleSelection({
         container: fieldEl.element,
         ui: {
            selectAll: fieldEl.element.querySelector('[name="select-all-uploads"]'),
            bulkControls: fieldEl.element.querySelector('.selection-actions'),
            count: fieldEl.element.querySelector('.selection-count')
         },
         itemSelector: '[data-upload-id]',
         checkboxSelector: '[name*="select-item"]'
      });
      handler.subscribe((event, data) => {
         switch(event) {
            case 'item-selected':
            case 'item-deselected':
            case 'range-selected':
               this.selected.set(fieldId, data.selectedItems);
               break;
            case 'select-all':
               this.handleSelectAll(data.container, data.selected);
               break;
         }
      });
      this.selectionHandlers.set(fieldId, handler);
      return handler;
   }
   addGroupSelectionHandler(fieldId, groupId) {
      const handlerKey = `${fieldId}_${groupId}`;
      if (this.selectionHandlers.has(handlerKey)) {
         return this.selectionHandlers.get(handlerKey);
      }
      const groupEl = this.groupElements.get(groupId);
      if (!groupEl?.element) return;
      const handler = new window.jvbHandleSelection({
         container: groupEl.element,
         ui: {
            selectAll: groupEl.element.querySelector(this.selectors.groups.selectAll),
            bulkControls: groupEl.element.querySelector(this.selectors.groups.actions),
            count: groupEl.element.querySelector(this.selectors.groups.count)
         },
         itemSelector: '[data-upload-id]',
         checkboxSelector: '[name*="select-item"]'
      });
      handler.subscribe((event, data) => {
         switch(event) {
            case 'item-selected':
            case 'item-deselected':
            case 'range-selected':
               this.selected.set(fieldId, data.selectedItems);
               break;
            case 'select-all':
               this.handleSelectAll(data.container, data.selected);
               break;
         }
      });
      this.selectionHandlers.set(handlerKey, handler);
      return handler;
   }
   handleSelectAll(container, selected) {
      // Can add custom logic here if needed
   }
   getCurrentSelection(fieldId) {
      let selected = [];
      for (let [key, handler] of this.selectionHandlers) {
         if ((fieldId === key || key.includes(fieldId)) && handler.selectedItems.size > 0) {
            selected = selected.concat([...handler.selectedItems]);
         }
      }
      return selected;
   }
   /*******************************************************************************
    * HELPER METHODS
    *******************************************************************************/
   /**
    * Get field data from store and normalize it
    * Always use this instead of directly accessing fieldStore.get()
    */
   getFieldData(fieldId) {
      const fieldData = this.fieldStore.get(fieldId);
      if (!fieldData) return null;
      // Only convert uploads back to Set (DataStore returns Arrays)
      if (Array.isArray(fieldData.uploads)) {
         fieldData.uploads = new Set(fieldData.uploads);
      } else if (!fieldData.uploads) {
         fieldData.uploads = new Set();
      }
      // Ensure groups is an array
      if (!Array.isArray(fieldData.groups)) {
         fieldData.groups = [];
      }
      return fieldData;
   }
   /**
    * Save field data to store, converting Sets to Arrays
    */
   async saveFieldData(fieldData) {
      await this.fieldStore.save({
         ...fieldData,
         timestamp: Date.now()
      });
   }
   determineFieldId(fieldElement) {
      const content = fieldElement.dataset.content ||
         fieldElement.closest('dialog')?.dataset.content ||
         fieldElement.closest('form')?.dataset.save || '';
      const itemID = fieldElement.dataset.itemId ||
         fieldElement.closest('dialog')?.dataset.itemId || '';
      const field = fieldElement.dataset.field || '';
      return `${content}_${itemID}_${field}`;
   }
   getFromElement(element, type) {
      const map = {
         'field': {
            selector: this.selectors.field.field,
            key: 'uploader',
            getRuntimeData: (id) => this.fieldElements.get(id),
            getStoreData: (id) => this.getFieldData(id)
         },
         'upload': {
            selector: this.selectors.items.item,
            key: 'uploadId',
            getRuntimeData: (id) => this.uploadElements.get(id),
            getStoreData: (id) => this.uploadStore.get(id)
         },
         'group': {
            selector: this.selectors.groups.container,
            key: 'groupId',
            getRuntimeData: (id) => this.groupElements.get(id),
            getStoreData: (id) => {
               // Groups are stored in field.groups array
               const groupEl = this.groupElements.get(id);
               if (!groupEl) return null;
               const fieldData = this.getFieldData(groupEl.fieldId);
               return fieldData?.groups?.find(g => g.id === id);
            }
         }
      };
      const config = map[type];
      if (!config) return null;
      const el = element.closest(config.selector);
      if (!el) return null;
      const id = el.dataset[config.key];
      // Return combined runtime + store data for convenience
      const runtime = config.getRuntimeData(id);
      const store = config.getStoreData(id);
      return { ...runtime, ...store };
   }
   getFieldFromElement(el) { return this.getFromElement(el, 'field'); }
   getUploadFromElement(el) { return this.getFromElement(el, 'upload'); }
   getGroupFromElement(el) { return this.getFromElement(el, 'group'); }
   getFieldIdFromElement(el) {
      const field = this.getFromElement(el, 'field');
      return field?.id ?? null;
   }
   getUploadIdFromElement(el) {
      const upload = this.getFromElement(el, 'upload');
      return upload?.id ?? null;
   }
   getGroupIdFromElement(el) {
      const group = this.getFromElement(el, 'group');
      return group?.id ?? null;
   }
   getSubtypeFromMime(mimeType) {
      if (mimeType.startsWith('image/')) return 'image';
      if (mimeType.startsWith('video/')) return 'video';
      return 'document';
   }
   getStatusText(status) {
      return this.statusMapping[status] || status;
   }
   getStatusIcon(status) {
      return window.getIcon(this.queue.icons[status]);
      return map[status]||status;
   }
   getStatusProgress(status) {
      const progress = {
      let progress = {
         'local_processing': 28,
         'queued': 50,
         'uploading': 66,
@@ -2456,37 +1091,21 @@
         'processing': 89,
         'completed': 100
      };
      return progress[status] || 0;
      return progress[status]??0;
   }
   getModalType(fieldEl) {
      if (!fieldEl?.element) return null;
      if (fieldEl._cachedModalType !== undefined) {
         return fieldEl._cachedModalType;
      }
      const dialog = fieldEl.element.closest('dialog');
      if (!dialog) {
         fieldEl._cachedModalType = null;
         return null;
      }
      let modalType = null;
      if (dialog.classList.contains('edit')) modalType = 'edit';
      else if (dialog.classList.contains('create')) modalType = 'create';
      else if (dialog.classList.contains('bulkEdit')) modalType = 'bulkEdit';
      else modalType = dialog.className;
      fieldEl._cachedModalType = modalType;
      return modalType;
   }
   createUploadElement(upload, draggable = false) {
   /*******************************************************************************
    UPLOAD METHODS
   *******************************************************************************/
   async createUpload(uploadId, file, fieldId) {
      let image = window.getTemplate('uploadItem');
      if (!image) return;
      if (!image) return null;
      image.dataset.uploadId = upload.id;
      image.dataset.subtype = upload.subtype || 'image';
      let field = this.fields.get(fieldId);
      if (!field) return null;
      image.dataset.uploadId = uploadId;
      let mimeType = this.getSubtypeFromMime(file.type)||'image';
      image.dataset.subtype = mimeType;
      let [featured, img, video, preview, details] = [
         image.querySelector('[name="featured"]'),
@@ -2496,33 +1115,37 @@
         image.querySelector('details')
      ];
      if (featured) featured.value = upload.id;
      switch (upload.subtype) {
      if (featured) featured.value = uploadId;
      switch (mimeType) {
         case 'image':
            if (img) {
               img.src = upload.preview;
               img.alt = upload.meta?.originalName || '';
               const previewUrl = this.createPreviewUrl(file);
               img.src = previewUrl;
               img.alt = file.name || '';
               img.dataset.previewUrl = previewUrl;
            }
            video?.remove();
            preview?.remove();
            break;
         case 'video':
            if (video) video.src = upload.preview;
            if (video){
               const previewUrl = this.createPreviewUrl(file);
               video.src = previewUrl;
               video.dataset.previewUrl = previewUrl;
            }
            img?.remove();
            preview?.remove();
            break;
         case 'document':
            const fileName = upload.meta?.originalName || '';
            const extension = fileName.split('.').pop()?.toLowerCase() || '';
            const iconMap = {
            let ext = file.name.split('.').pop()?.toLowerCase()??'';
            let map = {
               'pdf': 'file-pdf', 'csv': 'file-csv',
               'doc': 'file-doc', 'docx': 'file-doc',
               'txt': 'file-txt', 'xls': 'file-xls', 'xlsx': 'file-xls'
            };
            const icon = window.getIcon(iconMap[extension] || 'file');
            let icon = window.getIcon(map[ext]??'file');
            if (preview) {
               preview.innerText = fileName;
               preview.innerText = file.name;
               preview.prepend(icon);
            }
            img?.remove();
@@ -2535,13 +1158,12 @@
         if (template) details.append(template);
      }
      image.draggable = draggable;
      image.draggable = field.config.type !== 'single'??false;
      // Update input IDs
      image.querySelectorAll('input').forEach(input => {
      image.querySelectorAll('input').forEach(input  => {
         let id = input.id;
         if (id) {
            let newId = id + upload.id;
            let newId = id + uploadId;
            let label = input.parentNode.querySelector(`label[for="${id}"]`);
            input.id = newId;
            if (label) label.htmlFor = newId;
@@ -2551,348 +1173,527 @@
      return image;
   }
   /*******************************************************************************
    * PERSISTENCE
    *******************************************************************************/
   getSubtypeFromMime(mimeType) {
      if (mimeType.startsWith('image/')) return 'image';
      if (mimeType.startsWith('video/')) return 'video';
      return 'document';
   }
   /**
    * Normalize field data loaded from IndexedDB
    * Converts Arrays back to Sets, handles missing properties
    * Called by handleAction
    * @param button
    */
   normalizeFieldData(fieldData) {
      if (!fieldData) return null;
   async handleRemoveItem(button) {
      const item = button.closest(this.selectors.items.item);
      if (!item) return;
      // Convert uploads array back to Set
      if (Array.isArray(fieldData.uploads)) {
         fieldData.uploads = new Set(fieldData.uploads);
      } else if (!fieldData.uploads) {
         fieldData.uploads = new Set();
      }
      // Convert groups array, ensure proper structure
      if (!Array.isArray(fieldData.groups)) {
         fieldData.groups = [];
      }
      // Ensure each group has uploads array
      fieldData.groups = fieldData.groups.map(group => ({
         ...group,
         uploads: Array.isArray(group.uploads) ? group.uploads : []
      }));
      return fieldData;
      const uploadId = item.dataset.uploadId;
      if (!confirm('Remove this item?')) return;
      await this.removeUpload(uploadId);
      this.a11y.announce('Item removed');
   }
   schedulePersistance(fieldId) {
      const key = `persist_${fieldId}`;
      window.debouncer.schedule(
         key,
         () => this.persistFieldState(fieldId),
         1000
   async setBulkUpload(uploads, key, value) {
      const promises = Array.from(uploads).map(async (upload) => {
         if (key === 'status') {
            await this.setUploadStatus(upload, value);
         }
         upload[key] = value;
         return this.stores.uploads.save(upload);
      });
      await Promise.all(promises);
   }
   async setUploadStatus(upload, status) {
      if (upload.progress) {
         window.showProgress(upload.progress, this.getStatusProgress(status), 100, this.getStatusText(status), this.queue.icons[status]??'');
      }
   }
   async removeUpload(uploadId) {
      let upload = this.stores.uploads.get(uploadId);
      if (!upload) return;
      if (upload.group) {
         let group = this.stores.groups.get(upload.group);
         group.uploads = group.uploads.filter(id => id !== uploadId);
         if (group.uploads.length === 0) {
            await this.removeGroup(group.id, false);
         }
      }
      await this.clearUpload(uploadId);
      this.maybeLockUploads(upload.field);
      let handler = this.selectionHandlers.get(upload.field);
      if (handler){
         handler.deselect(uploadId);
      }
      this.a11y.announce('Upload removed');
   }
   async clearUpload(uploadId) {
      const element = this.uploads.get(uploadId);
      if (element) {
         this.revokePreviewUrl(element.preview);
         if (element.element) {
            const previewUrl = element.element.dataset.previewUrl;
            this.revokePreviewUrl(previewUrl);
            element.element.remove();
         }
      }
      this.uploads.delete(uploadId);
      await this.stores.uploads.delete(uploadId);
   }
   /*******************************************************************************
    GROUP METHODS
   *******************************************************************************/
   async handleAddToGroup(fieldId) {
      const selected = this.selected.get(fieldId);
      if (!selected || selected.size === 0) return;
      let groupId = await this.createGroup(fieldId);
      if (!groupId) return;
      await Promise.all(
         Array.from(selected).map(uploadId => this.addToGroup(uploadId, groupId))
      );
      this.selectionHandlers.get(fieldId)?.clearSelection();
      this.a11y.announce(`Created group with ${selected.size} items`);
   }
   async createGroup(fieldId, groupId = null) {
      let field = this.fields.get(fieldId);
      if (!field) return;
      if (!groupId) {
         groupId = window.generateID('group');
      }
      const element = this.createGroupElement(groupId, fieldId);
      if (!element) return null;
      field.groupUI.grid.append(element);
      // Create Sortable for this group's grid
      const grid = element.querySelector('.item-grid');
      if (grid) {
         grid.dataset.groupId = groupId;
         this.createSortableForGrid(fieldId, grid, groupId);
      }
      let storedData = this.stores.groups.data.has(groupId)
         ? this.stores.groups.data.get(groupId)
         : {};
      await this.setGroup(groupId, { ...storedData, id: groupId, field: fieldId });
      return groupId;
   }
   createGroupElement(groupId, fieldId = null) {
      let element = window.getTemplate('imageGroup');
      if (!element) return;
      element.dataset.groupId = groupId;
      if (fieldId) {
         element.dataset.fieldId = fieldId;
      }
      const selectAll = element.querySelector('[data-select-all]');
      if (selectAll) {
         const newId = `select-all-${groupId}`;
         const label = element.querySelector(`label[for="${selectAll.id}"]`);
         selectAll.id = newId;
         selectAll.name = newId;
         if (label) label.htmlFor = newId;
      }
      let fields = window.getTemplate('groupMetadata');
      let container = element.querySelector('.fields');
      if (fields && container) {
         container.append(fields);
         let title = container.querySelector('[name="post_title"]');
         let excerpt = container.querySelector('[name="post_excerpt"]');
         if (title) {
            title.id = `${groupId}_title`;
            title.name = `${groupId}[post_title]`;
         }
         if (excerpt) {
            excerpt.id = `${groupId}_excerpt`;
            excerpt.name = `${groupId}[post_excerpt]`;
         }
      } else {
         element.querySelector('details')?.remove();
      }
      const grid = element.querySelector('.item-grid');
      if (grid) {
         grid.dataset.groupId = groupId;
      }
      this.groups.set(groupId, {
         element: element,
         ui: window.uiFromSelectors(this.selectors.group, element)
      });
      return element;
   }
   async setGroup(groupId, data) {
      const defaults = {
         id: groupId,
         src: window.location.href,
         uploads: [],
         operationId: null,
         field: null,
         fields: {}
      };
      const group = {...defaults, ...data};
      Object.preventExtensions(group);
      await this.stores.groups.save(group);
   }
   async setBulkGroup(fieldId, key, value) {
      let groups = this.stores.groups.filterByIndex({field:fieldId});
      if (groups.length === 0) {
         return;
      }
      let Promises = groups.map(group => {
         group[key] = value;
         this.stores.groups.save(group);
      });
      await Promise.all(Promises);
   }
   async addToGroup(uploadId, groupId = null){
      const upload = this.stores.uploads.get(uploadId);
      const element = this.uploads.get(uploadId);
      if (!upload || !element) return;
      const field = this.fields.get(upload.field);
      if (!field) return;
      //Check if it's already in this destination, it's probably a reorder
      const isInDOM = element.element?.parentElement !== null;
      if (isInDOM && ((!groupId && upload.group === null) || groupId === upload.group)) {
         this.handleReorder(upload.field, groupId);
         return;
      }
      if (upload.group) {
         const group = this.stores.groups.get(upload.group);
         if (group) {
            group.uploads = group.uploads.filter(id => id !== uploadId);
            if (group.uploads.length === 0) {
               await this.removeGroup(group.id, false);
            }
         }
      }
      //clear any selection
      if (element.ui.checkbox) element.ui.checkbox.checked = false;
      if (this.selected.get(upload.field)?.has(uploadId)) {
         this.selected.get(upload.field).delete(uploadId);
      }
      if (element.ui.featured) element.ui.featured.hidden = !groupId;
      if (!groupId) {
         upload.group = null;
      } else {
         if (element.ui.featured) element.ui.featured.name = `${groupId}_featured`;
         let group = this.stores.groups.get(groupId);
         if (group) {
            group.uploads.push(uploadId);
            upload.group = groupId;
            this.stores.groups.save(group);
         }
      }
      let target = (groupId) ? this.groups.get(groupId)?.ui.grid : field.ui.grid;
      if (target) {
         target.append(element.element)
      }
      this.stores.uploads.save(upload);
   }
   handleDeleteGroup(button) {
      const group = button.closest(this.selectors.group.item);
      if (!group) return;
      let groupId = group.dataset.groupId;
      if (!confirm('Delete this group? Items will be moved back to the upload area.')) {
         return;
      }
      let uploads = this.stores.uploads.filterByIndex({group: groupId});
      Promise.all(
         uploads.map(upload => this.addToGroup(upload.id, null))
      ).then(() => {
         this.removeGroup(groupId, false).then(()=>{});
         this.a11y.announce('Group deleted. Items returned to upload area');
      });
   }
   async removeGroup(groupId, confirm = true) {
      let element = this.groups.get(groupId);
      let group = this.stores.groups.get(groupId);
      if (!group) return;
      let keepUploads = true;
      if (confirm && group.uploads.length > 0) {
         keepUploads = window.confirm('Keep uploads in this group?');
      }
      await Promise.all(
         group.uploads.map(uploadId =>
            keepUploads ? this.addToGroup(uploadId, null) : this.removeUpload(uploadId)
         )
      );
      // Destroy the Sortable for this group
      const sortableKey = this.getGroupKey(group.field, groupId);
      const sortable = this.sortables.get(sortableKey);
      if (sortable?.destroy) {
         sortable.destroy();
      }
      this.sortables.delete(sortableKey);
      if (element?.element) {
         element.element.remove();
      }
      this.groups.delete(groupId);
      await this.stores.groups.delete(groupId);
      this.a11y.announce('Group removed');
   }
   maybeLockUploads(fieldId) {
      let field = this.fields.get(fieldId);
      if (!field || !field.ui.dropZone) return;
      let uploads = this.stores.uploads.filterByIndex({field: fieldId});
      let count = uploads.length;
      let max = field.config.maxFiles??25;
      field.ui.dropZone.hidden = count >= max;
   }
   /*******************************************************************************
    OPERATION METHODS
   *******************************************************************************/
   async handleOperationCancelled(fieldId) {
      const uploads = this.stores.uploads.filterByIndex({field: fieldId});
      const groups = this.stores.groups.filterByIndex({field: fieldId});
      await Promise.all([
         ...uploads.map(upload => this.removeUpload(upload.id)),
         ...groups.map(group => this.removeGroup(group.id, false))
      ]);
      this.a11y.announce('Upload Cancelled');
   }
   async handleOperationFailed(operation, fieldId) {
      // Mark uploads as failed, maybe show retry UI
      await this.setBulkUpload(
         this.stores.uploads.filterByIndex({field: fieldId}),
         'status',
         'failed'
      );
   }
   async persistFieldState(fieldId) {
      const fieldData = this.getFieldData(fieldId);
      if (!fieldData) return;
      // Save with updated timestamp
      await this.saveFieldData(fieldData);
   async handleFieldStatus(fieldId, operation) {
      let status = operation.status;
      let uploads = this.stores.uploads.filterByIndex({field: fieldId});
      await this.setBulkUpload(uploads, 'status', status);
   }
   // In UploadManager, add blob conversion helpers
   async saveBlobData(uploadId, file) {
      const arrayBuffer = await file.arrayBuffer();
      const uploadData = this.uploadStore.get(uploadId) || { id: uploadId };
      // Store blob data as ArrayBuffer with metadata
      uploadData.blobData = {
         buffer: arrayBuffer,
         name: file.name,
         type: file.type,
         size: file.size,
         lastModified: file.lastModified || Date.now()
      };
      await this.uploadStore.save(uploadData);
   }
   async getBlobData(uploadId) {
      const upload = this.uploadStore.get(uploadId);
      if (!upload?.blobData) return null;
      // Reconstruct File from ArrayBuffer
      const blob = new Blob([upload.blobData.buffer], { type: upload.blobData.type });
      return new File([blob], upload.blobData.name, {
         type: upload.blobData.type,
         lastModified: upload.blobData.lastModified
      });
   }
   /*******************************************************************************
    * RECOVERY & RESTORATION
    *******************************************************************************/
   handleFieldStoreEvent(event, data) {
      switch(event) {
         case 'data-loaded':
            // Check for pending uploads to restore
            this.checkForStoredUploads();
            break;
      }
    SELECTION HANDLERS
   *******************************************************************************/
   getGroupKey(fieldId, groupId = null) {
      return (groupId) ? `${fieldId}_${groupId}` : `${fieldId}`;
   }
   handleUploadStoreEvent(event, data) {
      switch(event) {
         case 'data-loaded':
            break;
         case 'item-saved':
            this.showSaveIndicator(data.key);
            break;
      }
   }
   getSelectionHandler(fieldId) {
      let key = this.getGroupKey(fieldId);
   async checkForStoredUploads() {
      const allFieldStates = this.fieldStore.getAll();
      console.log('Checking for stored uploads...', {
         fieldStates: allFieldStates.length,
         uploadStoreSize: this.uploadStore.data.size
      });
      const pendingFields = allFieldStates.filter(field => {
         if (!field.uploads) return false;
         // Handle both Set and Array (from IndexedDB)
         const uploadsArray = field.uploads instanceof Set
            ? Array.from(field.uploads)
            : Array.isArray(field.uploads)
               ? field.uploads
               : [];
         return uploadsArray.some(uploadId => {
            const upload = this.uploadStore.get(uploadId);
            return upload && !upload.operationId &&
               ['completed', 'processed', 'local_processing', 'processed-original'].includes(upload.status);
      if (!this.selectionHandlers.has(key)) {
         let field = this.fields.get(fieldId);
         if (!field) return;
         let handler = new window.jvbHandleSelection({
            container:  field.element,
            item: this.selectors.items.item,
            count: this.selectors.fields.count,
            bulkControls: this.selectors.fields.actions,
            checkbox: this.selectors.items.checkbox,
            selectAll: this.selectors.fields.selectAll,
            wrapper: `${this.selectors.fields.preview}, ${this.selectors.group.item}`,
         });
      });
      console.log('Found pending fields:', pendingFields.length);
      if (pendingFields.length === 0) return;
      this.showRecoveryNotification(pendingFields);
   }
         handler.subscribe((event, data) => {
            this.selected.set(fieldId, data.selectedItems);
            console.log(Array.from(this.selected));
            this.syncSortableSelection(fieldId, data.selectedItems);
         });
   async showRecoveryNotification(pendingFields) {
      const totalUploads = pendingFields.reduce((sum, field) => sum + field.uploads.length, 0);
      const totalGroups = pendingFields.reduce((sum, field) =>
         sum + (field.groups?.length || 0), 0);
      let notification = window.getTemplate('restoreNotification');
      if (!notification) {
         console.error('Restore notification template not found');
         return;
         this.selectionHandlers.set(key, handler);
      }
      // Build appropriate message
      let message;
      if (totalGroups > 0) {
         let group = totalGroups > 1 ? 'groups' : 'group';
         let upload = totalUploads > 1 ? 'uploads' : 'upload';
         message = `${totalGroups} ${group} with ${totalUploads} ${upload} can be restored.`;
      return this.selectionHandlers.get(key);
   }
   /*******************************************************************************
    SORTABLE
   *******************************************************************************/
   initSortable(fieldId) {
      if (!window.Sortable) return;
      const field = this.fields.get(fieldId);
      if (!field) return;
      if (!Sortable._multiDragMounted && Sortable.MultiDrag) {
         Sortable.mount(new Sortable.MultiDrag());
         Sortable._multiDragMounted = true;
      }
      // Create sortable for the main preview grid
      this.createSortable(fieldId, field.ui.grid, null);
      // Set up empty-group as native drop zone
      this.initEmptyGroupDropZone(fieldId);
   }
   createSortable(fieldId, gridElement, groupId) {
      if (!gridElement) return null;
      const key = this.getGroupKey(fieldId, groupId);
      // Already exists
      if (this.sortables.has(key)) {
         return this.sortables.get(key);
      }
      const sortable = new Sortable(gridElement, {
         animation: 150,
         draggable: '.item',
         multiDrag: true,
         selectedClass: 'selected',
         avoidImplicitDeselect: true,
         group: { name: fieldId, pull: true, put: true },
         ghostClass: 'ghost',
         chosenClass: 'chosen',
         dragClass: 'dragging',
         onStart: () => this.syncSortableSelection(fieldId),
         onEnd: (evt) => this.sortableDrop(evt, fieldId),
      });
      this.sortables.set(key, sortable);
      return sortable;
   }
   initEmptyGroupDropZone(fieldId) {
      const field = this.fields.get(fieldId);
      const emptyZone = field?.groupUI?.empty;
      if (!emptyZone) return;
      emptyZone.addEventListener('dragover', (e) => {
         e.preventDefault();
         e.dataTransfer.dropEffect = 'move';
         emptyZone.classList.add('drag-over');
      });
      emptyZone.addEventListener('dragleave', (e) => {
         if (!emptyZone.contains(e.relatedTarget)) {
            emptyZone.classList.remove('drag-over');
         }
      });
      emptyZone.addEventListener('drop', async (e) => {
         e.preventDefault();
         emptyZone.classList.remove('drag-over');
         // Get selected items from our tracking
         const selectedItems = this.selected.get(fieldId);
         if (!selectedItems || selectedItems.size === 0) return;
         const groupId = await this.createGroup(fieldId);
         if (!groupId) return;
         await Promise.all(
            Array.from(selectedItems).map(uploadId => this.addToGroup(uploadId, groupId))
         );
         this.selectionHandlers.get(fieldId)?.clearSelection();
      });
   }
   async sortableDrop(evt, fieldId) {
      const dropTarget = evt.to;
      const items = evt.items?.length > 0 ? Array.from(evt.items) : [evt.item];
      const uploadIds = items.map(item => item.dataset.uploadId).filter(Boolean);
      if (uploadIds.length === 0) return;
      // Determine target group from the grid's data attribute
      const targetGroupId = dropTarget.dataset.groupId || null;
      await Promise.all(
         uploadIds.map(uploadId => this.addToGroup(uploadId, targetGroupId))
      );
      this.selectionHandlers.get(fieldId)?.clearSelection();
   }
   syncSortableSelection(fieldId) {
      const selectedItems = this.selected.get(fieldId) || new Set();
      for (const [uploadId, uploadData] of this.uploads) {
         const upload = this.stores.uploads.get(uploadId);
         if (!upload || upload.field !== fieldId) continue;
         const element = uploadData.element;
         if (!element) continue;
         const shouldBeSelected = selectedItems.has(uploadId);
         if (shouldBeSelected && !element.classList.contains('selected')) {
            Sortable.utils.select(element);
         } else if (!shouldBeSelected && element.classList.contains('selected')) {
            Sortable.utils.deselect(element);
         }
      }
   }
   handleReorder(fieldId, groupId = null) {
      let target = (groupId) ? this.groups.get(groupId)?.ui.grid : this.fields.get(fieldId)?.ui.grid;
      if (!target) {
         console.log ('Couldn\'t Reorder items...');
         return;
      }
      //Get current order from DOM
      let items = Array.from(target.querySelectorAll(this.selectors.items.item+':not(.ghost)'))
         .map(upload => upload.dataset.uploadId)
         .filter(id => id);
      if (!groupId) {
         let hiddenInput = this.fields.get(fieldId)?.ui.hidden;
         if (hiddenInput) {
            hiddenInput.value = items.join(',');
         }
      } else {
         message = `${totalUploads} upload(s) from ${pendingFields.length} field(s) can be recovered.`;
      }
      const detailsEl = notification.querySelector('.restore-details');
      if (detailsEl) {
         detailsEl.textContent = message;
      }
      // Build the restoration preview
      for (const field of pendingFields) {
         let fieldTemplate = window.getTemplate('restoreField');
         if (!fieldTemplate) continue;
         // Set field name/title
         const titleEl = fieldTemplate.querySelector('h3');
         if (titleEl) {
            titleEl.textContent = field.config.name || 'Unnamed Field';
         let group = this.groups.get(groupId);
         if (group) {
            group.uploads = items;
         }
         const itemGrid = fieldTemplate.querySelector('.item-grid.restore');
         // Process each upload
         for (const upload of field.uploads) {
            let uploadItem = window.getTemplate('uploadItem');
            if (!uploadItem) continue;
            //
            //    const imgEl = uploadItem.querySelector('img');
            //    const placeholderEl = uploadItem.querySelector('.image-placeholder');
            //
            const file = await this.getBlobData(upload.id);
            if (file) {
               try {
                  // Create new blob URL from stored data
                  const previewUrl = this.createPreviewUrl(file);
                  let [
                     featured,
                     img,
                     video,
                     preview,
                     details
                  ] = [
                     uploadItem.querySelector('[name="featured"]'),
                     uploadItem.querySelector('img'),
                     uploadItem.querySelector('video'),
                     uploadItem.querySelector('label > span'),
                     uploadItem.querySelector('details')
                  ];
                  uploadItem.dataset.uploadId = upload.id;
                  uploadItem.dataset.fieldId = field.id;
                  let subtype = this.getSubtypeFromMime(file.type);
                  uploadItem.dataset.subtype = subtype;
                  switch (subtype) {
                     case 'image':
                        [
                           img.src,
                           img.alt
                        ] = [
                           previewUrl,
                           file.name ?? upload.meta?.originalName ?? ''
                        ];
                        video.remove();
                        preview.remove();
                        break;
                     case 'video':
                        video.src = previewUrl;
                        img.remove();
                        preview.remove();
                        break;
                     case 'document':
                        let extension = '';
                        let icon;
                        switch (extension) {
                           case 'pdf':
                              icon = window.getIcon('file-pdf');
                              break;
                           case 'csv':
                              icon = window.getIcon('file-csv');
                              break;
                           case 'doc':
                              icon = window.getIcon('file-doc');
                              break;
                           case 'txt':
                              icon = window.getIcon('file-txt');
                              break;
                           case 'xls':
                              icon = window.getIcon('file-xls');
                              break;
                           default:
                              icon = window.getIcon('file');
                              break;
                        }
                        preview.innerText = upload.originalFile.name;
                        preview.prepend(icon);
                        img.remove();
                        video.remove();
                        break;
                  }
                  // Store URL for cleanup later
                  uploadItem.dataset.previewUrl = previewUrl;
               } catch (error) {
                  console.warn('Failed to create preview for upload:', upload.id, error);
               }
            }
            // Set upload metadata
            const nameEl = uploadItem.querySelector('summary span');
            if (nameEl) {
               nameEl.textContent = upload.meta?.originalName || 'Unknown file';
            }
            const metaEl = uploadItem.querySelector('details');
            if (metaEl && upload.meta) {
               metaEl.textContent = `${this.formatBytes(upload.meta.size)} • ${upload.meta.type}`;
            }
            // Update input IDs safely
            uploadItem.querySelectorAll('input').forEach(input => {
               let id = input.id;
               if (id) {
                  let newId = id + upload.id;
                  let label = input.parentNode.querySelector(`label[for="${id}"]`);
                  input.id = newId;
                  if (label) {
                     label.htmlFor = newId;
                  }
               }
            });
            if (itemGrid) {
               itemGrid.appendChild(uploadItem);
            }
         }
         notification.querySelector('.wrap').appendChild(itemGrid);
      }
      document.querySelector('.field.upload').appendChild(notification);
      notification = document.querySelector('dialog.restore-uploads');
      this.restoreModal = new window.jvbModal(notification);
      this.restoreSelection = new window.jvbHandleSelection({
         container: notification,
         ui: {
            selectAll: notification.querySelector('#select-all-restore'),
            count: notification.querySelector('.selection-count'),
         },
      });
      this.restoreModal.handleOpen();
      this.a11y.announce('Items reordered');
   }
   async handleRestoreUploads() {
      let notification = document.querySelector('dialog.restore-uploads');
      if (!notification) {
         return;
      }
      const selectedUploads = this.getSelectedRestorationUploads(notification);
      if (selectedUploads.length === 0) {
         return;
      }
      await this.restoreSelectedUploads(selectedUploads);
      this.cleanupRestore();
   }
   showSaveIndicator(key) {
      // Optional: show user that state is being saved
   }
   cleanupRestore() {
      this.restoreModal.handleClose();
      this.restoreSelection.destroy();
      this.restoreSelection = null;
      this.restoreModal.destroy();
      this.restoreModal.modal.remove();
      this.restoreModal = null;
   }
   async cleanupStoredUploads() {
      await this.fieldStore.clear();
      await this.uploadStore.clear();
   }
   /*******************************************************************************
    * EVENT SYSTEM
    *******************************************************************************/
@@ -2904,49 +1705,99 @@
   notify(event, data = {}) {
      this.subscribers.forEach(cb => {
         try {
            cb(event, data);
         } catch (error) {
            console.error('Subscriber error:', error);
         }
         try { cb(event, data); } catch (e) { console.error('Subscriber error:', e); }
      });
   }
   /*******************************************************************************
    * DESTROY & CLEANUP
    *******************************************************************************/
   /********************************************************************
    CLEANUP
   ********************************************************************/
   destroy() {
      document.removeEventListener('click', this.clickHandler);
      document.removeEventListener('change', this.changeHandler);
      document.removeEventListener('dragenter', this.dragEnterHandler);
      document.removeEventListener('dragleave', this.dragLeaveHandler);
      document.removeEventListener('dragover', this.dragOverHandler);
      document.removeEventListener('drop', this.dropHandler);
      this.subscribers.clear();
      this.previewUrls.forEach(url => {
         this.revokePreviewUrl(url);
      });
      this.previewUrls.clear();
   }
      if (this.dragController) {
         this.dragController.destroy();
   cleanupAllPreviewUrls() {
      this.previewUrls.forEach(url => this.revokePreviewUrl(url));
      this.previewUrls.clear();
   }
   async handleClearCache() {
      const currentSrc = window.location.href;
      const uploads = this.stores.uploads.filterByIndex({src: currentSrc});
      const groups = this.stores.groups.filterByIndex({src:currentSrc});
      await Promise.all([
         ...uploads.map(upload => this.clearUpload(upload.id)),
         ...groups.map(group => {
            this.groups.get(group.id)?.element?.remove();
            this.groups.delete(group.id);
            return this.stores.groups.delete(group.id);
         })
      ]);
      this.a11y.announce('Cache cleared for this page');
   }
   /**
    * Get files from all upload fields in a form
    * Returns array of {file, fieldName, uploadId, meta}
    */
   async getFilesForForm(formElement) {
      const uploadFields = formElement.querySelectorAll(this.selectors.fields.field);
      const allFiles = [];
      for (const fieldElement of uploadFields) {
         const fieldId = this.determineFieldId(fieldElement);
         const fieldName = fieldElement.dataset.field;
         const uploads = this.stores.uploads.filterByIndex({ field: fieldId });
         for (const upload of uploads) {
            const file = this.formatFile(upload);
            if (file) {
               allFiles.push({
                  file: file,
                  fieldName: fieldName,
                  uploadId: upload.id,
                  meta: upload.fields || {}
               });
            }
         }
      }
      this.selectionHandlers.forEach(handler => handler.destroy());
      this.selectionHandlers.clear();
      return allFiles;
   }
      this.cleanupAllPreviewUrls();
   /**
    * Clear all uploads and groups for a specific field from stores
    */
   async clearFieldFromStores(fieldId) {
      const uploads = this.stores.uploads.filterByIndex({ field: fieldId });
      const groups = this.stores.groups.filterByIndex({ field: fieldId });
      this.sortableInstances.forEach(instance => {
         if (instance?.destroy) instance.destroy();
      });
      this.sortableInstances.clear();
      // Clear all uploads
      await Promise.all(
         uploads.map(upload => this.clearUpload(upload.id))
      );
      this.uploadElements.clear();
      this.fieldElements.clear();
      this.groupElements.clear();
      this.selected.clear();
      this.subscribers.clear();
      // Clear all groups
      await Promise.all(
         groups.map(group => {
            this.groups.get(group.id)?.element?.remove();
            this.groups.delete(group.id);
            return this.stores.groups.delete(group.id);
         })
      );
   }
}
// Initialize when DOM is ready
document.addEventListener('DOMContentLoaded', () => {
   window.jvbUploads = new UploadManager();
document.addEventListener('DOMContentLoaded', async function () {
   window.auth.subscribe((event) => {
      if (event === 'auth-loaded') {
         window.jvbUploads = new UploadManager();
      }
   });
});