function getDefaultHelpFromSource(hp)
%

%   Copyright 2017-2020 The MathWorks, Inc.

    if ~hp.noDefault 
        hp.needsHotlinking = true;
        
        topicFile = regexprep(hp.fullTopic, "(\.m)?" + filemarker + "[^\\/]*$", '.m');
        existence = exist(topicFile, 'file');
        if existence || ~endsWith(hp.fullTopic, ".m") && isfile(regexprep(hp.fullTopic, '\.\w+$', '.m'))
            hp.helpStr = defaultHelpWithFile(hp, existence, topicFile);
        elseif hp.isBuiltin
            hp.helpStr = defaultBuiltinHelp(hp);
        elseif ~isempty(matlab.internal.language.introspective.hashedDirInfo(hp.topic))
            folderName = matlab.internal.language.introspective.minimizePath(hp.topic, true);
            if folderName ~= "private"
                folderName = ensureHighlighting(hp, folderName);
                hp.helpStr = oneArgDefaultHelp('DefaultFolderHelp', folderName);
                hp.isDir = true;
            end
        end
    end
end

function helpStr = defaultHelpWithFile(hp, existence, topicFile)
    name = hp.objectSystemName;
    localName = false;
    if name == ""
        name = extractAfter(hp.topic, filemarker);
        localName = name ~= "";
    end
    usage = matlab.internal.language.introspective.getUsageFromSource(topicFile, name);
    if name == ""
        [~, name] = fileparts(hp.topic);
    end
    noUsageName = name;
    if hp.wantHyperlinks
        % ensure highlighting by uppering the name
        usage = regexprep(usage, "(^|=)\s*" + name + "\s*(\(|$)", "${upper($0)}");
        name = upper(name);
    end
    if usage == ""
        helpStr = defaultHelpWithoutUsage(hp, existence, name, noUsageName);
    else
        helpStr = defaultHelpWithUsage(hp, name, usage, localName);
    end
end

function helpStr = defaultHelpWithoutUsage(hp, existence, name, noUsageName)
    if hp.isMCOSClass
        helpStr = defaultClassHelp(name);
    elseif hp.isMCOSClassOrConstructor
        helpStr = oneArgDefaultHelp('DefaultNoConstructorHelp', name);
    elseif hp.objectSystemName == ""
        if existence == 4
            helpStr = oneArgDefaultHelp('DefaultSLXHelp', name);
        else
            helpStr = defaultFullPathHelp(noUsageName, hp.fullTopic);
        end
    else
        switch hp.elementKeyword
        case 'properties'
            helpStr = oneArgDefaultHelp('DefaultPropertyHelp', name);
        case 'events'
            helpStr = oneArgDefaultHelp('DefaultEventHelp', name);
        case 'enumeration'
            helpStr = oneArgDefaultHelp('DefaultEnumerationHelp', name);
        case 'methods'
            helpStr = defaultNoArgFunctionHelp(name);
        case 'constructor'
            helpStr = defaultClassHelp(name);
        case 'packagedItem'
            helpStr = defaultFullPathHelp(noUsageName, hp.fullTopic);
        otherwise
            helpStr = defaultPackageHelp(name);
        end
    end
end

function helpStr = defaultHelpWithUsage(hp, name, usage, localName)
    if hp.isMCOSClass
        helpStr = twoArgDefaultHelp('DefaultConstructorHelp', name, usage);
    elseif hp.isMCOSClassOrConstructor
        helpStr = twoArgDefaultHelp('DefaultFullConstructorHelp', name, usage);
    elseif strcmpi(usage, name)
        if localName
            helpStr = oneArgDefaultHelp('DefaultNoArgLocalFunctionHelp', name);
        else
            helpStr = defaultNoArgFunctionHelp(name);
        end
    else
        if localName
            helpStr = twoArgDefaultHelp('DefaultLocalFunctionHelp', name, usage);
        else
            helpStr = twoArgDefaultHelp('DefaultFunctionHelp', name, usage);
        end
    end
end

function helpStr = defaultFullPathHelp(name, fullPath)
    switch helpUtils.sourceFileType(fullPath)
    case "Script"
        helpStr = defaultScriptHelp(name);
    case "Function"
        helpStr = defaultNoArgFunctionHelp(name);
    case "Class"
        helpStr = defaultClassHelp(name);
    case "Unknown"
        [~, ~, ext] = fileparts(fullPath);
        helpStr = oneArgDefaultHelp('DefaultInvalidFileHelp', [name, ext]);
    end
end

function helpStr = defaultBuiltinHelp(hp)
    name = hp.topic;
    name = ensureHighlighting(hp, name);
    hp.fullTopic = hp.topic;
    if hp.isDir
        helpStr = defaultPackageHelp(name);
    else
        [~, whichComment] = which(hp.topic);
        if whichComment == ""
            whichComment = 'function';
        else
            if startsWith(whichComment, hp.topic)
                whichComment = extractAfter(whichComment, ' ');
            end
            hp.objectSystemName = hp.topic;
            hp.isMCOSClass = ~isempty(meta.class.fromName(hp.objectSystemName));
        end
        helpStr = twoArgDefaultHelp('DefaultBuiltinHelp', name, whichComment);
    end
end

function name = ensureHighlighting(hp, name)
    if hp.wantHyperlinks
        % ensure highlighting by uppering the name
        name = upper(name); 
    end
end

function helpStr = oneArgDefaultHelp(key, name)
    helpStr = getString(message("MATLAB:help:" + key, name));    
end

function helpStr = twoArgDefaultHelp(key, name, usage)
    helpStr = getString(message("MATLAB:help:" + key, name, usage));    
end

function helpStr = defaultClassHelp(name)
    helpStr = oneArgDefaultHelp('DefaultClassHelp', name);
end

function helpStr = defaultNoArgFunctionHelp(name)
    helpStr = oneArgDefaultHelp('DefaultNoArgFunctionHelp', name);
end

function helpStr = defaultPackageHelp(name)
    helpStr = oneArgDefaultHelp('DefaultPackageHelp', name);
end

function helpStr = defaultScriptHelp(name)
    helpStr = oneArgDefaultHelp('DefaultScriptHelp', name);
end
