function [groupingData,groupVars] = parseGroupVars(groupVarsIn,tableFlag,messageIdent,T_)
%MATLAB Code Generation Library Function

%   Copyright 2021 The MathWorks, Inc.
%#codegen

coder.internal.prefer_const(groupVarsIn,tableFlag,messageIdent);

ONE = coder.internal.indexInt(1);

if tableFlag
    % Extract grouping variables
    [groupVarsInd,T] = checkDataVariables(T_,groupVarsIn,messageIdent,'Group');
    varNamesT = T.Properties.VariableNames;

    % groupVarsInd is always numerical
    groupVars = {varNamesT{groupVarsInd}};

    groupingData = cell(1,numel(groupVarsInd));

    coder.unroll();
    for jj = ONE:numel(groupVarsInd)
        groupingData{jj} = T{:,groupVarsInd(jj)};
        % Check data is column vector since mgrp2idx allows row vectors also
        coder.internal.assert(iscolumn(groupingData{jj}), 'MATLAB:findgroups:GroupingVarNotVector');
    end
else
    groupVars = groupVarsIn;
    if nargin == 4
        T = T_;
        if iscell(T) && ~iscellstr(T) && strcmp(messageIdent,'groupsummary') %#ok<ISCLSTR>
            % Ensure cells are matrices or vectors
            for jj = ONE:numel(T)
                coder.internal.assert(ismatrix(T{jj}) && ~isstruct(T{jj}) && ~isa(T{jj},'function_handle'), ...
                    ['MATLAB:',messageIdent,':FirstInputSize']);
            end
        else
            % Ensure the data is a matrix or vector
            isValidT = ismatrix(T) && ~isstruct(T) && ~isa(T,'function_handle');
            if strcmp(messageIdent,'groupsummary') || strcmp(messageIdent,'groupfilter')
                coder.internal.assert(isValidT, ['MATLAB:',messageIdent,':FirstInputSize']);
            else
                coder.internal.assert(isValidT, ['MATLAB:',messageIdent,':FirstInputType']);
            end
        end
    end

    % Error for function handle grouping variable
    varIsFuncHandle = isa(groupVars,'function_handle');
    if strcmp(messageIdent,'groupcounts')
        coder.internal.assert(~varIsFuncHandle,['MATLAB:',messageIdent,':FirstInputSize']);
    else
        coder.internal.assert(~varIsFuncHandle,['MATLAB:',messageIdent,':SecondInputType']);
    end

    % Extract grouping variables
    if iscell(groupVars)
        if coder.internal.isConstTrue(isempty(groupVars))
            groupingData = cell(1,0);
        elseif coder.internal.isConstTrue(iscellstr(groupVars))
            % iscellstr returns true for empty cell arrays - exclude
            % variable-sized cell arrays that don't contain chars
            % (iscellstr will not be const in that case)
            groupingData = {groupVars};
        else
            groupingData = reshape(groupVars,1,[]);
        end
        % Verify grouping variables are column vectors
        numGroupVars = numel(groupingData);
        coder.unroll(coder.internal.isConst(numGroupVars))
        for k = ONE:numGroupVars
            coder.internal.assert(iscolumn(groupingData{k}),'MATLAB:findgroups:GroupingVarNotVector');
        end
    else
        coder.internal.assert(ismatrix(groupVars),'MATLAB:findgroups:GroupingVarNotVector');
        numGroupVars = size(groupVars,2);
        groupingData = cell(1,numGroupVars);
        for k = ONE:numGroupVars
            groupingData{k} = groupVars(:,k);
        end
    end

    if nargin == 4
        if iscell(T) && ~iscellstr(T) && strcmp(messageIdent,'groupsummary') %#ok<ISCLSTR>
            % Verify size matches between data matrix and grouping vectors
            if isempty(groupVars)
                % This checks the data vs groups for 100x0 vs 100xN and 0x1 vs 0xM
                if ismatrix(groupVars) && (size(groupVars,1)~=0 || size(groupVars,2)~=0)
                    for jj = ONE:numel(T)
                        coder.internal.assert(size(T{jj},1) == size(groupVars,1), ['MATLAB:',messageIdent,':FirstSecondMismatchSize']);
                    end
                elseif size(groupVars,1) == 0 && size(groupVars,2) == 0
                    % groupVars is [] and make sure all the data is the
                    % same height
                    n = size(T{1},1);
                    for jj = ONE:numel(T)
                        coder.internal.assert(size(T{jj},1) == n, ['MATLAB:',messageIdent,':FirstInputSizeCellRows']);
                    end
                end
            else
                for jj = ONE:numel(T)
                    coder.internal.assert(size(T{jj},1) == size(groupingData{1},1), ['MATLAB:',messageIdent,':FirstSecondMismatchSize']);
                end
            end
        else
            % Verify size matches between data matrix and grouping vectors
            if isempty(groupVars)
                % This checks the data vs groups for 100x0 vs 100xN and 0x1 vs 0xM
                if ismatrix(groupVars) && (size(groupVars,1)~=0 || size(groupVars,2)~=0)
                    coder.internal.assert(size(T,1) == size(groupVars,1), ['MATLAB:',messageIdent,':FirstSecondMismatchSize']);
                end
            else
                coder.internal.assert(size(T,1) == size(groupingData{1},1), ['MATLAB:',messageIdent,':FirstSecondMismatchSize']);
            end
        end
    end
end
end
