function [hitNums, metricEnum, newBlockCvId, dataMat, codeCovRes, justifiedHitNums, cvd] = getHitCount(indata, ...
    block, metricName, ignoreDescendants, includeAllSizes, covMode)
%

% Copyright 1990-2019 The MathWorks, Inc.

if nargin < 6
    covMode = [];
end

cvprivate('check_cvdata_input', indata);

blkPathObj = block;
if isa(blkPathObj, 'Simulink.BlockPath')
    block = blkPathObj.getBlock(blkPathObj.getLength); %Use the block handle from the visible ref model.
elseif iscell(block) && isa(block{1},'Simulink.BlockPath')
    blkPathObj = block{1};
    block = block{2};
end

[data, blockCvId, newBlockCvId, portIdx, codeInfo]= ...
    SlCov.CoverageAPI.getCvdata(indata, block, covMode);

% Initialize output and early return if no match
hitNums = [];
justifiedHitNums = 0;
metricEnum = [];
dataMat = [];
codeCovRes = [];
cvd  = data;
if ~isempty(blockCvId) && ~ischar(blockCvId) && blockCvId~=0 ...
        && portIdx <= 0 %it doesn't have only sigrange info
    metricEnum =  cvi.MetricRegistry.getEnum(metricName);
    if codeInfo.mode~=SlCov.CovMode.Unknown
        % Code coverage special cases
        if codeInfo.mode==SlCov.CovMode.SFunction
            [hitNums, codeCovRes, justifiedHitNums] = cvi.ReportData.getSFunctionCodeResInfo(data, codeInfo, metricName);
        elseif codeInfo.mode==SlCov.CovMode.SLCustomCode
            [hitNums, codeCovRes, justifiedHitNums] = cvi.ReportData.getSimCustomCodeResInfo(data, codeInfo, metricName);
        else
            [hitNums, codeCovRes, justifiedHitNums] = cvi.ReportData.getECCodeResInfo(data, codeInfo, metricName, ignoreDescendants, covMode);
        end
    else
        % Otherwise, return model coverage data.

        %the block doesn't have coverage, a root variant turned it off
        if cv('get', blockCvId, '.isDormant')
            return;
        end
        
        %For instance specific coverage query, we do the sneaky thing of using
        %the visible model's cvid for calculating the index, and use it to query 
        %the data from the copied model for the hitcount
        if isa(indata,'cv.cvdatagroup') && isa(blkPathObj, 'Simulink.BlockPath')
            data = indata.getRefCvDataForNormalMdlCopy(blkPathObj);
        end
        
      
        [dataMat, hitNums, justifiedHitNums]= cvi.ReportData.getAPIMetricInfo( ...
                data, metricName, blockCvId, ignoreDescendants, ...
                includeAllSizes);
    end
end

end
