from __future__ import annotations

from typing import Any, List, TYPE_CHECKING

from kirun_py.function.system.array.abstract_array_function import AbstractArrayFunction
from kirun_py.model.event_result import EventResult
from kirun_py.model.function_output import FunctionOutput

if TYPE_CHECKING:
    from kirun_py.runtime.function_execution_parameters import FunctionExecutionParameters


class AddFirst(AbstractArrayFunction):

    def __init__(self) -> None:
        super().__init__(
            'AddFirst',
            [AbstractArrayFunction.PARAMETER_ARRAY_SOURCE, AbstractArrayFunction.PARAMETER_ANY],
            AbstractArrayFunction.EVENT_RESULT_ARRAY,
        )

    async def internal_execute(
        self, context: FunctionExecutionParameters
    ) -> FunctionOutput:
        source: List[Any] = list(
            context.get_arguments().get(
                AbstractArrayFunction.PARAMETER_ARRAY_SOURCE.get_parameter_name()
            )
        )
        element: Any = context.get_arguments().get(
            AbstractArrayFunction.PARAMETER_ANY.get_parameter_name()
        )

        source.insert(0, element)

        return FunctionOutput([
            EventResult.output_of(
                {AbstractArrayFunction.EVENT_RESULT_NAME: source}
            )
        ])
