import {
  block_init_default
} from "./chunk-67DBIM6H.js";
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 {
  dofetch3
} from "./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";

// gdc/lollipop.js
var tip = new Menu({ padding: "" });
async function init(arg, holder, genomes) {
  const useGenome = arg.genome || "hg38";
  const useDslabel = arg.dslabel || "GDC";
  const genome = genomes[useGenome];
  if (!genome) throw useGenome + " missing";
  if (arg.geneSearch4GDCmds3.onloadalltk_always && typeof arg.geneSearch4GDCmds3.onloadalltk_always != "function")
    throw "arg.geneSearch4GDCmds3.onloadalltk_always not function";
  if (arg.geneSearch4GDCmds3.postRender && typeof arg.geneSearch4GDCmds3.postRender != "function")
    throw "arg.geneSearch4GDCmds3.postRender not function";
  holder.selectAll(".sja_lollipop_holder").remove();
  const mainDiv = holder.append("div").attr("class", "sja_lollipop_holder");
  const geneInputDiv = mainDiv.append("div").style("margin-left", "20px");
  geneInputDiv.append("div").text(
    arg.geneSearch4GDCmds3.hardcodeCnvOnly ? `To view ${useDslabel} CNV segments over a gene or region, enter genomic position (chr11:108195437-108267444), dbSNP accesion, or gene name (MYC).` : `To view ${useDslabel} mutations on a gene, enter one of gene symbol (MYC), alias (c-Myc), GENCODE accession (ENSG00000136997, ENST00000621592), or RefSeq accession (NM_002467).`
  );
  const graphDiv = mainDiv.append("div").attr("class", "sja_geneSearch4GDCmds3_blockdiv");
  const searchOpt = {
    genome,
    tip,
    row: geneInputDiv,
    callback: launchView,
    geneSymbol: arg.geneSymbol,
    triggerSearch: arg.geneSymbol && arg.geneSearch4GDCmds3?.hardcodeCnvOnly == true,
    hideInputBeforeCallback: arg.geneSearch4GDCmds3?.hardcodeCnvOnly == true
  };
  if (!arg.geneSearch4GDCmds3.hardcodeCnvOnly) {
    searchOpt.searchOnly = "gene";
  }
  const coordInput = addGeneSearchbox(searchOpt);
  let userSelection;
  await arg.geneSearch4GDCmds3.postRender?.({ tip });
  if (arg.state) {
    if (arg.state.userSelection) launchView(false, arg.state.userSelection);
    delete arg.state;
  }
  async function launchView(triggeredByInput = true, userSelection2) {
    const pa = {
      // param for instantiating block
      genome,
      holder: graphDiv,
      gmmode: "exon only",
      nobox: 1,
      hide_dsHandles: arg.hide_dsHandles,
      onloadalltk_always: arg.geneSearch4GDCmds3.onloadalltk_always
    };
    if (arg.tracks) {
      pa.tklst = arg.tracks;
    } else {
      const tk = {
        type: "mds3",
        dslabel: useDslabel,
        allow2selectSamples: arg.allow2selectSamples,
        filter0: arg.filter0
      };
      pa.tklst = [tk];
      if (arg.geneSearch4GDCmds3.hardcodeCnvOnly) {
        tk.hardcodeCnvOnly = 1;
        delete pa.gmmode;
        first_genetrack_tolist(pa.genome, pa.tklst);
      }
      if (arg.geneSearch4GDCmds3.snvIndelOnly) {
        tk.snvIndelOnly = 1;
      }
    }
    if (userSelection2) {
      if (arg.geneSearch4GDCmds3.hardcodeCnvOnly) {
        if (typeof userSelection2 != "object") throw "userSelection not object when pa.block is true";
        pa.chr = userSelection2.chr;
        pa.start = userSelection2.start;
        pa.stop = userSelection2.stop;
        if (!pa.chr || !Number.isInteger(pa.start) || !Number.isInteger(pa.stop))
          throw "userSelection not {chr,start,stop}";
      } else {
        if (typeof userSelection2 != "string") throw "userSelection should be string when pa.block is not true";
        pa.query = userSelection2;
      }
    } else {
      if (arg.geneSearch4GDCmds3.hardcodeCnvOnly) {
        if (!coordInput.chr || !Number.isInteger(coordInput.start) || !Number.isInteger(coordInput.stop)) {
          if (triggeredByInput) throw "coordInput.chr/start/stop missing";
        }
        pa.chr = coordInput.chr;
        pa.start = coordInput.start;
        pa.stop = coordInput.stop;
      } else {
        if (!coordInput.geneSymbol) {
          if (triggeredByInput) throw "coordInput.geneSymbol missing";
        }
        const gmlst = (await dofetch3(`genelookup?deep=1&input=${coordInput.geneSymbol}&genome=${useGenome}`)).gmlst;
        if (!Array.isArray(gmlst) || gmlst.length == 0) throw "gmlst is not non-empty array";
        pa.query = getSelectedIsoform(coordInput, gmlst);
        if (gmlst.some((i) => i.coding)) pa.gmmode = "protein";
      }
    }
    graphDiv.selectAll("*").remove();
    if (!arg.geneSearch4GDCmds3.hardcodeCnvOnly) return await block_init_default(pa);
    const _ = await import("./block-TTN2IQAH.js");
    return new _.Block(pa);
  }
  const api = {
    update: (_arg) => {
      Object.assign(arg, _arg);
      launchView(false);
    },
    getState: () => ({ userSelection })
  };
  return api;
}
function getSelectedIsoform(coordInput, gmlst) {
  if (coordInput.fromWhat) {
    if (gmlst.some((i) => i.isoform.toUpperCase() == coordInput.fromWhat.toUpperCase())) {
      return coordInput.fromWhat;
    }
    if (coordInput.fromWhat.toUpperCase().startsWith("ENSG")) {
      for (const i of gmlst) {
        if (i.isdefault && i.isoform.startsWith("ENST")) return i.isoform;
      }
    }
  }
  const defaultIsoform = gmlst.find((i) => i.isdefault);
  if (defaultIsoform) return defaultIsoform.isoform;
  return gmlst[0].isoform;
}
export {
  init
};
//# sourceMappingURL=lollipop-LQCQBTSX.js.map
