from functools import cache
import json
from pathlib import Path


def extract_photo_list_from_raw_page(md_body: str) -> tuple[str, str | None]:
    sp = md_body.split("---")

    if len(sp) == 1:
        return sp, None

    return "---".join(sp[:-1]), sp[-1]


_manifest: None | dict = None


def load_manifest_from_disk(site_base_path: Path | str) -> dict:
    global _manifest

    if _manifest is not None:
        return _manifest

    try:
        with open(Path(site_base_path) / "photos.manifest.json") as f:
            _manifest = json.load(f)
            return _manifest
    except FileNotFoundError:
        return {}


def get_photo_info(site_base_path: Path | str, path: str) -> dict:
    manifest = load_manifest_from_disk(site_base_path)
    return manifest.get(path, {})


def get_full_filename(site_config: dict, fname: str) -> str:
    return site_config["photos"]["baseHostedURL"].rstrip("/") + "/" + fname
