function getBuiltinHelp(hp, justH1)
    topic = hp.getTopic;
    
    resolveFcn = @(topic)getBuiltinHelpForTopic(hp, topic, justH1);
    foundWithImport = matlab.internal.language.introspective.iterateOverImports(resolveFcn, hp.imports, topic, false);

    if ~foundWithImport
        getBuiltinHelpForTopic(hp, topic, justH1);
    end
end

function isResolved = getBuiltinHelpForTopic(hp, topic, justH1)
    mcosResolver = matlab.internal.language.introspective.MCOSMetaResolver(topic);
    mcosResolver.executeResolve();

    isResolved = mcosResolver.isResolved;
    if isResolved
        hp.needsHotlinking = true;
        hp.isTypo = hp.isTypo || ~mcosResolver.isCaseSensitive;

        if mcosResolver.isPackage
            hp.helpStr = getBuiltinPackageContentHelpText(hp, mcosResolver.resolvedMeta);
            hp.isDir = true;
        else
            hp.helpStr = helpUtils.getBuiltinHelpText(mcosResolver.resolvedMeta, justH1);
            if isempty(hp.helpStr)
                if mcosResolver.isConstructor
                    hp.helpStr = helpUtils.getBuiltinHelpText(mcosResolver.resolvedMeta.DefiningClass, justH1);
                elseif mcosResolver.isClass
                    className = extract(mcosResolver.resolvedName, alphanumericsPattern + lineBoundary);
                    constructorIndex = strcmp({mcosResolver.resolvedMeta.MethodList.Name}, className);
                    constructorIndex = find(constructorIndex, 1);
                    if constructorIndex ~= 0
                        hp.helpStr = helpUtils.getBuiltinHelpText(mcosResolver.resolvedMeta.MethodList(1), justH1);
                    end
                end
            end                
        end
        
        hp.isMCOSClass = mcosResolver.isClass;
        hp.isMCOSClassOrConstructor = hp.isMCOSClass || mcosResolver.isConstructor;
        
        if ~isempty(hp.helpStr)
            hp.fullTopic        = mcosResolver.resolvedName;
            hp.objectSystemName = mcosResolver.resolvedName;
        elseif mcosResolver.isMethod
            % Check to see if this is shadowing an ordinary function
            metaName = mcosResolver.resolvedMeta.Name;
            fullTopic = matlab.internal.language.introspective.safeWhich(metaName, true);
            if ~isempty(fullTopic) && ~matlab.internal.language.introspective.isObjectDirectorySpecified(fullTopic)
                helpFunction = matlab.internal.language.introspective.getHelpFunction(fullTopic);
                if ~isempty(helpFunction)
                    hp.helpStr = matlab.internal.language.introspective.callHelpFunction(helpFunction, fullTopic, false);
                    if ~isempty(hp.helpStr)
                        hp.topic = metaName;
                        hp.fullTopic = fullTopic;
                    end
                end
            end
        end
        
        hp.suppressedImplicit = false; 

        if ~hp.helpOnInstance || ~isempty(hp.helpStr)
            hp.isBuiltin = true;
            hp.topic = mcosResolver.resolvedName;
        end
    end
end

%% ------------------------------------------------------------------------
function contentText = getBuiltinPackageContentHelpText(hp, metaInfo)
    contentText = '';
    
    if any({metaInfo.FunctionList.Name} == "Contents")
        % Do not generate Contents if the Contents.m file has been skipped
        return;
    end
    
    packageText = getPackagedMetaInfoDescriptions(hp, metaInfo.PackageList, metaInfo.Name);
    if ~isempty(packageText)
        contentText = [contentText sprintf('\nPackages contained in %s:\n', metaInfo.Name) packageText];
    end

    classText = getPackagedMetaInfoDescriptions(hp, metaInfo.ClassList, metaInfo.Name);
    if ~isempty(classText)
        contentText = [contentText sprintf('\nClasses contained in %s:\n', metaInfo.Name) classText];
    end

    functionText = getPackagedMetaInfoDescriptions(hp, metaInfo.FunctionList, metaInfo.Name);
    if ~isempty(functionText)
        contentText = [contentText sprintf('\nFunctions contained in %s:\n', metaInfo.Name) functionText];
    end
end

%% ------------------------------------------------------------------------
function result = getPackagedMetaInfoDescriptions(hp, metaInfoList, packageName)
    result = '';

    if isempty(metaInfoList) || ~all(isprop(metaInfoList,'Description')) || ~all(isprop(metaInfoList,'Name'))
        return;
    end

    lineItem = cell(1,numel(metaInfoList));

    for i = 1:numel(metaInfoList)        
        name = strsplit(metaInfoList(i).Name,'.');
        name = name{end};
        
        qualifiedName = [packageName '.' name];
        description   = metaInfoList(i).Description;

        link    = createHotlink(hp, qualifiedName, name);
        linkPad = repmat(' ', 1, 30-numel(name));

        lineItem{i} = [link linkPad ' - ' description];
    end

    result = [strjoin(lineItem, newline) newline];
end

%% ------------------------------------------------------------------------
function name = createHotlink(hp, qualifiedName, name)
    if hp.wantHyperlinks
        name = helpUtils.createMatlabLink(hp.command, qualifiedName, name);
    end
end

%   Copyright 2014-2020 The MathWorks, Inc.
