function [sid, modelName] = getSidFromModelObjectData(sldvData, modelObj)
%

%   Copyright 2010-2020 The MathWorks, Inc.
    
    %This is the actual analyzed object sid.
    sid = '';
    modelName = '';

    if isfield(modelObj, 'modelScope') && strcmp(modelObj.modelScope, 'ObserverReference')
    % If model object corresponds to an Observer model, return the Observer
    % model name
        sid = modelObj.designSid;
        modelName = Sldv.ReportUtils.getModelNameFromSid(sid);
        return;
    elseif isfield(modelObj, 'designSid') && ~isempty(modelObj.designSid) && ...
        isempty(modelObj.replacementSid)
        sid = modelObj.designSid;
        if Sldv.DataUtils.isXilSldvData(sldvData)
            modelName = Sldv.ReportUtils.getModelNameFromSid(sid);
            if isempty(modelName)
                % Special case when the sid is the model name
                modelName = sid;
            end
        else
            modelName = sldvData.ModelInformation.Name;
        end
    elseif isfield(modelObj, 'sid') &&  ~isempty(modelObj.designSid)

        replacementSid = modelObj.replacementSid;
        allReplacementInfoInTheModel = sldvData.AnalysisInformation.ReplacementInfo;
        replacementInfoOfModel = allReplacementInfoInTheModel(strcmp(replacementSid, {allReplacementInfoInTheModel.replacementSid}));

        if isempty(replacementInfoOfModel)
            %g1802696 : Replacement info may be missing in some cases
            %For ex, from inlining export function model
            %references in generated Scheduler model. Safe to ignore
            %such cases.
            return;
        end

        repRuleInfo = [replacementInfoOfModel.RepRuleInfo];
        replacementRuleName = {repRuleInfo.RuleName};

        %          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',...

        if any(cell2mat(regexp( replacementRuleName, 'blkrep_rule_modelref_\w*normal')))
            sid = modelObj.designSid;
            modelName = Sldv.ReportUtils.getModelNameFromSid(sid);
        else
            sid = modelObj.sid;
            modelName = sldvData.ModelInformation.ReplacementModel;
        end
    end
end