function addedObjects = add(targetObject, options)
% ADD Internal implementation of TARGET.ADD.

%   Copyright 2021-2022 The MathWorks, Inc.

    arguments
        targetObject
        options.UserInstall = false
        options.SuppressOutput = true
    end

    addOptions = targetframework.internal.repository.AddOptions();

    if options.UserInstall
        addOptions.DataSource = targetframework.internal.repository.datasource.Definitions.UserInstall;
    else
        addOptions.DataSource = targetframework.internal.repository.datasource.Definitions.Session;
    end

    returnElementCopiesFromRepository = true;
    tr = targetframework.internal.repository.createTargetRepository(returnElementCopiesFromRepository);

    addResults = tr.add(targetObject, addOptions);
    addedObjects = [addResults([addResults.Added]).Object];

    if isempty(addedObjects)
        addedObjects = target.internal.Object.empty();
    end

    if ~options.SuppressOutput
        target.internal.printResults(addResults);
    end
end
