import {
  makeSnpSelect,
  mayRestrictAncestry
} from "./chunk-T5BDYCFO.js";
import "./chunk-7OYOC7XY.js";
import {
  addGeneSearchbox,
  filterInit,
  getNormalRoot
} from "./chunk-SA7APTJR.js";
import "./chunk-HJ6L54YS.js";
import "./chunk-LSEFWW72.js";
import "./chunk-SKREEF3H.js";
import "./chunk-HYOEWQ5P.js";
import "./chunk-HBW42TDT.js";
import "./chunk-G6O3URDN.js";
import "./chunk-FN5XPUPH.js";
import "./chunk-IIT367QZ.js";
import "./chunk-RZGEKL77.js";
import "./chunk-UI7OCM2A.js";
import "./chunk-YZK37YCC.js";
import "./chunk-7EMWHCVW.js";
import "./chunk-7IYJZZQI.js";
import "./chunk-M3J4MINX.js";
import "./chunk-PF4DSFDR.js";
import "./chunk-QH6W3NVF.js";
import "./chunk-NOEAT6CX.js";
import "./chunk-4UWS5Y3N.js";
import "./chunk-DD4R5P6W.js";
import "./chunk-JNITUVXP.js";
import "./chunk-KSGA62R2.js";
import "./chunk-LOZEKOES.js";
import "./chunk-TOU7EVFQ.js";
import "./chunk-OAWQ6LOO.js";
import "./chunk-TLT4YIG3.js";
import "./chunk-KYBIQBXE.js";
import "./chunk-I6Y4O3RR.js";
import "./chunk-OMR2DT66.js";
import "./chunk-DQC5FFGV.js";
import "./chunk-HFNDKYVF.js";

// termsetting/handlers/snplocus.ts
var term_name = "Variants in a locus";
function getHandler(self) {
  return {
    getPillName() {
      return self.term.name;
    },
    getPillStatus() {
      if (!self.term || !self.q) return;
      if (!self.term.snps) throw `Missing term.snps [snplocs.ts getPillStatus()]`;
      let text = `${self.q.chr}:${self.q.start}-${self.q.stop}, ${self.term.snps.length} variant${self.term.snps.length > 1 ? "s" : ""}`;
      if (self.term.reachedVariantLimit) {
        text += '<span style="margin-left: 6px; background:#aaa; font-size:1em;font-style: normal; border-radius: 7px;color:white;padding:0px 5px;">&#9888;<span>';
      }
      return { text };
    },
    validateQ(data) {
      validateQ(data);
    },
    async showEditMenu(div) {
      await makeEditMenu(self, div);
    }
    /* no need for postMain()
    cache file contains all samples,
    variants in a cache file is only determined by locus range and info fields
    thus no need to regenerate cache file upon subcohort or filter change via pill.main()
    async postMain() {
    	if (self.q && self.q.chr) {
    		await validateInput(self)
    	}
    }
    */
  };
}
async function makeEditMenu(self, div0) {
  const div = div0.append("div").style("margin", "15px");
  const select_ancestry = await mayRestrictAncestry(self, div);
  const coordResult = addGeneSearchbox({
    genome: self.opts.genomeObj,
    tip: self.dom.tip2,
    row: div.append("div").style("margin-top", "20px"),
    defaultCoord: self.q && self.q.chr ? { chr: self.q.chr, start: self.q.start, stop: self.q.stop } : void 0
  });
  div.select(".sja_genesearchinput").style("margin", "0px");
  div.append("span").style("margin", "5px 0px").style("display", "inline-block").style("opacity", 0.4).style("font-size", ".7em").html(
    '"Gene": Gene name (e.g. AKT1)</br>"Position": chr:start-stop (e.g. chr1:5000-6000)</br>"dbSNP": dbSNP accession (e.g. rs1042522)'
  );
  await mayDisplayVariantFilter(self, self.q?.variant_filter, div);
  const [input_AFcutoff, select_alleleType, select_geneticModel] = makeSnpSelect(
    div.append("div").attr("class", "sjpp-snp-select").style("margin-top", "15px"),
    self,
    "snplocus"
  );
  if (self.usecase.target == "dataDownload") div.select(".sjpp-snp-select").style("display", "none");
  const btnRow = div.append("div").style("margin-top", "15px");
  btnRow.append("button").style("margin-top", "15px").text("Submit").on("click", async (event) => {
    if (!coordResult.chr) return window.alert("Invalid coordinate");
    event.target.disabled = true;
    event.target.innerHTML = "Validating input...";
    if (self.term) {
    } else {
      self.term = { id: makeId() };
    }
    if (!self.q) self.q = {};
    self.term.type = "snplocus";
    self.q.chr = coordResult.chr;
    self.q.start = coordResult.start;
    self.q.stop = coordResult.stop;
    self.term.name = term_name;
    delete self.term.snps;
    self.q.variant_filter = getNormalRoot(self.variantFilter.active);
    await validateInput(self);
    {
      const v = Number(input_AFcutoff.property("value"));
      self.q.AFcutoff = v < 0 || v >= 100 ? 5 : v;
    }
    self.q.alleleType = select_alleleType.property("selectedIndex");
    self.q.geneticModel = select_geneticModel.property("selectedIndex");
    if (select_ancestry) {
      self.q.restrictAncestry = select_ancestry.node().options[select_ancestry.property("selectedIndex")].__ancestry_obj;
    }
    self.api.runCallback();
  });
  btnRow.append("span").style("padding-left", "15px").style("opacity", 0.8).style("font-size", ".8em").text(
    self.usecase.target == "dataDownload" ? "" : "Variants will be treated individually in separate regression models"
  );
}
async function validateInput(self) {
  const data = await self.vocabApi.validateSnps(self.q);
  if (data.error) throw data.error;
  self.q.cacheid = data.cacheid;
  self.term.snps = data.snps;
  self.term.reachedVariantLimit = data.reachedVariantLimit;
}
function validateQ(data) {
  const q = data.q;
  if (!Number.isFinite(q.AFcutoff)) throw "AFcutoff is not number";
  if (q.AFcutoff < 0 || q.AFcutoff > 100) throw "AFcutoff is not within 0 to 100";
  if (![0, 1].includes(q.alleleType)) throw "alleleType value is not one of 0/1";
  if (![0, 1, 2, 3].includes(q.geneticModel)) throw "geneticModel value is not one of 0/1";
  if (!q.chr) throw "chr missing";
  if (!Number.isInteger(q.start)) throw "start coordinate is not integer";
  if (!Number.isInteger(q.stop)) throw "stop coordinate is not integer";
  if (q.start < 0) throw "start < 0";
  if (q.stop <= q.start) throw "stop <= start";
}
async function fillTW(tw, vocabApi) {
  try {
    validateQ(tw);
  } catch (e) {
    throw "snplocus validateQ(): " + e;
  }
  if (!tw.term.name) tw.term.name = term_name;
  if (tw.id == void 0 || tw.id == "") {
    if (tw.term.id == void 0 || tw.term.id == "") {
      tw.term.id = makeId();
    }
    tw.id = tw.term.id;
  } else {
    if (tw.term.id == void 0 || tw.term.id == "") {
      tw.term.id = tw.id;
    }
  }
  await validateInput({
    term: tw.term,
    q: tw.q,
    vocabApi
  });
}
function makeId() {
  return "snplocus" + Math.random();
}
async function mayDisplayVariantFilter(self, filterInState, holder, callback2) {
  if (!self.variantFilter) {
    self.variantFilter = await self.vocabApi.get_variantFilter();
  }
  if (!self.variantFilter.terms) {
    return;
  }
  if (!self.variantFilter.opts) throw "variantFilter.opts{} missing";
  if (!self.variantFilter.filter) throw ".filter missing from variantFilter{}";
  if (!Array.isArray(self.variantFilter.terms) || self.variantFilter.terms.length == 0)
    throw "variantFilter.terms[] is not non-empty array";
  if (filterInState) {
    self.variantFilter.active = JSON.parse(JSON.stringify(filterInState));
  } else {
    self.variantFilter.active = JSON.parse(JSON.stringify(self.variantFilter.filter));
  }
  const div = holder.append("div").style("margin-top", "15px");
  div.append("span").text("VARIANT FILTERS").style("font-size", ".8em").style("opacity", 0.5);
  const filterBody = div.append("div");
  filterInit({
    joinWith: self.variantFilter.opts.joinWith,
    emptyLabel: "+Variant Filter",
    holder: filterBody,
    vocab: { terms: self.variantFilter.terms },
    callback: async (filter) => {
      self.variantFilter.active = filter;
      if (callback2) await callback2();
    }
  }).main(self.variantFilter.active);
}
export {
  fillTW,
  getHandler
};
//# sourceMappingURL=snplocus-IL5Z4XWV.js.map
