#                🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
#           This file was automatically generated from src/transformers/models/granite_docling/modular_granite_docling.py.
#               Do NOT edit this file manually as any edits will be overwritten by the generation of
#             the file from the modular. If any change should be done, please apply the change to the
#                          modular_granite_docling.py file directly. One of our CI enforces this.
#                🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
# Copyright 2026 IBM and The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from itertools import accumulate
from typing import TYPE_CHECKING, Union

from ...image_utils import ImageInput, is_valid_image
from ...processing_utils import MultiModalData, ProcessingKwargs, ProcessorMixin, Unpack
from ...tokenization_utils_base import BatchEncoding, TextInput
from ...utils import auto_docstring


if TYPE_CHECKING:
    from ...tokenization_utils_base import PreTokenizedInput


@auto_docstring
class GraniteDoclingProcessor(ProcessorMixin):
    # rows/cols build the prompt. They are ragged across a batch, so they stay Python lists and are dropped
    # before the processor returns.
    skip_tensor_conversion = ["video_metadata", "text_replacement_offsets", "rows", "cols"]

    def __init__(
        self, image_processor, tokenizer=None, image_seq_len: int = 64, chat_template: str | None = None, **kwargs
    ):
        r"""
        image_seq_len (`int`, *optional*, defaults to 64):
            The number of `<image>` tokens per tile on the coarse path. It is computed as
            `image_seq_len = int(((image_size // patch_size) ** 2) / (scale_factor**2))`.
        """
        self.fake_image_token = "<fake_token_around_image>"
        self.image_token = tokenizer.image_token if hasattr(tokenizer, "image_token") else "<image>"
        self.global_image_tag = "<global-img>"
        self.image_seq_len = image_seq_len
        self.fake_image_token_id = tokenizer.convert_tokens_to_ids(self.fake_image_token)
        self.image_token_id = tokenizer.convert_tokens_to_ids(self.image_token)
        super().__init__(image_processor, tokenizer, chat_template=chat_template, **kwargs)

    @auto_docstring
    def __call__(self, images=None, text=None, **kwargs) -> BatchEncoding:
        r"""
        Returns:
            [`BatchEncoding`]: The tokenized prompts with `pixel_values` and `pixel_attention_mask`, and
            `tile_fine_mask` when `fine_route=True`. The tile grids only build the prompt and are not returned.
        """
        outputs = super().__call__(images=images, text=text, **kwargs)
        # The tile grids only build the prompt
        outputs.pop("rows", None)
        outputs.pop("cols", None)
        return outputs

    def prepare_inputs_layout(self, images=None, text=None, videos=None, audio=None, **kwargs):
        """Idefics3's layout: one list of images per prompt, matched to the `<image>` placeholders."""
        if text is not None:
            if isinstance(text, str):
                text = [text]
            text = list(text).copy()

        if images is not None:
            images = self.image_processor.fetch_images(images)
            if is_valid_image(images):
                images = [[images]]
            elif isinstance(images, (list, tuple)) and is_valid_image(images[0]):
                if text is not None:
                    # Reorganize the images to match the prompts
                    n_images_in_text = [sample.count(self.image_token) for sample in text]
                    cumsum_images_in_text = [0] + list(accumulate(n_images_in_text))
                    split_images = [
                        images[cumsum_images_in_text[i] : cumsum_images_in_text[i + 1]]
                        for i in range(len(n_images_in_text))
                    ]
                    # Append the rest if any, we will error out when validating if they don't match with text
                    if len(images) > cumsum_images_in_text[-1]:
                        images = split_images + [images[cumsum_images_in_text[-1] :]]
                    else:
                        images = split_images
                else:
                    images = [images]

        return images, text, videos, audio

    def validate_inputs(
        self,
        images: ImageInput | None = None,
        text: Union[TextInput, "PreTokenizedInput", list[TextInput], list["PreTokenizedInput"]] = None,
        **kwargs: Unpack[ProcessingKwargs],
    ):
        super().validate_inputs(images, text, **kwargs)

        if text is None and images is None:
            raise ValueError("You must provide either `text` or `images`.")

        if text is not None and images is not None:
            n_images_in_text = [sample.count(self.image_token) for sample in text]
            n_images_in_images = [len(sublist) for sublist in images]
            if n_images_in_text != n_images_in_images:
                raise ValueError(
                    f"The number of {self.image_token} tokens in each prompt should be the same as the number of images passed."
                    f" Found {n_images_in_text} {self.image_token} tokens and {n_images_in_images} images per sample."
                )

    def replace_image_token(self, image_inputs: dict, image_idx: int, **kwargs) -> str:
        image_rows = [row for row_list in image_inputs["rows"] for row in row_list][image_idx]
        image_cols = [col for col_list in image_inputs["cols"] for col in col_list][image_idx]
        # The image processor decides the route; the prompt follows the mask it returned
        tokens_per_tile = self.image_seq_len * 4 if "tile_fine_mask" in image_inputs else self.image_seq_len
        image_tokens = self.image_token * tokens_per_tile
        text_split_images = ""
        for n_h in range(image_rows):
            for n_w in range(image_cols):
                text_split_images += f"{self.fake_image_token}<row_{n_h + 1}_col_{n_w + 1}>{image_tokens}"
            text_split_images += "\n"
        # The thumbnail is only added when the image is split into several tiles
        if image_rows * image_cols > 1:
            text_split_images += (
                f"\n{self.fake_image_token}{self.global_image_tag}{image_tokens}{self.fake_image_token}"
            )
        return text_split_images

    def _get_num_multimodal_tokens(self, image_sizes=None, **kwargs):
        """
        Computes the number of placeholder tokens needed for multimodal inputs with the given sizes.

        Args:
            image_sizes (`list[list[int]]`, *optional*):
                The input sizes formatted as (height, width) per each image.

        Returns:
            `MultiModalData`: A `MultiModalData` object holding number of tokens per each of the provided
            input modalities, along with other useful data.
        """
        vision_data = {}
        if image_sizes is not None:
            num_image_tokens = []
            num_image_patches = []
            fine_route = kwargs.get("fine_route", self.image_processor.fine_route)
            for height, width in image_sizes:
                num_rows, num_cols = self.image_processor.get_tile_grid(height, width, kwargs)
                image_inputs = {"rows": [[num_rows]], "cols": [[num_cols]]}
                if fine_route:
                    image_inputs["tile_fine_mask"] = None
                image_prompt = self.replace_image_token(image_inputs, 0)
                num_image_tokens.append(len(self.tokenizer(image_prompt, add_special_tokens=False)["input_ids"]))
                num_image_patches.append(self.image_processor.get_number_of_image_patches(height, width, kwargs))
            vision_data.update({"num_image_tokens": num_image_tokens, "num_image_patches": num_image_patches})
        return MultiModalData(**vision_data)


__all__ = ["GraniteDoclingProcessor"]
