function xparam1 = set_default_initial_params(xparam1, Prior, use_prior_mode)

% Fills NaN entries in the initial parameter vector from the prior.
%
%
% INPUTS
% - xparam1        [double]    n×1 vector of initial parameter values (may contain NaN)
% - Prior          [dprior]    prior object carrying p1 (mean) and p5 (mode)
% - use_prior_mode [logical]   if true, replace the entire vector with the prior
%                              modes (default: false)
%
% OUTPUTS
% - xparam1        [double]    n×1 vector with NaN entries replaced
%
% REMARKS
% When xparam1 contains NaN entries (parameters without explicit initial
% values), they are replaced by the corresponding prior means (Prior.p1).
% If use_prior_mode is true, the entire vector is replaced by the prior
% modes (Prior.p5), with any remaining NaN (e.g. uniform priors, which
% have no mode) filled from the prior mean.

% Copyright © 2003-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/>.

if nargin < 3
    use_prior_mode = false;
end

k = find(isnan(xparam1));
if ~isempty(k)
    xparam1(k) = Prior.p1(k);
end

if use_prior_mode
    xparam1 = Prior.p5;
    k = find(isnan(xparam1));
    if ~isempty(k)
        xparam1(k) = Prior.p1(k);
    end
end
