import { store } from "../redux/store";
import { WhichRulesetToApply } from "./constants";

export default function GetJobTarget({ group, v_age, targets, close_date }) {
  // //Old Validation
  // const targetsForGroup = targets.filter((t) => t.group === group);
  // console.log(
  //   "🚀 ~ file: GetJobTarget.js:7 ~ GetJobTarget ~ targetsForGroup",
  //   targetsForGroup
  // );
  // if (!targetsForGroup) {
  //   console.log("Result:", 0);
  // }
  // const targetPc = targetsForGroup.filter(
  //   (t) => t.ageGte <= v_age && (t.ageLt ? t.ageLt > v_age : true)
  // );
  // if (targetPc.length === 0) console.log("Result:", 1);
  // else if (targetPc.length === 1) console.log("Result: ", targetPc[0].target);
  // else {
  //   console.log("Result:", 1);
  // }

  //V2 Check
  const newTargets = store.getState().user.targets;
  const rulesToApply = WhichRulesetToApply(close_date);
  const newTargetsForGroup = newTargets.filter(
    (t) => t.name === rulesToApply && t.group === group
  );

  if (!newTargetsForGroup) return 0;
  const newTargetPc = newTargetsForGroup.filter(
    (t) => t.ageGte <= v_age && (t.ageLt ? t.ageLt > v_age : true)
  );

  if (newTargetPc.length === 0) return 1;
  else if (newTargetPc.length === 1) return newTargetPc[0].target;
  else {
    return 1;
  }
}
