import {
  first_genetrack_tolist,
  gmlst2loci
} 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 {
  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";

// plots/plot.ssgq.js
async function plotSingleSampleGenomeQuantification(termdbConfig, dslabel, queryKey, sample, holder, genomeObj, geneName, showError = true) {
  const loadingDiv = holder.append("div").text("Loading...");
  try {
    if (typeof termdbConfig?.queries?.singleSampleGenomeQuantification != "object")
      throw "termdbConfig.queries.singleSampleGenomeQuantification{} missing, cannot plot";
    const q = termdbConfig.queries.singleSampleGenomeQuantification[queryKey];
    if (!q) throw "invalid queryKey";
    if (typeof sample != "object") throw "sample{} not object";
    if (typeof genomeObj != "object") throw "genomeObj{} not object";
    const body = {
      genome: genomeObj.name,
      dslabel,
      devicePixelRatio: window.devicePixelRatio > 1 ? window.devicePixelRatio : 1,
      singleSampleGenomeQuantification: { dataType: queryKey, sample: sample[q.sample_id_key] }
    };
    const data = await dofetch3("mds3", { body });
    if (data.error) throw data.error;
    const q2 = termdbConfig.queries.singleSampleGbtk?.[q.singleSampleGbtk];
    holder.append("div").attr("data-testid", "sjpp_ssgq_sandbox").text(q.description || queryKey);
    if (q2) {
      holder.append("div").attr("data-testid", "sjpp_ssgq_intro_text").text(`Click a chromosomal position to zoom in and view ${q2.description || q.singleSampleGbtk}`);
    }
    const img = holder.append("img").attr("data-testid", "sjpp_ssgq_img").attr("width", data.canvasWidth).attr("height", data.canvasHeight).attr("src", data.src);
    loadingDiv.remove();
    if (!q2) return;
    let bb;
    if (geneName) {
      const geneData = await dofetch3("genelookup", {
        body: { genome: genomeObj.name, input: geneName, deep: 1 }
      });
      if (geneData.error) throw geneData.error;
      if (geneData.gmlst && geneData.gmlst.length) {
        const locs = gmlst2loci(geneData.gmlst);
        const chr = locs[0].chr;
        const start = Math.max(0, locs[0].start - (locs[0].stop - locs[0].start));
        const chrLen = data.chrLst.filter((c) => c.chr == chr)[0].chrLen;
        const stop = Math.min(chrLen, locs[0].stop + (locs[0].stop - locs[0].start));
        bb = await plotSingleSampleGbtk(dslabel, sample, holder, genomeObj, q, q2, chr, start, stop);
      }
    }
    img.on("click", async (event) => {
      const x = event.offsetX - data.xoff;
      let chr, chrLen, position;
      for (const c of data.chrLst) {
        if (c.xStart <= x && c.xStop >= x) {
          chr = c.chr;
          chrLen = c.chrLen;
          position = Math.ceil(c.chrLen / (c.xStop - c.xStart) * (x - c.xStart));
          break;
        }
      }
      if (!chr) return;
      const start = Math.max(0, position - 5e5), stop = Math.min(position + 5e5, chrLen);
      if (bb) {
        bb.jump_1basedcoordinate({ chr, start, stop });
        return;
      }
      bb = await plotSingleSampleGbtk(dslabel, sample, holder, genomeObj, q, q2, chr, start, stop);
    });
    return true;
  } catch (e) {
    if (showError) loadingDiv.text("Error: " + (e.message || e));
    else loadingDiv.remove();
    return false;
  }
}
async function plotSingleSampleGbtk(dslabel, sample, holder, genomeObj, q, q2, chr, start, stop) {
  const body = {
    genome: genomeObj.name,
    dslabel,
    singleSampleGbtk: { dataType: q.singleSampleGbtk, sample: sample[q2.sample_id_key] }
  };
  const d2 = await dofetch3("mds3", { body });
  if (!d2.path) return;
  const tklst = [
    {
      type: "bigwig",
      name: sample[q2.sample_id_key],
      file: d2.path,
      height: 100,
      scale: { min: q2.min, max: q2.max },
      pcolor: q.positiveColor,
      ncolor: q.negativeColor
    }
  ];
  first_genetrack_tolist(genomeObj, tklst);
  const bb = new (await import("./block-TTN2IQAH.js")).Block({
    genome: genomeObj,
    holder: holder.append("div"),
    nobox: true,
    tklst,
    chr,
    start,
    stop
  });
  return bb;
}
export {
  plotSingleSampleGenomeQuantification
};
//# sourceMappingURL=plot.ssgq-WJHGMXW5.js.map
