### ===================================================================================================================
###   Get output-item
### ===================================================================================================================
# Copyright ©VIIA 2025
### ===================================================================================================================
###   1. Import modules
### ===================================================================================================================
# General imports
from typing import Dict, List, Union
# References for functions and classes in the haskoning_structural package
from haskoning_structural.output_items import DisplacementOutputItem, StrainOutputItem, StressOutputItem, OutputItem
### ===================================================================================================================
###   2. Function to get the output-item for a result picture in VIIA
### ===================================================================================================================
[docs]def viia_get_output_item(
        project: 'ViiaProject', picture_type: Dict[str, str], output_item_lst: List[OutputItem],
        component: Union[str, int]) -> Union[OutputItem, None]:
    """
    Function to get the correct output-item to be used for the result picture.
    Input:
        - project (obj): VIIA project object containing collections of fem objects and project variables.
        - picture_type (dict): Dictionary with info from yaml to get the correct output-item. The dictionary should
          contain 'output' and 'theoretical_formulation'. Optional is 'component' key. The dictionary should contain:
        - output_item_lst (list): The output item list containing a list of output items used to search for the item
          for result pictures.
        - component (str): The component of the requested output item.
    Output:
        - The instance of OutputItem class that should  be used for the result picture.
    """
    # Mapping names in yaml to OutputItem classes
    output_item_mapping = {
        'displacement': DisplacementOutputItem,
        'crack width': StrainOutputItem,
        'strain': StrainOutputItem,
        'stress': StressOutputItem,
        'distributed force': StressOutputItem,
        'interface relative displacement': StrainOutputItem,
        'interface total traction': StressOutputItem}
    # Check if output name is recognised
    if not isinstance(picture_type, dict) or 'output' not in picture_type or \
            
'theoretical_formulation' not in picture_type:
        raise ValueError(f"ERROR: Request for picture output-item is not correct.")
    if picture_type['output'].lower() not in output_item_mapping:
        raise NotImplementedError(f"ERROR: Request for picture {picture_type['output']} is not available.")
    # Find corresponding output-items from provided list
    output_item = [
        item
        for item in output_item_lst
        if isinstance(item, output_item_mapping.get(picture_type['output'].lower())) and
        item.theoretical_formulation == picture_type['theoretical_formulation'] and item.component == component]
    # Check if relative displacements (to base node) are requested
    if picture_type.get('relative') is True:
        output_item = [
            _output_item for _output_item in output_item
            if hasattr(_output_item, 'options') and _output_item.options and
            hasattr(_output_item.options, 'relative_to_base') and _output_item.options.relative_to_base]
    if len(output_item) == 1:
        return output_item[0]
    if len(output_item) > 1:
        project.write_log(
            f"WARNING: The output-item used for result picture {picture_type['output']} of {picture_type['case']} in "
            f"output file {picture_type['output_filename']} cannot be found, multiple output-items exist for the given "
            f"arguments. The result picture for this output-item will be skipped. Found: {output_item}.")
    project.write_log(
        f"WARNING: The output-item used for result picture {picture_type['output']} of {picture_type['case']} in "
        f"output file {picture_type['output_filename']} cannot be found, the result picture for this output-item "
        f"will be skipped.")
    return None 
### ===================================================================================================================
###   3. End of script
### ===================================================================================================================