import { CompactRecipeDto } from "../api/dtos/CompactRecipeDto.js";
import { RecipeEntity } from "../entities/RecipeEntity.js";
import { CompactRecipeDtoEntityMapper } from "../mappers/CompactRecipeDtoEntityMapper.js";
import { RecipeRepository } from "../repositories/RecipeRepository.js";

/**
 * Responsible for loading recipe header data
 */
export class CompactRecipeHandler {
    constructor(
        private repository: RecipeRepository,
        private mapper: CompactRecipeDtoEntityMapper
    ) {}

    /**
     * Load list of all recipes
     * @returns List of all recipes
     */
    async getAllCompactRecipes() {
        const recipeEntities: RecipeEntity[] = await this.repository.findAll();
        // @todo load instruction steps, ingredient groups and ingredients before mapping!
        let recipeDtos: CompactRecipeDto[] = [];
        recipeEntities.forEach(recipeEntity => {
            recipeDtos.push(this.mapper.toDto(recipeEntity));
        });
        return recipeDtos;
    }

    /**
     * Get all recipes matching search
     * 
     * Recipe title must contain type string
     * @todo Full text search??

     */
    async getMatchingRecipes(searchString : string){
        if(!searchString || searchString.length===0){
            // get all 
            return this.getAllCompactRecipes();
        } else {
            return this.repository.findCompactRecipeBySearch(searchString);
        }
    }
}