import {
  addGeneSearchbox,
  first_genetrack_tolist
} from "./chunk-SA7APTJR.js";
import "./chunk-HJ6L54YS.js";
import "./chunk-LSEFWW72.js";
import "./chunk-SKREEF3H.js";
import {
  Menu
} from "./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 {
  contigNameNoChr2
} from "./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";

// src/selectGenomeWithTklst.js
var tip = new Menu({ padding: "" });
var variantFlankingSize = 60;
async function init(arg, holder, genomes) {
  holder.style("margin", "40px 20px 20px 20px");
  const formdiv = holder.append("div");
  formdiv.append("p").text("To launch view, press ENTER at gene search.").style("opacity", 0.3);
  const blockholder = holder.append("div");
  const genomeselect = addGenomeSelect(formdiv, genomes).on("change", makeGeneSearch);
  let coordInput, genome;
  const geneInputSpan = formdiv.append("span").style("margin-left", "10px");
  const submitBtn = formdiv.append("button").style("display", "none").style("margin-top", "20px").style("padding", "10px 15px").style("border-radius", "30px").text("Launch ProteinPaint").on("click", submit);
  makeGeneSearch();
  function makeGeneSearch() {
    geneInputSpan.selectAll("*").remove();
    const s = genomeselect.node();
    genome = genomes[s.options[s.selectedIndex].innerHTML];
    const opt = {
      genome,
      tip,
      row: geneInputSpan,
      allowVariant: true,
      /* only show submit button if valid coord is found
      this is deal with a defect with genesearch.js
      that pasting coordinate in <input> without hitting enter
      will not trigger coord parsing
      */
      callback: () => submitBtn.style("display", "block")
    };
    coordInput = addGeneSearchbox(opt);
  }
  async function submit() {
    try {
      formdiv.remove();
      const par = {
        nobox: 1,
        genome,
        holder: blockholder,
        tklst: arg.selectGenomeWithTklst
      };
      const ci = coordInput;
      if (ci.chr) {
        const [nocount, hascount] = contigNameNoChr2(genome, [ci.chr]);
        if (nocount + hascount == 0) throw "Invalid chromosome name: " + ci.chr;
        const chr = nocount ? "chr" + ci.chr : ci.chr;
        if (Number.isInteger(ci.pos)) {
          if (!ci.ref) throw "Reference allele missing from variant string";
          if (!ci.alt) throw "Alternative allele missing from variant string";
          par.chr = chr;
          par.start = ci.pos - variantFlankingSize;
          par.stop = ci.pos + variantFlankingSize;
          for (const tk of par.tklst) {
            if (tk.type == "bam") {
              tk.variants = [
                {
                  chr,
                  pos: ci.pos - 1,
                  // convert 1-based to 0-based
                  ref: ci.ref,
                  alt: ci.alt
                }
              ];
            }
          }
        } else {
          if (!Number.isInteger(ci.start) || !Number.isInteger(ci.stop)) throw "non-integer start/stop";
          par.chr = chr;
          par.start = ci.start;
          par.stop = ci.stop;
        }
      } else {
        const d = genome.defaultcoord;
        par.chr = d.chr;
        par.start = d.start;
        par.stop = d.stop;
      }
      first_genetrack_tolist(genome, par.tklst);
      const _ = await import("./block-TTN2IQAH.js");
      new _.Block(par);
    } catch (e) {
      window.alert(e.message || e);
    }
  }
}
function addGenomeSelect(div, genomes) {
  const select = div.append("select");
  for (const gn in genomes) {
    select.append("option").text(gn);
  }
  return select;
}
export {
  init
};
//# sourceMappingURL=selectGenomeWithTklst-OSB7B6L3.js.map
