function B = datetimeSubsrefDot(A, aidx)
% datetimeSubsrefDot performs dot indexing for distributed datetime/duration/calendarDuration.

% Copyright 2019-2020 The MathWorks, Inc.

% Since all local parts should have datetime family arrays with identical
% properties, we should just be able to call subsref on the local part and
% then rebuild the result. The exception is for the DATETIME properties
% Year, Month, Day, Hour, Minute, Second which are exactly equivalent to
% the same named methods.

% Call subsref on the local part. This will throw if the indexing
% expression was incorrect (e.g. bad property name).
if iscell(aidx.subs)
    idx = aidx.subs{1};
else
    idx = aidx.subs;
end
Alp = getLocalPart(A);
Blp = subsref(Alp, substruct('.', idx));

% Check for properties that are actually data accessors
if any(strcmpi(idx, ["year", "month", "day", "hour", "minute", "second"]))
    % Output has same size and distribution as input
    B = codistributed.pDoBuildFromLocalPart(Blp, getCodistributor(A));
else
    % Result is metadata and should be returned on the client
    B = Blp;
end
