function tc = timetableCollection(tc, varargin)
%TIMETABLECOLLECTION Build a tall timetableCollection
%   TTC = TIMETABLECOLLECTION(TCELL) creates a tall timetableCollection from
%   a tall cell array TCELL. All non-empty elements of TCELL must contain
%   timetables.
%
%   Creating a tall timetableCollection from one or more tall timetables
%   is not supported.
%
%   See also tall, timetableCollection.

% Copyright 2019 The MathWorks, Inc.

% If the first input is neither a cell array nor a timetable, let the
% in-memory version throw the right error.
adap = matlab.bigdata.internal.adaptors.getAdaptor(tc);
if ~isempty(adap.Class) && ~ismember(adap.Class, ["cell", "timetable"])
    tall.validateSyntax(@timetableCollection,[{tc}, varargin],'DefaultType', 'double');
end


% If the inputs are timetables (which is supported in-memory), we need a
% specific error.
if nargin>1 || adap.Class == "timetable"
    error(message("MATLAB:bigdata:timetableCollection:ConstructFromTimetables"));
end

% The only remaining syntax should be a single input that is a tall cell
% array. We will need to check that all elements are timetables or missing.
tc = tall.validateTypeWithError(tc, mfilename, 1, "cell", "MATLAB:bigdata:timetableCollection:MustBeCell");

% Now we know we have cells, attempt the conversion. This will error if any
% cell is not a timetable.
inAdap = tc.Adaptor;
tc = elementfun(@timetableCollection, tc);

% Output is now a timetableCollection but with identical size to the input.
outAdap = matlab.bigdata.internal.adaptors.TimetableCollectionAdaptor();
tc.Adaptor = copySizeInformation(outAdap, inAdap);

end