function y0 = get_conditional_forecast(type, moment, varargin)
% function y0 = get_conditional_forecast(type, moment, varargin)
% Returns conditional forecasts of variables identified by their name,
% for a given type (conditional or unconditional), and a given moment.
%
% INPUTS:
%   type       [char/string] either 'cond' or 'uncond'
%   moment     [char/string] either 'Point', 'Mean', 'CI_inf', or 'CI_sup'
%   varargin   [cell array]  list of variable names
%
% OUTPUTS
%   y0         [double]      T×n array of conditional forecasts

% Copyright © 2026 Dynare Team
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare.  If not, see <https://www.gnu.org/licenses/>.

global oo_

if ~ismember(type, { 'cond', 'uncond '})
    error('get_conditional_forecast: unsupported value ''%s'' for type argument', type)
end

if ~ismember(moment, { 'Point', 'Mean', 'CI_inf', 'CI_sup' })
    error('get_conditional_forecast: unsupported value ''%s'' for moment argument', moment)
end

if strcmp(moment, 'CI_inf') || strcmp(moment, 'CI_sup')
    values = oo_.conditional_forecast.(type).ci;
    nobs = size(values.(varargin{1}), 2);
else
    values = oo_.conditional_forecast.(type).(moment);
    nobs = length(values.(varargin{1}));
end

y0 = zeros(nobs, length(varargin));

for j = 1:length(varargin)
    v = values.(varargin{j});
    if strcmp(moment, 'CI_inf')
        y0(:,j) = v(1,:);
    elseif strcmp(moment, 'CI_sup')
        y0(:,j) = v(2,:);
    else
        y0(:,j) = v;
    end
end
