import { genotypeColors, nucleotide_gene } from "./globals";

const getAnnotations = (jsonData) => {
  const annotations = [];
  const nuc = [];
  let aaCount = 0;
  for (const prot of Object.keys(jsonData)) {
    if (prot !== nucleotide_gene) {
      aaCount++;
      annotations.push({
        prot: prot,
        start: jsonData[prot].start,
        end: jsonData[prot].end,
        readingFrame: jsonData[prot].strand,
        fill: genotypeColors[aaCount % 10]
      });
    } else {
      nuc.push({
        start: jsonData[prot].start,
        end: jsonData[prot].end
      });
    }
  }
  return [annotations, nuc];
};

const processAnnotations = (annotations) => {
  const m = {}; /* m === geneMap */
  annotations.forEach((d) => {
    m[d.prot] = d;
  });
  const sorted = Object.keys(m).sort((a, b) =>
    m[a].start < m[b].start ? -1 : m[a].start > m[b].start ? 1 : 0
  );
  for (const gene of Object.keys(m)) {
    m[gene].idx = sorted.indexOf(gene);
  }
  return m;
};

export const entropyCreateState = (genomeAnnotations) => {
  if (genomeAnnotations) {
    const ant = getAnnotations(genomeAnnotations);
    const annotations = ant[0];
    const lengthSequence = ant[1][0].end;
    return {
      showCounts: false,
      loaded: true,
      annotations,
      lengthSequence,
      geneMap: processAnnotations(annotations)
    };
  }
  return {
    showCounts: false,
    loaded: false,
    annotations: [],
    geneMap: {}
  };
};

import { genotypeColors, nucleotide_gene } from "./globals";

const getAnnotations = (jsonData) => {
  const annotations = [];
  const nuc = [];
  let aaCount = 0;
  for (const prot of Object.keys(jsonData)) {
    if (prot !== nucleotide_gene) {
      aaCount++;
      annotations.push({
        prot: prot,
        start: jsonData[prot].start - 1,
        end: jsonData[prot].end,
        readingFrame: jsonData[prot].strand,
        fill: genotypeColors[aaCount % 10]
      });
    } else {
      nuc.push({
        start: jsonData[prot].start,
        end: jsonData[prot].end
      });
    }
  }
  return [annotations, nuc];
};

const processAnnotations = (annotations) => {
  const m = {}; /* m === geneMap */
  annotations.forEach((d) => {
    m[d.prot] = d;
  });
  const sorted = Object.keys(m).sort((a, b) =>
    m[a].start < m[b].start ? -1 : m[a].start > m[b].start ? 1 : 0
  );
  for (const gene of Object.keys(m)) {
    m[gene].idx = sorted.indexOf(gene);
  }
  return m;
};

export const entropyCreateState = (genomeAnnotations) => {
  if (genomeAnnotations) {
    const ant = getAnnotations(genomeAnnotations);
    const annotations = ant[0];
    const lengthSequence = ant[1][0].end;
    return {
      showCounts: false,
      loaded: true,
      annotations,
      lengthSequence,
      geneMap: processAnnotations(annotations)
    };
  }
  return {
    showCounts: false,
    loaded: false,
    annotations: [],
    geneMap: {}
  };
};

Convert start to zero-based