package puzzle;

import puzzle.SwedishGenerator.Lemma;
import java.util.function.LongConsumer;
import static java.nio.charset.StandardCharsets.US_ASCII;

public final class CsvIndexService {
   
   static int SIMPEL_IDX = 3;
   
   public static int lineToSimpel(String line) {
      var parts = line.split(",", 5);
      return Integer.parseInt(parts[SIMPEL_IDX].trim());
   }
   public static String[] lineToClue(String line) {
      if (line.isBlank()) throw new RuntimeException("Empty line");
      var parts   = line.split(",", 5);
      var rawClue = parts[4].trim();
      if (rawClue.startsWith("\"") && rawClue.endsWith("\"")) {
         rawClue = rawClue.substring(1, rawClue.length() - 1).replace("\"\"", "\"");
      }
      return Meta.GSON.fromJson(rawClue, String[].class);
   }
   public static void lineToLemma(String line, LongConsumer ok) {
      if (line.isBlank()) {
         throw new RuntimeException("Empty line");
      }
      var parts = line.split(",", 5);
      var id    = Integer.parseInt(parts[0].trim());
      var word  = parts[1].trim();
      int score = Integer.parseInt(parts[2].trim());
      if (score < 1) {
         if (Main.VERBOSE) System.err.println("Word too complex: " + line);
         return;
      }
      ok.accept(Lemma.from(word.getBytes(US_ASCII)));
   }
}
