function [codeText, matlabCmd, tooltipStr] = ...
    profGetDataForCodeLink(lTraceInfo, lUseAbsPath)
%PROFGETDATAFORCODELINK generates data for code link in profiling report

%   Copyright 2010-2019 The MathWorks, Inc.

if nargin < 2
    lUseAbsPath = true;
end

buildDir = lTraceInfo.getBuildDir;
modelName = lTraceInfo.getOriginalModelName;

argStr = '';
if ~isempty(lTraceInfo) && ...
        ~isempty(lTraceInfo.getFileNames)
    fileNames = lTraceInfo.getFileNames;
    paths = lTraceInfo.getFilePaths;
    allLineNos = lTraceInfo.getLineNumbers;

    sort(paths);
    assert(all(strncmp(paths{1}, paths, numel(paths{1}))), ...
           'All files for a profiled section must be in the same folder.');
    
    tooltipStr = sprintf('%s\n', lTraceInfo.getCodeSectionName);
    tooltipStr = coder.profile.htmlEscape(tooltipStr);
    tooltipStr = strrep(tooltipStr, ' ', '&nbsp;');
    fileNamesStr = '';
    for i=1:length(fileNames)

        lineNos = allLineNos{i};
        
        % Build the line number string, e.g. [10 20]
        lineNosStr = ' [';
        for ii2 = 1:length(lineNos)
            lineNosStr = sprintf('%s%d ', lineNosStr, lineNos(ii2));
        end
        lineNosStr = sprintf('%s]', lineNosStr(1:end-1));
        
        % Build the file name part
        fileName = fileNames{i};
        argStr = sprintf('%s, ''%s'',%s', argStr, fileName, lineNosStr);
        
        fileNamesStr = strtrim(sprintf('%s %s:%s',...
                                       fileNamesStr, fileName, strrep(strtrim(lineNosStr), ' ', '&nbsp;')));
    end
end

if ~isempty(argStr) 
    tooltipStr = sprintf('%s%s', tooltipStr, fileNamesStr);
    
    % prepend the command names
    checksumStr = lTraceInfo.getStoredChecksum;
    argStr = sprintf('''%s'',''%s'', ''%s''%s', buildDir, modelName, checksumStr, argStr);
    
    % build the final command
    matlabCmd = sprintf('coder.profile.ExecTimeHelper.codeHighlight(%s);', argStr);
    icon = coder.profile.ExecTimeHelper.CodeIcon;
    if lUseAbsPath
        icon = fullfile(coder.profile.ExecTimeHelper.ResourcesFolder, icon);
    end
    codeText = sprintf('<img src="%s"/>', icon);
else
    matlabCmd = '';
    codeText = '';
    tooltipStr = '';
end


