function found = getHelpForTopics(hp, topics, isOperator, forceContents)
    if isempty(topics)
        found = false;
    elseif isscalar(topics) && ~forceContents
        found = getSingleTopicHelp(hp, topics{1}, isOperator, false);
        hp.displayBanner = found;
    else
        found = getMultiTopicHelp(hp, topics, isOperator, forceContents);
    end
end

function found = getSingleTopicHelp(hp, topic, isOperator, justH1)
    if isOperator
        [isNotFunctionOperator, topic] = matlab.internal.language.introspective.isOperator(topic, true);
        if isNotFunctionOperator 
            found = false;
            return;
        end
    end
    hp.inputTopic = topic;
    hp.topic = topic;
    hp.fixTypos = false;
    hp.getTopicHelpText(justH1);
    found = ~isempty(hp.fullTopic);
    if ~found
        hp.topic = '';
        hp.helpStr = '';
    end
end

function found = getMultiTopicHelp(hp, topics, isOperator, forceContents)
    collect = struct('topic', num2cell(topics));
    [collect(:).help] = deal('');
    [collect(:).hasHelp] = deal(false);
    [collect(:).isDir] = deal(false);
    for i = 1:numel(topics)
        topic = topics{i};
        helpStr = '';
        isDir = false;
        if forceContents
            helpStr = getFullTopicHelp(topic);
        else
            hp2 = helpUtils.helpProcess(1,0);
            found = getSingleTopicHelp(hp2, topic, isOperator, true);
            if found
                helpStr = hp2.helpStr;
                isDir = hp2.isDir;
            end
        end
        if ~isempty(helpStr)
            collect(i).hasHelp = true;
            collect(i).isDir = isDir;
            collect(i).help = helpStr;
        end
    end
    collect = collect([collect.hasHelp]);
    found = ~isempty(collect);
    if found
        if isscalar(collect) && ~forceContents
            topic = char(collect.topic);
            getSingleTopicHelp(hp, topic, isOperator, false);
            hp.displayBanner = true;
        else
            fullTopics = [collect.topic];
            topics = fullTopics;
            if forceContents
                topics = regexpi(topics, '[^@+\\/][^\\/]*?(\.mat$)?(?=(\.\w+)?$)', 'match', 'once');
            end
            h1Lines = cellfun(@makeH1, {collect.help}, topics, 'UniformOutput', false);

            paddedTopics = pad(topics, 'left');
            if hp.wantHyperlinks
                if isOperator
                    linkText = " " + topics + " ";
                else 
                    linkText = topics;
                end
                linkedTopics = cellfun(@(link, text)string(helpUtils.createMatlabLink(hp.command, link, text)), fullTopics, linkText);
                paddedTopics = regexp(paddedTopics, '^\s*', 'match', 'once', 'emptymatch') + linkedTopics;
            end
            hp.helpStr = sprintf('%s - %s\n', string([paddedTopics; h1Lines]));
        end
    end
end

function helpStr = getFullTopicHelp(topic)
    helpStr = '';
    helpFunction = matlab.internal.language.introspective.getHelpFunction(topic);
    if ~isempty(helpFunction)
        helpStr = matlab.internal.language.introspective.callHelpFunction(helpFunction, topic, true);
    end
    if isempty(helpStr)
        hp2 = helpUtils.helpProcess(1,0);
        hp2.topic = topic;
        hp2.inputTopic = topic;
        hp2.fullTopic = topic;
        nameResolver = matlab.internal.language.introspective.resolveName(topic, '', false);
        hp2.elementKeyword = nameResolver.elementKeyword;
        if ~isempty(nameResolver.classInfo)
            hp2.extractFromClassInfo(nameResolver.classInfo);
        end
        if hp2.isDir
            hp2.getFolderHelp(true);
        else
            hp2.getDefaultHelpFromSource;
        end
        helpStr = hp2.helpStr;
    end
end

function h1 = makeH1(helpStr, topic)
    h1 = strip(helpStr);
    h1 = regexprep(h1, '\s+', ' ');
    h1 = regexprep(h1, "^" + regexptranslate('escape', topic) + "\>(\.\w+)?\s*(-\s*)?", '', 'ignorecase');
end

% Copyright 2018-2020 The MathWorks, Inc.
