import { customTrainDataDetector } from "@/components/custom-train-data";
import { CustomTrainData, trainTypeID } from "./CommonTypes";
import { trainDataType } from "./trainPositionTextArray";

export const getCurrentTrainData = (
  trainNum: string,
  currentTrain: trainDataType[],
  allCustomTrainData: CustomTrainData[] | undefined
) => {
  const customTrainData = customTrainDataDetector(trainNum, allCustomTrainData);
  switch (customTrainData.type) {
    case "Normal":
    case "OneMan":
      const currentTrainData = currentTrain.filter((a) => a.num == trainNum);
      if (currentTrainData.length == 0) return customTrainData;
      else if (currentTrainData[0].Type?.includes("rapid:")) {
        const typeText = currentTrainData[0].Type?.split(":");
        const returnData = {
          type: "Rapid" as trainTypeID,
          trainName: typeText[1].replace("\r", ""),
          trainIcon: null,
          trainNumDistance: null,
          info: "",
        };
        return returnData;
      }
      return customTrainData;
    default:
      return customTrainData;
  }
};
