"""Integrate Betty with `Leaflet.js <https://leafletjs.com/>`_."""

from __future__ import annotations

from pathlib import Path
from typing import TYPE_CHECKING, Self, final, override

from betty.extension import ExtensionDefinition
from betty.locale.localizable.gettext import _
from betty.plugins.asset.maps import Maps as MapsAsset
from betty.plugins.extension.maps.jobs import _GeneratePlacePreviews
from betty.plugins.extension.webpack import Webpack
from betty.plugins.extension.webpack.build import EntryPointProvider
from betty.project import Project
from betty.project.generate import Generator
from betty.requirement import require
from betty.service.factory import Manufacturable

if TYPE_CHECKING:
    from collections.abc import Sequence

    from betty.job.scheduler import Scheduler


@final
@ExtensionDefinition(
    "maps",
    label="Maps",
    description=_("Display interactive maps"),
    requires={MapsAsset, Webpack},
)
class Maps(Generator, EntryPointProvider, Manufacturable):
    """
    .. plugin:: extension:maps.
    """

    def __init__(self, *, project: Project):
        super().__init__()
        self._project = project

    @override
    @classmethod
    @require(Project)
    async def new(cls, project: Project, /) -> Self:
        return cls(project=project)

    @override
    @classmethod
    def webpack_entry_point_directory_path(cls) -> Path:
        return Path(__file__).parent / "webpack"

    @override
    def webpack_entry_point_cache_keys(self) -> Sequence[str]:
        return ()

    @override
    async def generate(self, scheduler: Scheduler) -> None:
        await scheduler.add(_GeneratePlacePreviews(project=self._project))
