function layer = adaptiveAveragePooling2dLayer(outputSize, nameValueArgs)

%   Copyright 2023 The MathWorks, Inc.

% Declare input argument restrictions.
arguments
    outputSize {iAssertValidOutputSize}
    nameValueArgs.Name {iAssertValidLayerName} = ''
end

outputSize = iConvertToTwoElementVector(outputSize);

layer = nnet.cnn.layer.AdaptiveAveragePooling2DLayer(outputSize, nameValueArgs.Name);

end

function iAssertValidLayerName(name)
nnet.internal.cnn.layer.paramvalidation.validateLayerName(name);
end

function iAssertValidOutputSize(value)
validateattributes(value, {'numeric'}, ...
    {'positive', 'real', 'integer', 'nonempty'});
iAssertScalarOrRowVectorOfTwo(value,'outputSize');
end

function tf = iIsRowVectorOfTwo(x)
tf = isrow(x) && numel(x)==2;
end

function iAssertScalarOrRowVectorOfTwo(value,name)
if ~(isscalar(value) || iIsRowVectorOfTwo(value))
    error(message('nnet_cnn:layer:Layer:ParamMustBeScalarOrPair',name));
end
end

function outputSize = iConvertToTwoElementVector(outputSize)
if isscalar(outputSize)
    outputSize = [outputSize, outputSize];
end
end