import { domtools } from '@design.estate/dees-element';

export class PdfManager {
  private static pdfjsLib: any;
  private static initialized = false;

  public static async initialize() {
    if (this.initialized) return;

    // @ts-ignore
    this.pdfjsLib = await import('https://cdn.jsdelivr.net/npm/pdfjs-dist@4.0.379/+esm');
    this.pdfjsLib.GlobalWorkerOptions.workerSrc = 'https://cdn.jsdelivr.net/npm/pdfjs-dist@4.0.379/build/pdf.worker.mjs';

    this.initialized = true;
  }

  public static async loadDocument(url: string): Promise<any> {
    await this.initialize();

    // IMPORTANT: Disabled caching to ensure component isolation
    // Each viewer instance gets its own document to prevent state sharing
    // This fixes issues where multiple viewers interfere with each other
    const loadingTask = this.pdfjsLib.getDocument(url);
    const document = await loadingTask.promise;

    return document;
  }

  public static releaseDocument(_url: string) {
    // No-op since we're not caching documents anymore
    // Each viewer manages its own document lifecycle
  }

  // Cache methods removed to ensure component isolation
  // Each viewer now manages its own document lifecycle
}
