function Y = permuteInputVar(X, userDataPerm_, onnxNDims_)  
% Returns reverse-ONNX ordering numeric array
%#codegen

%   Copyright 2024 The MathWorks, Inc.  

XNum = EDHR_Net.coder.ops.extractIfDlarray(X);
userDataPerm = EDHR_Net.coder.ops.extractIfDlarray(userDataPerm_);
onnxNDims = EDHR_Net.coder.ops.extractIfDlarray(onnxNDims_);

if onnxNDims == 0
    Y = XNum;
    return;
elseif onnxNDims == 1 && isvector(X)
    Y = XNum(:);
    return;    
elseif isnumeric(userDataPerm)
    % Permute into reverse ONNX ordering
    if numel(userDataPerm) ~= onnxNDims
        error(message('nnet_cnn_onnx:onnx:InputPermutationSize', numel(userDataPerm), onnxNDims));
    end
    perm = fliplr(userDataPerm);
elseif isequal(userDataPerm, 'auto') && onnxNDims == 4
    % Permute MATLAB HWCN to reverse onnx (WHCN)
    perm = [2 1 3 4];
elseif isequal(userDataPerm, 'as-is')
    % Do not permute the input
    perm = 1:ndims(XNum);
else
    % userDataPerm is either 'none' or 'auto' with no default, which means
    % it's already in onnx ordering, so just make it reverse onnx
    perm = max(2,onnxNDims):-1:1;
end
Y = permute(XNum, perm);
end
