function add(targetObjectInstances, varargin)
%TARGET.ADD    Add target feature object to memory.
%   TARGET.ADD(targetFeatureObject) adds the specified target feature 
%   object to memory. By default, the target data is available only for the
%   current session.
%
%   TARGET.ADD(targetFeatureObject, 'UserInstall', dataPersistence)
%   controls persistence of target data over sessions. If dataPersistence =
%   false (default), target data is available only for the current session.
%   If dataPersistence = true, target data persists over multiple sessions.
%
%   See also TARGET.CREATE, TARGET.GET, TARGET.REMOVE

%   Copyright 2018-2019 The MathWorks, Inc.

    % Inputs should be an array, cell array or scalar value containing 1 or
    % more target.Object instances
    validateattributes(targetObjectInstances, {'target.Object', 'cell'}, {'vector'}, 'target.add');

    % Process cell
    if iscell(targetObjectInstances)
        invalidElements = cellfun(@(e) ~isa(e, 'target.Object') || ~isscalar(e), targetObjectInstances);
        if any(invalidElements)
            error(message('targetrepository:Command:CellArrayMustContainTargetObjects'));
        end

        targetObjectInstances = [targetObjectInstances{:}];
    end
    
    % Process varargin, these should be name - value pairs providing
    % options to the add call.
    parser = inputParser();
    parser.FunctionName = 'target.add';
    
    parser.addParameter('UserInstall', false, @(v) validateattributes(v, {'logical'}, {'scalar'}, 'target.add'));
    
    parser.parse(varargin{:});
    
    arguments = parser.Results;

    % Now we have an array of target objects we can add them to the target
    % repository
    
    options = targetrepository.AddOptions();
    options.AddHiddenAssociations = true;
    
    if arguments.UserInstall
        options.DataSource = targetrepository.DataSource.UserInstall;
    else
        options.DataSource = targetrepository.DataSource.Session;
    end
    
    tr = targetrepository.create();
    tr.add(targetObjectInstances, options);
end

