function A = hSubsasgnSquareEmpty(A, S)
%HSUBSASGNSQUAREEMPTY hidden helper for subsasgn to []
%   
%   A = HSUBSASGNSQUAREEMPTY(A, subs) performs a subsasgn on A using the 
%   subscripts in SUBS and square empty ([]) as the right-hand side.
%   


%   Copyright 2019-2020 The MathWorks, Inc.

% We only support a single level of indexing for now, unless the input is a
% table.
if numel(S)>1
    if istable(A) || istimetable(A)
        % If multiple levels of indexing, first get the variable and update it.
        Avar = subsref(A, S(1));
        B = hSubsasgnSquareEmpty(Avar, S(2:end));
        % Assign the result back into the variable.
        A = subsasgn(A, S(1), B);
        % Make sure to flush any metadata
        A.Metadata.TableTemplate.reset();
        return
    else
        error(message('parallel:distributed:SubsasgnBadIndexType'))
    end
end


% Pick a method depending on datatype of A.
asgnEmptyMethods = iGetAssignEmptyMethods(A);

switch S(1).type
    case '()'
        method = asgnEmptyMethods.Parens;
    case '{}'
        method = asgnEmptyMethods.Braces;
    case '.'
        method = asgnEmptyMethods.Dot;
end
A = method(A, S);

end

function subsdeleteMethods = iGetAssignEmptyMethods(obj)
% TODO: these should probably be in some kind of adaptor class, as per tall arrays,
% so that we can use OO principles instead of switchyards.

% Get all deletion subsasgn methods as function handles depending on the underlying
% class
switch underlyingType(obj)
    case {'table', 'timetable'}
        % Table has its own rules and error messages.
        subsdeleteMethods.Parens = @subsdeleteTabularParens;
        subsdeleteMethods.Braces = @subsdeleteTabularBraces;
        subsdeleteMethods.Dot    = @subsdeleteTabularDot;
    otherwise
        % All other types follow standard rules. It's only deleting
        % subsasgn if (), for others just go through the regular API.
        subsdeleteMethods.Parens = @subsdeleteParens;
        subsdeleteMethods.Braces = @(A,S) subsasgnBraces(A,S,[]);
        subsdeleteMethods.Dot    = @(A,S) subsasgnDot(A,S,[]);
end
end
