function [model, updatedPath, flag] = updateDesignSIDsForPath(sldvData, path)
% Decides if the  Highlighting should be done on the design model or
% replacement model.
%%
% Algorithm:
%
% Outputs : 
% model - designModel/updatedModel as required.
% updatedPath - the SIDs of the path would be updated to the
%               designSIDs/replacementSIDs
% flag - Implies that the replacement model needs to be used. 
%        false would imply the design model would be used. 
% 
% Subsystem workflows would also display the highlighting in the design model.
% Hence no separate checks are included for that. 

% The function gets called in path highlighting workflows. 
%%
updatedPath = path;
model = sldvData.ModelInformation.Name;
%% Filters the Model Objects contained in the path
[~,modelObjectIndices, pathIndices] = intersect({sldvData.ModelObjects.sid}, ...
    {path.modelObj}, 'stable');

modelObjectsToBeConsidered = sldvData.ModelObjects(modelObjectIndices);
% 1. Check presence of replacementSid
% 2. Check if the replacement was effected by Model Reference rule.
% Note: We do not check if modelObjectsToBeConsidered is empty
% as there will be at least one block which is
% common(Inspection block)
if isempty([modelObjectsToBeConsidered.replacementSid])
    flag = false;  
else
    replacementSidsToConsider = {modelObjectsToBeConsidered.replacementSid};
    
    replacementSidsToConsider = replacementSidsToConsider(~cellfun('isempty',replacementSidsToConsider));
    
    allReplacementInfoInTheModel = sldvData.AnalysisInformation.ReplacementInfo;
    allReplacementSidInModel = {allReplacementInfoInTheModel.replacementSid};
    
    allReplacementInfoAppiledInThePath = allReplacementInfoInTheModel...
        (cellfun(@(repl_Sid)any(strcmp((repl_Sid), allReplacementSidInModel)),...
        unique(replacementSidsToConsider)));
    
    appliedReplacementRuleInformation =  [allReplacementInfoAppiledInThePath.RepRuleInfo];
    %        Checks if the rule name is any of the following
    %          'blkrep_rule_modelref_param_normal',...
    %          'blkrep_rule_modelref_added_param_normal',...
    %          'blkrep_rule_modelref_normal',...
    % There are cases when regexp call returns a scalar. Using cellfun
    % returns an error in those cases. Trying to use 'forceCellOutput'
    % flag to the function.
    flag = any(cellfun(@isempty, ...
        regexp( {appliedReplacementRuleInformation.RuleName}, ...
        'blkrep_rule_modelref_\w*normal', ...
        'forceCellOutput')));

    
end
if ~flag
    updatedPath = [];
    for iterator = 1:length(pathIndices)
        updatedPath(iterator).modelObj = ...
            sldvData.ModelObjects(modelObjectIndices(iterator)).designSid; %#ok<AGROW>
        updatedPath(iterator).port = path(pathIndices(iterator)).port; %#ok<AGROW>
    end
else
model = sldvData.ModelInformation.ReplacementModel;
open_system(model);
end

end


