function [iLeft,iRight,nPadLeft,nPadRight] = movfunWindow( ...
    outputIdx,maxInputIdx,kLeft,kRight,endopt)
%MATLAB Code Generation Private Function

%    Return the left and right index values for the moving window of the an
%    array of length maxInputIdx. Also returns the number of elements
%    needed for padding on the left and on the right to fill out the window
%    if it intersects the edges of the data vector.

% Copyright 2016 The MathWorks, Inc.
%#codegen

coder.internal.prefer_const(kLeft,kRight,endopt);
coder.inline('always');
ONE = coder.internal.indexInt(1);
ZERO = coder.internal.indexInt(0);
if endopt == 'd'
    iLeft = outputIdx;
    iRight = outputIdx + kLeft + kRight;
    nPadLeft = ZERO;
    nPadRight = ZERO;
else
    if outputIdx <= kLeft
        iLeft = ONE;
        if endopt == 's'
            nPadLeft = ZERO;
        else
            nPadLeft = kLeft - outputIdx + 1;
        end
    else
        iLeft = outputIdx - kLeft;
        nPadLeft = ZERO;
    end
    ipnf = outputIdx + kRight;
    if ipnf > maxInputIdx
        iRight = maxInputIdx;
        if endopt == 's'
            nPadRight = ZERO;
        else
            nPadRight = ipnf - maxInputIdx;
        end
    else
        iRight = ipnf;
        nPadRight = ZERO;
    end
end
