// frontend/src/lib/loadQuill.js
// Shared Quill CDN loader — ensures the script is only injected once across
// all modals (ComposeEmailModal, MailSettingsModal, etc.).
// Calls cb() immediately if Quill is already on window, otherwise queues it.

let _ready = false
let _callbacks = []

export function loadQuill(cb) {
  if (_ready || window.Quill) { _ready = true; cb(); return }
  _callbacks.push(cb)
  if (document.getElementById('__quill_js__')) return // already injecting

  const link = document.createElement('link')
  link.id   = '__quill_css__'
  link.rel  = 'stylesheet'
  link.href = 'https://cdn.quilljs.com/1.3.7/quill.snow.css'
  document.head.appendChild(link)

  const script  = document.createElement('script')
  script.id     = '__quill_js__'
  script.src    = 'https://cdn.quilljs.com/1.3.7/quill.min.js'
  script.onload = () => {
    _ready = true
    _callbacks.forEach(fn => fn())
    _callbacks = []
  }
  document.head.appendChild(script)
}
