export const openBackTrainInfo = (stationInfo, trainData, showNearTrain) => {
  const migrationArray = (stationInfo) => {
    const mainTrainStationPosition = trainData.findIndex(
      (d) => d.split(",")[0] == stationInfo
    );

    const relationMain = (() => {
      if (mainTrainStationPosition == 0) return "head";
      if (mainTrainStationPosition == trainData.length - 1) return "tail";
      return "middle";
    })();

    const subTrainStationPosition = showNearTrain.findIndex(
      (d) => d.split(",")[0] == stationInfo
    );
    const relationSub = (() => {
      if (subTrainStationPosition == 0) return "head";
      if (subTrainStationPosition == showNearTrain.length - 1) return "tail";
      return "middle";
    })();

    switch (relationMain) {
      case "head":
        if (relationSub == "head") {
          return;
        } else if (relationSub == "tail") {
          return [
            ...showNearTrain.slice(0, subTrainStationPosition),
            ...trainData,
          ];
        } else if (relationSub == "middle") {
          return [
            ...showNearTrain.slice(0, subTrainStationPosition),
            ...trainData,
          ];
        } else return;
      case "tail":
        if (relationSub == "head") {
          return [
            ...trainData.slice(0, mainTrainStationPosition),
            ...showNearTrain,
          ];
        } else if (relationSub == "tail") {
          return;
        } else if (relationSub == "middle") {
          return [
            ...trainData.slice(0, mainTrainStationPosition),
            ...showNearTrain.slice(subTrainStationPosition),
          ];
        } else return;
      case "middle":
        if (relationSub == "head") {
          return [
            ...trainData.slice(0, mainTrainStationPosition),
            ...showNearTrain,
          ];
        } else if (relationSub == "tail") {
          return [
            ...showNearTrain.slice(0, subTrainStationPosition),
            ...trainData.slice(mainTrainStationPosition),
          ];
        } else return;
    }
  };
  const array = migrationArray(stationInfo);
  if (!array) return null;
  return array;
};
