#                🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
#           This file was automatically generated from src/transformers/models/step3p7/modular_step3p7.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_step3p7.py file directly. One of our CI enforces this.
#                🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
# Copyright 2026 The StepFun and HuggingFace Inc. 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 ...processing_utils import ProcessorMixin
from ...utils import auto_docstring


@auto_docstring
class Step3p7Processor(ProcessorMixin):
    """Processor for Step-3.7-Flash.

    Uses :class:`ProcessorMixin.__call__` for the standard image-token expansion
    flow: the image processor splits each image into global + local patch crops,
    then :meth:`replace_image_token` builds the per-image replacement string
    that ``get_text_with_replacements`` substitutes into the text.
    """

    def __init__(self, image_processor, tokenizer=None, chat_template=None, **kwargs) -> None:
        self.image_token = "<im_patch>"
        self.image_token_id = tokenizer.convert_tokens_to_ids(self.image_token) if tokenizer is not None else None
        stride = image_processor.vision_patch_size * image_processor.downsampler_stride
        self.num_image_feature_size = (image_processor.size["height"] // stride) ** 2
        self.num_patch_feature_size = (image_processor.patch_size // stride) ** 2
        self.image_feature_placeholder = self.image_token * self.num_image_feature_size
        self.patch_feature_placeholder = self.image_token * self.num_patch_feature_size
        super().__init__(image_processor=image_processor, tokenizer=tokenizer, chat_template=chat_template, **kwargs)

    @property
    def unused_input_names(self) -> list[str]:
        return ["patch_newline_masks"]

    def replace_image_token(self, image_inputs: dict, image_idx: int, **kwargs) -> str:
        """Return the expanded token string for image *image_idx* (patches + global view)."""
        num_patches = image_inputs["num_local_patches"][image_idx]
        patch_newline_masks = image_inputs.get("patch_newline_masks")
        patch_newline_mask = patch_newline_masks[image_idx] if patch_newline_masks is not None else None
        repl = ""
        for i in range(num_patches):
            repl += f"<patch_start>{self.patch_feature_placeholder}<patch_end>"
            if patch_newline_mask and patch_newline_mask[i]:
                repl += "<patch_newline>"
        repl += f"<im_start>{self.image_feature_placeholder}<im_end>"
        return repl


__all__ = ["Step3p7Processor"]
