import {
  GENE_EXPRESSION,
  METABOLITE_INTENSITY,
  PROTEOME_ABUNDANCE,
  SINGLECELL_GENE_EXPRESSION
} from "./chunk-NOEAT6CX.js";

// common/termutils.js
function sample_match_termvaluesetting(row, filter, geneVariant$ids) {
  const lst = !filter ? [] : filter.type == "tvslst" ? filter.lst : [filter];
  let numberofmatchedterms = 0;
  for (const item of lst) {
    if (item.type == "tvslst") {
      if (sample_match_termvaluesetting(row, item)) {
        numberofmatchedterms++;
      }
    } else {
      const t = item.tvs;
      let samplevalue;
      if (t.term.type == "geneVariant") {
        samplevalue = geneVariant$ids.map((g) => row[g]).filter((s) => s);
      } else if (t.term.type == "integer" || t.term.type == "float" || t.term.type == GENE_EXPRESSION || t.term.type == METABOLITE_INTENSITY || t.term.type == PROTEOME_ABUNDANCE) {
        samplevalue = row[t.term.id] || row[t.term.$id]?.key;
      } else if (t.term.type == "survival") {
        samplevalue = row[t.term.$id]?.key;
      } else {
        samplevalue = row[t.term.id] || row[t.term.$id]?.value;
      }
      let thistermmatch;
      if (t.term.type == "categorical") {
        if (samplevalue === void 0) {
          if (t.isnot) thistermmatch = !thistermmatch;
          if (thistermmatch) numberofmatchedterms++;
          continue;
        }
        const valueset = t.valueset ? t.valueset : new Set(t.values.map((i) => i.key));
        thistermmatch = valueset.has(samplevalue);
      } else if (t.term.type == "integer" || t.term.type == "float" || t.term.type == GENE_EXPRESSION || t.term.type == METABOLITE_INTENSITY || t.term.type == PROTEOME_ABUNDANCE) {
        if (samplevalue === void 0) {
          if (t.isnot) thistermmatch = !thistermmatch;
          if (thistermmatch) numberofmatchedterms++;
          continue;
        }
        for (const range of t.ranges) {
          if ("value" in range) {
            thistermmatch = samplevalue === range.value;
            if (thistermmatch) break;
          } else if (samplevalue == range.name) {
            thistermmatch = true;
            break;
          } else {
            if (t.term.values) {
              const v = t.term.values[samplevalue.toString()];
              if (v && v.uncomputable) {
                continue;
              }
            }
            let left, right;
            if (range.startunbounded) {
              left = true;
            } else if ("start" in range) {
              if (range.startinclusive) {
                left = samplevalue >= range.start;
              } else {
                left = samplevalue > range.start;
              }
            }
            if (range.stopunbounded) {
              right = true;
            } else if ("stop" in range) {
              if (range.stopinclusive) {
                right = samplevalue <= range.stop;
              } else {
                right = samplevalue < range.stop;
              }
            }
            thistermmatch = left && right;
          }
          if (thistermmatch) break;
        }
      } else if (t.term.type == "condition") {
        const key = getPrecomputedKey(t);
        const anno = samplevalue && samplevalue[key];
        if (anno) {
          thistermmatch = Array.isArray(anno) ? t.values.find((d) => anno.includes(d.key)) : t.values.find((d) => d.key == anno);
        }
      } else if (t.term.type == "survival") {
        if (samplevalue === void 0) {
          if (t.isnot) thistermmatch = !thistermmatch;
          if (thistermmatch) numberofmatchedterms++;
          continue;
        }
        const valueset = t.valueset ? t.valueset : new Set(t.values.map((i) => i.key));
        thistermmatch = valueset.has(samplevalue);
      } else if (t.term.type == "geneVariant" && t.legendFilterType == "geneVariant_hard") {
        const f = t.values[0];
        thistermmatch = samplevalue.find((s) => {
          for (const v of s.values) {
            if (v.dt == f.dt && (!v.origin || v.origin == f.origin) && f.mclasslst.includes(v.class)) return true;
          }
        }) && true;
      } else if (t.term.type == SINGLECELL_GENE_EXPRESSION) {
      } else {
        throw "unknown term type";
      }
      if (t.isnot) {
        thistermmatch = !thistermmatch;
      }
      if (thistermmatch) numberofmatchedterms++;
    }
    if (filter.join == "or" && numberofmatchedterms) return true;
  }
  if (numberofmatchedterms == lst.length) return true;
}

export {
  sample_match_termvaluesetting
};
//# sourceMappingURL=chunk-YZK37YCC.js.map
