/* ************************************************************************** */
/*                                                                            */
/*                                                        ::::::::            */
/*   parser_get_arguments.c                             :+:    :+:            */
/*                                                     +:+                    */
/*   By: qmennen <qmennen@student.codam.nl>           +#+                     */
/*                                                   +#+                      */
/*   Created: 2025/02/11 16:20:09 by qmennen       #+#    #+#                 */
/*   Updated: 2025/02/28 14:07:23 by whaffman      ########   odam.nl         */
/*                                                                            */
/* ************************************************************************** */

#include "libft.h"
#include "minishell.h"
#include "parser.h"
#include "utils.h"

static int	count_cmds(t_list *list)
{
	int		cmds;
	t_list	*current;
	t_list	*prev;
	t_token	*token;

	cmds = 0;
	current = list;
	prev = NULL;
	while (current)
	{
		token = ((t_token *)current->content);
		if (token->type < 3 && (!prev || ((t_token *)prev->content)->type < 3))
			cmds++;
		prev = current;
		current = current->next;
	}
	return (cmds);
}

static int	parser_should_expand(t_list *value)
{
	t_token	*token;
	int		i;

	token = (t_token *)value->content;
	if (!token)
		return (0);
	i = 0;
	if (token->type == T_DQWORD || token->type == T_WORD)
	{
		while (token->value[i])
		{
			if (token->value[i] == '$'
				&& (expander_character_valid(token->value[i + 1]) || token->value[i + 1] == '?'))
				return (1);
			i++;
		}
		return (0);
	}
	return (0);
}

static char	*parser_process_token(t_minishell *msh, char **args, t_list *prev, t_list *t_head)
{
	char	*str;
	t_token	*token;

	if (!t_head)
		return (NULL);
	token = (t_token *)t_head->content;
	str = NULL;
	if (ft_strcmp(token->value, "") == 0)
		return (NULL);
	if (parser_should_expand(t_head))
		str = expander_parse_string(token->value, msh);
	if (!str)
		str = ft_strdup_safe(msh, token->value);
	if (str && token->type == T_WORD && ft_strchr(str, '"'))
		str = parser_sanitize_string(msh, str, '"');
	else if (str && token->type == T_WORD && ft_strchr(str, '\''))
		str = parser_sanitize_string(msh, str, '\'');
	return (str);
}

char	**parser_get_arguments(t_list *list, t_minishell *msh)
{
	t_list	*current;
	t_list	*prev;
	char	**args;
	int		argc;
	int		i;
	char	*str;
	char	*cat;

	argc = count_cmds(list);
	args = malloc_safe(msh, (argc + 1) * sizeof(char *));
	current = list;
	i = 0;
	prev = NULL;
	while (argc > 0 && current)
	{
		if (((t_token *)current->content)->type < 3 && (!prev || ((t_token *)prev->content)->type < 3))
		{
			str = parser_process_token(msh, args, prev, current);
			if (i > 0 && prev && ft_strcmp(((t_token *)prev->content)->value, "") == 0)
			{
				cat = malloc_safe(msh, ft_strlen(str) + ft_strlen(args[i - 1]) + 1);
				ft_strlcpy(cat, args[i - 1], ft_strlen(args[i - 1]) + 1);
				ft_strlcat(cat, str, ft_strlen(str) + ft_strlen(args[i - 1]) + 1);
				args[i - 1] = cat;
			}
			else if (str)
				args[i++] = str;
			argc--;
		}
		prev = current;
		current = current->next;
	}
	args[i] = 0;
	return (args);
}
