// termsetting/handlers/snplst.sampleSum.ts
function get_effectAllele(alleleType, snp) {
  let effectAllele;
  if (snp.effectAllele) return snp.effectAllele;
  if (alleleType == 0) {
    const majorAlleleCount = Math.max(...snp.alleles.map((al) => al.count));
    const otherAlleles = snp.alleles.filter((i) => i.count != majorAlleleCount);
    if (otherAlleles.length) {
      const maxOtherAlleleCount = Math.max(...otherAlleles.map((al) => al.count));
      effectAllele = otherAlleles.find((al) => al.count == maxOtherAlleleCount).allele;
    } else {
      effectAllele = void 0;
    }
  } else if (alleleType == 1) {
    const altals = snp.alleles.filter((i) => !i.isRef);
    if (altals.length) {
      const maxcnt = Math.max(...altals.map((al) => al.count));
      effectAllele = altals.find((al) => al.count === maxcnt).allele;
    } else {
      effectAllele = void 0;
    }
  } else if (alleleType == 2) {
    effectAllele = void 0;
  } else {
    throw "unknown alleleType value";
  }
  snp.effectAllele = effectAllele;
  return effectAllele;
}
function mayRunSnplstTask(tw, data) {
  if (tw.term.type != "snplst" && tw.term.type != "snplocus") return;
  if (data.error) throw data.error;
  if (!Array.isArray(data.snps)) throw "data.snps[] not array";
  if (!tw.term.snps) throw `Missing tw.term.snps [snplst.sampleSum.ts mayRunSnplstTask()]`;
  for (const s of tw.term.snps) {
    delete s.alleles;
    delete s.gt2count;
  }
  tw.q.snp2effAle = {};
  for (const s of data.snps) {
    const snp = tw.term.snps.find((i) => i.snpid == s.snpid);
    if (!snp) throw "snp not found by id";
    snp.alleles = s.alleles;
    snp.gt2count = s.gt2count;
    tw.q.snp2effAle[snp.snpid] = get_effectAllele(tw.q.alleleType, snp);
  }
  tw.q.numOfSampleWithAnyValidGT = data.numOfSampleWithAnyValidGT;
  if (tw.q.geneticModel == 3) {
    tw.q.snp2refGrp = {};
    for (const s of tw.term.snps) {
      if (s.invalid || !s.gt2count) continue;
      const effectAllele = tw.q.snp2effAle[s.snpid];
      let refGT;
      for (const gt in s.gt2count) {
        if (!gt.includes(effectAllele)) {
          refGT = gt;
          break;
        }
      }
      if (!refGT) {
        refGT = Object.keys(s.gt2count)[0];
      }
      tw.q.snp2refGrp[s.snpid] = refGT;
    }
  } else {
    delete tw.q.snp2refGrp;
  }
}

export {
  mayRunSnplstTask
};
//# sourceMappingURL=chunk-7OYOC7XY.js.map
