% Copyright 2015-2020 The MathWorks, Inc.

function modelcovId = getNormalModeModelcovId(modelcovId)

if ~SlCov.CoverageAPI.isGeneratedCode(modelcovId)
    return
end


modelName = SlCov.CoverageAPI.getModelcovName(modelcovId);
%normalMode
mangledNormalName = SlCov.CoverageAPI.mangleModelcovName(modelName, SlCov.CovMode.Normal, cv('get', modelcovId, '.dbVersion'));
modelcovId2 = SlCov.CoverageAPI.findModelcovMangled(mangledNormalName);

if isempty(modelcovId2) || ...
   ((cv('get', modelcovId2, '.activeRoot') == 0) && ...
    (cv('get', modelcovId2, '.rootTree.child') == 0))
    % Re-create the hierarchy in the Coverage data dictionary.
    try
        if ~bdIsLoaded(modelName)
            load_system(modelName);
        end
        modelH = get_param(modelName,'Handle');
        cvi.TopModelCov.modelInit(modelH, false, true);
        modelcovId2 = get_param(modelH, 'CoverageId');
	% Add the new rootId to the roots hierarchy (needed for cvi.TopModelCov.updateModelHandles())
        cv('compareCheckSum', modelcovId2);
        topModelcovId = cv('get', modelcovId, '.topModelcovId');
        if topModelcovId ~= 0
            cv('set', modelcovId2, '.topModelcovId', topModelcovId);
        end
    catch
    end
end

if ~isempty(modelcovId2) && (cv('get', modelcovId, '.topModelcovId') == 0)
    cv('set', modelcovId, '.topModelcovId', modelcovId2);
end

modelcovId = modelcovId2;
