from pathlib import Path

from ssb_pubmd.adapters.storage import LocalFileStorage
from ssb_pubmd.adapters.storage import Storage
from ssb_pubmd.core.document_publisher import ARTICLE_KEY
from ssb_pubmd.interface.component.models import Factbox
from ssb_pubmd.interface.component.models import Highchart

STORAGE: Storage = LocalFileStorage(project_folder=Path.cwd())


class CreateComponentError(Exception): ...


def create_component(
    key: str,
    data: Factbox | Highchart,
) -> None:
    """Oppretter en komponent og printer en Markdown-snippet som kan limes inn i artikkelen.

    :param key: En unik nøkkel for komponenten.
    :param data: Et SSB-dataobjekt.
    """
    if key == ARTICLE_KEY:
        raise CreateComponentError(f"Key '{ARTICLE_KEY}' is reserved.")
    STORAGE.update(key, data)

    div_config = f"{{ #{key} .ssb }}"
    div_content = ""
    if isinstance(data, Factbox):
        div_content = "\nInnhold i faktaboksen skrives her med Markdown-syntaks.\n\n"

    markdown = f"::: {div_config}\n{div_content}:::"

    print(markdown)
