function b = movevars(a, varargin)
%MOVEVARS Move the specified codistributed table variables to a new location.
%   T2 = MOVEVARS(T1, VARS)
%   T2 = MOVEVARS(T1, VARS, 'Before', LOCATION)
%   T2 = MOVEVARS(T1, VARS, 'After', LOCATION)
%   
%   See also TABLE, CODISTRIBUTED.
%   
%   


%   Copyright 2018-2022 The MathWorks, Inc.

if ~istable(a) && ~istimetable(a)
    % Throw the same error as for in-memory
    msg = message('MATLAB:legacy_two_part:UndefinedFunctionTextInputArgumentsType', mfilename, underlyingType(a));
    error(message('MATLAB:UndefinedFunction', getString(msg)));
end

% a must be codistributed, vars must not
distributedutil.CodistParser.verifyNonCodistributedInputs([{a}, varargin]);
varargin = distributedutil.CodistParser.gatherIfCodistributed(varargin);
if ~isa(a, 'codistributed')
    % Not distributed. Run replicated.
    b = movevars(a, varargin{:});
    return;
end

% Call the in-memory version to do input checking and get the desired
% output table template.
aProto = getTableTemplate(a);
bProto = distributedutil.validateSyntax(@movevars, [{a}, varargin]);

% Let subsref do the real work. We can work out which variables are going
% where using their names, which are guaranteed unique. 
[~,idx] = ismember(bProto.Properties.VariableNames, aProto.Properties.VariableNames);
b = subsref(a, substruct('()', {':',idx}));
