function varargout = pagefunBuildOutputs(LPout, outCodistr, numOutPages)
% Helper to build the outputs of pagefun from their local parts, and the
% codistributor and the trailing output dimensions (size)

%   Copyright 2020 The MathWorks, Inc.

varargout = cell(1,max(1,numel(LPout)));

%TODO: make this one communication for all outputs!
for oo=1:nargout
    % Just calculate the global size based on the local part page size.
    pageSz = size(LPout{oo},1:2);
    globalSz = [pageSz, numOutPages];
    thisOutCodistr = outCodistr.hGetCompleteForSize(globalSz);
    
    % Build the output codistributed array(s). If only one page we
    % are building from replicated, otherwise from local parts.
    if globalSz(outCodistr.Dimension)==1
        varargout{oo} = codistributed(LPout{oo}, thisOutCodistr);
    else
        varargout{oo} = codistributed.build(LPout{oo}, thisOutCodistr);
    end
end
end