import type { DocParagraphStylesCatalog } from "@/lib/drive/docs-paragraph-styles"
import { defaultDocumentParagraphStyles } from "@/lib/drive/docs-paragraph-styles"

let activeCatalog: DocParagraphStylesCatalog = defaultDocumentParagraphStyles()

export function setActiveParagraphStyleCatalog(catalog: DocParagraphStylesCatalog) {
  activeCatalog = catalog
}

export function getActiveParagraphStyleCatalog(): DocParagraphStylesCatalog {
  return activeCatalog
}

export type ParagraphStyleCatalogListener = (catalog: DocParagraphStylesCatalog) => void

const listeners = new Set<ParagraphStyleCatalogListener>()

export function subscribeParagraphStyleCatalog(listener: ParagraphStyleCatalogListener) {
  listeners.add(listener)
  return () => listeners.delete(listener)
}

export function notifyParagraphStyleCatalog(catalog: DocParagraphStylesCatalog) {
  activeCatalog = catalog
  for (const listener of listeners) listener(catalog)
}
