/**
 * Tabela 26 — Coeficientes de força Cx e Cy para barras prismáticas
 * de faces planas de comprimento infinito (NBR 6123:2023, sec. 8.1.1).
 *
 * Inclui formas: placa, perfil L, perfil T, perfil I, retângulo.
 *
 * Fonte: NBR 6123:2023, p. 52 (Tabela 26).
 * Última auditoria: 2026-07-07 — ⚠️ PENDENTE: revisar valores por
 *   forma × α × Cx/Cy.
 *   PDF página 64.
 */

import { linearInterp1D } from '../log-interp';

const ALPHAS = [0, 45, 90, 135, 180] as const;

export type FlatBarSection = 'placa' | 'l' | 't' | 'i' | 'rectangle';

interface CxCyPair {
  cx: number;
  cy: number;
}

/** Matrizes por seção × ângulo */
const T26: Record<FlatBarSection, readonly CxCyPair[]> = {
  placa: [
    { cx: 2.0, cy: 0 },
    { cx: 1.8, cy: 1.8 },
    { cx: 0, cy: 2.0 },
    { cx: -2.0, cy: 1.8 },
    { cx: -2.0, cy: 0 },
  ],
  l: [
    { cx: 2.0, cy: 0 },
    { cx: 1.6, cy: 1.7 },
    { cx: 0, cy: 1.9 },
    { cx: -1.5, cy: 1.8 },
    { cx: -2.0, cy: 1.4 },
  ],
  t: [
    { cx: 2.0, cy: 0 },
    { cx: 1.2, cy: 0.9 },
    { cx: 0, cy: 1.85 },
    { cx: -1.1, cy: 1.0 },
    { cx: -2.0, cy: 1.6 },
  ],
  i: [
    { cx: 2.0, cy: 0 },
    { cx: 1.5, cy: 1.5 },
    { cx: 0, cy: 1.8 },
    { cx: -1.1, cy: 1.0 },
    { cx: -2.0, cy: 1.6 },
  ],
  rectangle: [
    { cx: 1.5, cy: 0 },
    { cx: 1.2, cy: 0.9 },
    { cx: 0, cy: 1.85 },
    { cx: -1.1, cy: 1.0 },
    { cx: -2.0, cy: 1.6 },
  ],
};

export interface FlatBarForceInput {
  section: FlatBarSection;
  /** Ângulo α em graus */
  alpha: number;
  /** Largura c (dimensão frontal perpendicular ao eixo longitudinal) — em (m) */
  width: number;
  /** Comprimento ℓ (m) */
  length: number;
  /** Pressão dinâmica q (kN/m²) */
  q: number;
}

export interface FlatBarForceResult {
  cx: number;
  cy: number;
  fxKN: number;
  fyKN: number;
  /** Fator K (comprimento finito) — Tabela 28 */
  kFactor: number;
}

/**
 * Coeficientes de força para barra prismática de face plana.
 * α=0° é face plana contra o vento.
 */
export function getFlatBarCoefficients(section: FlatBarSection, alpha: number): CxCyPair {
  const arr = T26[section];
  const xs = ALPHAS;
  const cxs = arr.map((p) => p.cx);
  const cys = arr.map((p) => p.cy);
  return {
    cx: Number(linearInterp1D(xs, cxs, alpha).toFixed(3)),
    cy: Number(linearInterp1D(xs, cys, alpha).toFixed(3)),
  };
}

export function calculateFlatBarForce(input: FlatBarForceInput): FlatBarForceResult {
  const { section, alpha, width, length, q } = input;
  const { cx, cy } = getFlatBarCoefficients(section, alpha);

  // Fator K de redução por comprimento finito (Tabela 28)
  const lc = length / width;
  const kFactor = getKFactorFlatBar(lc);

  const fxKN = Number((cx * q * width * length * kFactor).toFixed(3));
  const fyKN = Number((cy * q * width * length * kFactor).toFixed(3));

  return { cx, cy, fxKN, fyKN, kFactor };
}

/**
 * Tabela 28 — Fator de redução K para barras de comprimento finito.
 * Caso: barras prismáticas de faces planas.
 */
const K_FLATBAR_LCS = [2, 5, 10, 20, 40, 50, 100, 1000] as const;
const K_FLATBAR_VALUES = [0.62, 0.66, 0.69, 0.81, 0.87, 0.90, 0.95, 1.0] as const;

export function getKFactorFlatBar(lc: number): number {
  const x = Math.max(2, Math.min(1000, lc));
  return Number(linearInterp1D(K_FLATBAR_LCS, K_FLATBAR_VALUES, x).toFixed(3));
}