from importlib.resources import files

from .. import RELAIData


class VegaChart(RELAIData):
    """Data model for a Vega chart component.

    Args:
        spec (dict): Vega chart specification.
        column_span (int, optional): Number of columns the component should span. Defaults to 12.
    """

    schema_file: str = files("relai_visualization").joinpath("schema/components/vega-chart.schema.json")

    def __init__(self, spec: dict, column_span: int = 12, vega_lite=True) -> None:
        super().__init__(
            componentName="vega-chart",
            columnSpan=column_span,
            vegaLite=vega_lite,
            spec=spec,
        )

    def custom_validation(self) -> None:
        if self.data["vegaLite"] and not self.data["spec"]["$schema"].startswith(
            "https://vega.github.io/schema/vega-lite/"
        ):
            raise ValueError("Invalid Vega-Lite specification.")
        if not self.data["vegaLite"] and not self.data["spec"]["$schema"].startswith(
            "https://vega.github.io/schema/vega/"
        ):
            raise ValueError("Invalid Vega specification.")
