function [tf,lthresh,uthresh,center] = isoutlier(a,varargin)
%MATLAB Code Generation Library Function

%   Limitations:
%   'movmean' and 'movmedian' methods not supported for datetime or
%   duration sample points and timetables

%   Copyright 2016-2023 The MathWorks, Inc.
%#codegen

narginchk(1,inf);
coder.internal.prefer_const(varargin);
[method,wl,p,sp,maxno,~,dim,dataVars,TABULAROUTPUT] = outlierFunParser('isoutlier',a,varargin{:});
if istabular(a)
    [tfTmp,lthreshTmp,uthreshTmp,centerTmp] = coder.internal.applyToTableVariables( ...
        a, @(x) validateAndLocateOutliers(x,dim,method,wl,p,sp,maxno,[]),...
        dataVars,[TABULAROUTPUT true true true],[~TABULAROUTPUT,false,false,false],[true,true,true,true],'','MATLAB:isoutlier:DataVariablesTableSubscript');
    if isempty(tfTmp)
        % Either a is empty or dataVars is empty - ensure the output is the
        % correct size
        if TABULAROUTPUT
            tfTmp2 = array2table(false(size(a,1),size(a,2)),'VariableNames',a.Properties.VariableNames);
        else
            tfTmp2 = false(size(a,1),size(a,2));
        end
        if size(tfTmp2,2) == 0
            lthreshTable = table.init({}, 1, {}, 0, {}, a.Properties.DimensionNames); % 1x0 table
            % Row names do not need to be preserved because there are no
            % rows
            uthreshTable = lthreshTable;
            centerTable = lthreshTable;
        else
            lthreshTable = lthreshTmp;
            uthreshTable = uthreshTmp;
            centerTable = centerTmp;
        end
    else
        tfTmp2 = tfTmp;
        lthreshTable = lthreshTmp;
        uthreshTable = uthreshTmp;
        centerTable = centerTmp;
    end
    if istimetable(a)
        if TABULAROUTPUT
            tf = table2timetableWithDimNames(tfTmp2,a.Properties.RowTimes(1:height(tfTmp2),:),a.Properties.DimensionNames);
        else
            tf = tfTmp2;
        end
        lthresh = table2timetableWithDimNames(lthreshTable,a.Properties.RowTimes(1:height(lthreshTable),:),a.Properties.DimensionNames);
        uthresh = table2timetableWithDimNames(uthreshTable,a.Properties.RowTimes(1:height(uthreshTable),:),a.Properties.DimensionNames);
        center = table2timetableWithDimNames(centerTable,a.Properties.RowTimes(1:height(centerTable),:),a.Properties.DimensionNames);
    else
        tf = tfTmp2;
        lthresh = lthreshTable;
        uthresh = uthreshTable;
        center = centerTable;
    end
else
    [tf,lthresh,uthresh,center] = locateoutliers(a,dim,method,wl,p,sp,maxno,[]);
end

%--------------------------------------------------------------------------

function [tf,lb,ub,center] = validateAndLocateOutliers(a,dim,...
    method,wl,p,sp,maxoutliers,replace)
coder.inline('always');
coder.internal.prefer_const(dim,method,wl,p,sp,maxoutliers,replace);
validateattributes(a,{'single','double','table'},{'real'},mfilename,'A',1);
coder.internal.assert(iscolumn(a) || isempty(a),'MATLAB:isoutlier:NonColumnTableVar');
[tf,lb,ub,center] = locateoutliers(a,dim,method,...
    wl,p,sp,maxoutliers,replace);