function A = subsasgnDatetimeDot(A, aidx, B)
% Dot assignment for distributed datetime/durection/calendarDuration (not currently supported)

% 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.

Alp = getLocalPart(A);
if iscell(aidx.subs)
    idx = aidx.subs{1};
else
    idx = aidx.subs;
end

% Check for properties that are actually data accessors
if isa(Alp, "datetime") ...
    && ischar(idx) ...
    && any(strcmpi(idx, ["year", "month", "day", "hour", "minute", "second"]))
    % e.g. D.Year = Y. RHS must have same size as LHS and must be
    % redistributed to match, or must be scalar. We can use element-wise op
    % to achieve that.
    assignFcn = @(a,b) iAssign(a, idx, b);
    A = codistributed.pElementwiseOp(assignFcn, A, B);
else
    % Setting property. Just set it on all local parts and rebuild the
    % output to be identical to the input. This will error if the property
    % is invalid or is (co)distributed.
    Alp = subsasgn(Alp, substruct('.', idx), B);
    A = codistributed.pDoBuildFromLocalPart(Alp, getCodistributor(A));
end
end

% Helper to assign an array into a property
function x = iAssign(x, prop, y)
xidx = substruct('.', prop);
x = subsasgn(x, xidx, y);
end
