function [status, allParamVarsMap] = getAllParamVarsFromModel(modelH, reportCompileErrors)
% @Input: Model Handle
% @Output: All the vars we get from model 

%   Copyright 2021-2023 The MathWorks, Inc.

    if nargin < 2
        reportCompileErrors = true;
    end
    status = 0;
    
    modelName = get_param(modelH, 'Name');
    if Simulink.internal.useFindSystemVariantsMatchFilter()
        refMdls = find_mdlrefs(modelName, 'IncludeProtectedModels',false,...
            'MatchFilter', @Simulink.match.activeVariants);
    else
        refMdls = find_mdlrefs(modelName, 'IncludeProtectedModels',false,...
            'Variants','ActiveVariants');
    end
    loadedModels = {};
    allParamVarsMap = containers.Map;

    % Inputs for Sldv.xform.RepMdlRefBlkTreeNode.genReferencedVars
    getSignals = false;
    filterInstanceSpecificParams = false;

    for idx=1:length(refMdls)
        currentMdl = refMdls{idx};
        isLoaded = bdIsLoaded(currentMdl);
        if ~isLoaded
            try
                Sldv.load_system(currentMdl);
                loadedModels{end+1} = currentMdl; %#ok<AGROW>
                isLoaded = true;
            catch Mex %#ok<NASGU>
                isLoaded = false;
            end
        end
        if isLoaded
            blkDiagramCompiled = false;
            try
                currentMdlH = get_param(currentMdl,'Handle');
                Sldv.xform.MdlInfo.compileBlkDiagram(currentMdlH,false);
                blkDiagramCompiled = true;
                % scope is 'global' implying that it could be 'base workspace' or 'data
                % dictionary'.
                allParamVars = Sldv.xform.RepMdlRefBlkTreeNode.genReferencedVars(currentMdlH, 'global', getSignals, filterInstanceSpecificParams);
                if slavteng('feature','ModelWorkspaceParamTun') % Populate Model workspace parameters.
                    allParamVars = cat(1, allParamVars, ...
                        Sldv.xform.RepMdlRefBlkTreeNode.genReferencedVars(currentMdlH, 'model', getSignals, filterInstanceSpecificParams));
                end
                Sldv.xform.MdlInfo.termBlkDiagram(currentMdl, blkDiagramCompiled);
                allParamVarsMap(currentMdl) = allParamVars;
            catch Mex
                Sldv.xform.MdlInfo.termBlkDiagram(currentMdl, blkDiagramCompiled);
                if reportCompileErrors
                    % When we compile for start up variants we might not
                    % want to report errors from the compile. That should
                    % be handled somewhere else by SLDV
                    sldvshareprivate('local_error_dlg', Mex.message);
                end    
                return;
            end
        end
    end

    status = 1;
end

