/**
 * Strategy para cúpulas (NBR 6123:2023, sec. 6.2.4).
 */

import { getDomeOnGroundCpeNBR6123, getDomeLiftForce } from '../nbr-tables/table-21';
import { getDomeOnCylinderCpeNBR6123 } from '../nbr-tables/table-22';

export type DomeType = 'on-ground' | 'on-cylinder';

export interface DomeInput {
  /** Diâmetro d (m) */
  d: number;
  /** Flecha f (altura) */
  f: number;
  /** Velocidade Vk (m/s) */
  vk: number;
  /** Altura da parede cilíndrica abaixo da cúpula (m) — apenas para on-cylinder */
  h?: number;
  type: DomeType;
  cpi: number;
}

export interface DomeResult {
  q: number;
  fOverD: number;
  cpi: number;
  cpeBarlavento: number;
  cpeTopo: number;
  cpeLateral: number;
  liftCoefficient: number;
  /** Força de sustentação (kN) */
  liftForceKN: number;
}

export function calculateDome(input: DomeInput): DomeResult {
  const { d, f, vk, type, cpi } = input;
  const q = Number((0.613 * vk * vk / 1000).toFixed(4));
  const fd = f / d;

  if (type === 'on-ground') {
    const v = getDomeOnGroundCpeNBR6123(fd);
    const lift = getDomeLiftForce(v.cs, q, d);
    return {
      q,
      fOverD: fd,
      cpi,
      cpeBarlavento: v.cpeMax,
      cpeTopo: v.cpeMin,
      cpeLateral: v.cpeMin,
      liftCoefficient: v.cs,
      liftForceKN: lift,
    };
  }

  const c = getDomeOnCylinderCpeNBR6123(fd);
  return {
    q,
    fOverD: fd,
    cpi,
    cpeBarlavento: c.cpeBarlavento,
    cpeTopo: c.cpeTopo,
    cpeLateral: c.cpeLateral,
    liftCoefficient: 0,
    liftForceKN: 0,
  };
}