function  ssid = mapFromHarnessSID_internal(ssid, rootId, ownerBlock, analyzedModel)
% cvdata.mapFromHarnessSID_internal

%   Copyright 2020 The MathWorks, Inc.
    
    %is subsystem harness
    if ~isempty(ownerBlock) &&  ...
       ~isempty(rootId) && ... %don't support CovFilter - setFilterBeforeSim
       ~isempty(cv('GetRootPath', rootId))
        modelName = bdroot(ownerBlock);
        res = Simulink.harness.internal.getActiveHarness(modelName);
        %any harness open
        if ~isempty(res) 
            ssid = mapSSIDFromHarnessToModel(ownerBlock, analyzedModel, ssid);
        end
    end
end
    
%================================================    
function blockBlockSID = mapSSIDFromHarnessToModel(ownerBlock, analyzedModel, harnessBlockSID)
    blockBlockSID = harnessBlockSID;
    harnessObject = SlCov.FilterEditor.getObject(harnessBlockSID);
    if isempty(harnessObject)
        return;
    end

    %how about this
    %newSID = Simulink.harness.internal.sidmap.getExtractedModelObjectSID(keys{idx},subSystemBlockH,extractModelName);
    if contains(class(harnessObject), 'Stateflow.')
        chartObj = harnessObject.Chart;
        newChartSSID = mapSSIDFromHarnessToModel(ownerBlock, analyzedModel, Simulink.ID.getSID(chartObj));
        newChartModelObject = cvi.TopModelCov.getObject(newChartSSID);
        chart = newChartModelObject.find('-isa','Stateflow.Chart');
        newModelObject = chart.find('SSIdNumber', harnessObject.SSIdNumber);
        blockBlockSID  = Simulink.ID.getSID(newModelObject);
    else
        harnessBlock = harnessObject.getFullName;
        if contains(harnessBlock, analyzedModel)
             modelBlock = [ownerBlock harnessBlock(numel(analyzedModel) + 1 : end)];
             blockBlockSID  = Simulink.ID.getSID(modelBlock);
        end
    end
end