# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the terms described in the LICENSE file in
# the root directory of this source tree.

from abc import ABC, abstractmethod

from ..datasets.interface import Row
from ..utils.utils import map_with_progress


class IPreprocessor(ABC):
    def __init__(self, preprocessor_id: str):
        self.preprocessor_id = preprocessor_id

    @abstractmethod
    def preprocess_row(self, row: Row) -> Row:
        pass

    def preprocess(self, rows: list[Row]) -> list[Row]:
        def preprocess_row_wrapper(row: Row) -> Row:
            row_result = self.preprocess_row(row)
            row_result["subset"] = row.get("subset")
            return row_result

        return map_with_progress(preprocess_row_wrapper, rows, show_progress=False)

    def __str__(self) -> str:
        return f"{self.__class__.__name__}({self.preprocessor_id})"
