function [startUpVariantParams, allVarsUsage] = findStartUpVariantParams(modelH, options)
% @Input: existing sldv_params which might contain tunable sldv parameters
% and model handle
% @Output: update sldv_params with variant parameters(if they exist) and
% all vars in model found using Simulink.findVars()

%   Copyright 2021-2023 The MathWorks, Inc.

    allVarsUsage = [];
    startUpVariantParams = {};

    modelName = get_param(modelH, 'name');

    if ~slavteng('feature','StartupVariantSLDV') || ~isequal(options.AnalyzeAllStartupVariants, 'on')
        return;
    end
    
    % Fix for g2556128
    variantControlVars = Sldv.utils.findVariantControlVars(modelName);
    if isempty(variantControlVars)
        return;
    end

    variantParamNames = cell(1,numel(variantControlVars));
    for idx = 1:numel(variantControlVars)
        variantParamNames{idx} = variantControlVars(idx).Name;
    end


    %% Code below this is temporary and needs to be removed
    % As the check below would be unnecessary after we move to publish
    % strategy for populating params in testComp from the current on-demand
    % strategy
    % Remove the code below after g2578579 is submitted
    simStatus = get_param(modelH, 'SimulationStatus');
    isCompiled = strcmp(simStatus, 'paused') || strcmp(simStatus, 'initializing');

    if isCompiled
        return;
    end
    %% Code above this is temporary and needs to be removed
    % 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
    reportCompileErrors = false;
    [status, allVarsUsageMap] = Sldv.ParameterTuning.Manager.getAllParamVarsFromModel(modelH, reportCompileErrors);

    paramVarLists = values(allVarsUsageMap);
    for idx = 1:length(paramVarLists)
        allVarsUsage = cat(1, allVarsUsage, paramVarLists{idx});
    end

    if ~status || isempty(allVarsUsage)
        return;
    end

    startUpVariantParams = Sldv.utils.getStartUpVariantParameters(allVarsUsage);

    % Fix for g2556128
    % There are variant control vars whose usage might be in startup
    % variant block but are not really startup variant parameters
    % Therefore, we need to filter the fake ones out.
    % Simulink.VariantManager.findVariantControlVars gives us the correct
    % list of startup variant params
    startUpVariantParams = intersect(startUpVariantParams, variantParamNames);
end
