function libs = getLibrary(libNames)

    arguments
        libNames string = [];
    end

    import bioinfo.pipeline.internal.library.isValidLibraryFile
    import bioinfo.pipeline.library.loadLibrary
    
    libFolder = bioinfo.pipeline.internal.library.libraryFolder;
    
    libFiles = string({dir(libFolder).name});
    libFiles = libFiles(endsWith(libFiles, '.mat'));

    validLibs = arrayfun(@(f) isValidLibraryFile(fullfile(libFolder, f)), libFiles);
    validLibFiles = libFiles(validLibs);

    validLibNames = extractBefore(validLibFiles, ".mat");

    if nargin == 0
        retrieveLibFiles = fullfile(libFolder, validLibFiles);
    else
        invalidLibNames = setdiff(libNames, validLibNames);
        if numel(invalidLibNames) > 0
            error(message("bioinfo:Library:getLibrary:InvalidLibraryName", strjoin(invalidLibNames, ', ')));
        end
        retrieveLibFiles = fullfile(libFolder, libNames + ".mat");    
    end

    libs = arrayfun(@loadLibrary, retrieveLibFiles);

    if isempty(libs)
        libs = bioinfo.pipeline.library.Library.empty;
    end
end

% Copyright 2023 The MathWorks, Inc.

