import { SimBrief } from '../types/general';

type Plan = {
  params: {
    units: 'kgs' | 'lbs';
  };
  weights: {
    est_zfw: number;
    est_ramp: number;
    pax_count_actual: number;
    freight_added: number;
  };
  fuel: {
    plan_ramp: number;
  };
};

export const ImportFlightPlanKH = (
  plan: Plan,
  maxZFW: number,
  maxTOW: number,
  maxFuel: number,
  isImperial: boolean
) => {
  if (!plan) return { type: 'error', message: 'Empty plan' };

  let convFactor = 1;
  if (plan.params.units === 'kgs' && isImperial) convFactor = 2.20462262185;
  if (plan.params.units === 'lbs' && !isImperial) convFactor = 1 / 2.20462262185;

  return {
    type: 'data',
    message: {
      plannedZFW: Math.min(maxZFW, Math.round(plan.weights.est_zfw * convFactor)),
      plannedGW: Math.min(maxTOW, Math.round(plan.weights.est_ramp * convFactor)),
      pax: plan.weights.pax_count_actual,
      cargo: Math.round(plan.weights.freight_added * convFactor),
      fuel: Math.min(maxFuel, Math.round(plan.fuel.plan_ramp * convFactor)),
    } as SimBrief,
  };
};
