function out = compute_component_force_ASCE(ap, SDS, Rp, Ip, z, h, Wp, a1, hcg, B, n_bolts)
% compute_component_force_ASCE
%
% Calculates seismic force on a nonstructural component per ASCE 7-22 Ch 13,
% including required minimum and maximum limits, overturning moments,
% restoring moment, vertical seismic load, and anchor shear.
%
% Equation:
%   Fp = (0.4*ap*SDS / (Rp/Ip)) * (1 + a1*z/h) * Wp
%
% Limits:
%   Fp >= Fp_min = 0.3*SDS*Ip*Wp
%   Fp <= Fp_max = 1.6*SDS*Ip*Wp
%
% INPUTS (required)
%   ap       - Component amplification factor
%   SDS      - Design spectral acceleration, short period (g)
%   Rp       - Component response modification factor
%   Ip       - Component importance factor
%   z        - Component height above base (ft or m)
%   h        - Average structure roof height (ft or m)
%   Wp       - Component operating weight (lbf or N)
%   a1       - Story amplification factor (2.5 one-story, 2.0 multi-story)
%
% INPUTS (optional - for overturning/anchor checks)
%   hcg      - Height of CG above base plate (in or mm) [default 0 = skip]
%   B        - Base half-width for restoring moment (in or mm) [default 0]
%   n_bolts  - Number of anchor bolts [default 4]
%
% OUTPUT
%   out  - struct with Fp_nominal, Fp_min, Fp_max, Fp (governing),
%          Ev, MOT, MR, Va, governs string

if nargin < 9,  hcg     = 0; end
if nargin < 10, B       = 0; end
if nargin < 11, n_bolts = 4; end

%% Base equation
Fp_nominal = (0.4 * ap * SDS / (Rp/Ip)) * (1 + a1*(z/h)) * Wp;

%% ASCE limits
Fp_min = 0.3 * SDS * Ip * Wp;
Fp_max = 1.6 * SDS * Ip * Wp;

%% Governing design force
if Fp_nominal < Fp_min
    Fp = Fp_min;
    governs = 'Fp_min governs (0.3*SDS*Ip*Wp)';
elseif Fp_nominal > Fp_max
    Fp = Fp_max;
    governs = 'Fp_max governs (1.6*SDS*Ip*Wp)';
else
    Fp = Fp_nominal;
    governs = 'Fp nominal governs';
end

%% Vertical seismic load effect
Ev = 0.2 * SDS * Wp;

%% Overturning and anchor checks (only if hcg provided)
if hcg > 0 && B > 0
    MOT    = Fp * hcg;
    MR     = (0.9 - 0.2*SDS) * Wp * B;
    Va     = Fp / n_bolts;
    uplift = (MOT > MR);
else
    MOT    = NaN;
    MR     = NaN;
    Va     = NaN;
    uplift = false;
end

%% Package output
out = struct();
out.ap         = ap;
out.SDS        = SDS;
out.Rp         = Rp;
out.Ip         = Ip;
out.z          = z;
out.h          = h;
out.Wp         = Wp;
out.a1         = a1;
out.Fp_nominal = Fp_nominal;
out.Fp_min     = Fp_min;
out.Fp_max     = Fp_max;
out.Fp         = Fp;
out.governs    = governs;
out.Ev         = Ev;
out.MOT        = MOT;
out.MR         = MR;
out.Va         = Va;
out.uplift     = uplift;

end
