function [xmax, xmin] = ws_srs_recursion(yy, a1, a2, b1, b2, b3)
%SRS_PEAKS_DISPATCH  Platform-aware dispatcher for SRS peak calculation
%
% This function selects the fastest available implementation for
% computing SRS peaks:
%   1) A platform-specific compiled MEX function, if available
%   2) A MATLAB fallback implementation otherwise
%
% The decision is cached using PERSISTENT variables so the OS/MEX checks
% are performed only once per MATLAB session, even if this function
% is called thousands of times inside a loop.
%
% Inputs:
%   yy  : base acceleration time history
%   a1,a2,b1,b2,b3 : IIR filter coefficients (vectors over frequency)
%
% Outputs:
%   xmax : positive SRS peak response
%   xmin : negative SRS peak response (magnitude)

% ---------------------------------------------------------
% Persistent variables
% ---------------------------------------------------------
% These retain their values between calls to this function
persistent mexFcn triedAndFailed initialized

% ---------------------------------------------------------
% One-time initialization block
% ---------------------------------------------------------
if isempty(initialized)
    initialized = true;

    mexFcn = [];            % Function handle to chosen MEX
                            % Empty means: no MEX available for this OS
    triedAndFailed = false; % If true, skip future MEX attempts

    % -----------------------------------------------------
    % OS detection
    % -----------------------------------------------------
    % ispc    : true on Microsoft Windows
    % isunix  : true on UNIX-like systems (Linux + macOS)
    % ismac   : true on macOS only
    %
    % Linux is: isunix == true AND ismac == false
    %
    % MATLAB MEX binaries are OS-specific:
    %   Windows : *.mexw64c
    %   Linux   : *.mexa64
    %   macOS   : *.mexmaci64 (Intel) or *.mexmaca64 (Apple Silicon)
    %
    % Therefore we must choose the correct MEX name per OS.
    % -----------------------------------------------------

    % ispc
    % isunix
    % ismac

    if ispc
        % Windows platform
        %
        % exist(name,'file') == 3 means:
        %   "a compiled MEX-function with this name exists on the MATLAB path"
        %
        if exist('srs_iir_peaks_mex', 'file') == 3
            mexFcn = @srs_iir_peaks_mex;
        end

    elseif isunix && ~ismac
        % Linux platform
        %
        % Windows MEX files (.mexw64) cannot run on Linux.
        % We look for a Linux-compiled MEX (.mexa64).
        %
        if exist('srs_iir_peaks_mex_linux', 'file') == 3
            mexFcn = @srs_iir_peaks_mex_linux;
        end

    elseif ismac
        % macOS platform (not implemented here)
        %
        % If you later add a macOS MEX, it would go here:
        %
        % if exist('srs_iir_peaks_mex_mac','file') == 3
        %     mexFcn = @srs_iir_peaks_mex_mac;
        % end
        %
        mexFcn = [];
    end
end

% ---------------------------------------------------------
% Execute selected implementation
% ---------------------------------------------------------
% Try the MEX function if:
%   1) A MEX was found for this OS
%   2) It has not previously failed in this MATLAB session
%
if ~isempty(mexFcn) && ~triedAndFailed
    try
        % Fast path: compiled MEX execution
        [~, xmax, xmin] = mexFcn(yy, a1, a2, b1, b2, b3);
        return

    catch ME
        % If the MEX fails once, disable it for the rest
        % of the MATLAB session to avoid repeated overhead.
        triedAndFailed = true;

        warning('SRS:MexFailed', ...
            'SRS MEX (%s) failed. Reverting to MATLAB fallback.\n%s', ...
            func2str(mexFcn), ME.message);
    end
end

% ---------------------------------------------------------
% MATLAB fallback implementation
% ---------------------------------------------------------
% This is your existing vectorized / loop-based SRS computation.
% It is slower than the MEX but guaranteed to work everywhere.
%
[xmax, xmin] = srs_iir_peaks_fallback(yy, a1, a2, b1, b2, b3);

end
