import {
  axisstyle,
  font,
  keyupEnter,
  make_radios
} 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 {
  axisLeft,
  axisRight
} from "./chunk-LOZEKOES.js";
import "./chunk-TOU7EVFQ.js";
import {
  linear
} from "./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/block.tk.aicheck.js
function makeTk(tk, block) {
  tk.img = tk.glider.append("image");
  if (!tk.dotsize) {
    tk.dotsize = 1;
  }
  if (!tk.coveragemax) {
    tk.coveragemax = 100;
  }
  if (!tk.vafheight) {
    tk.vafheight = 50;
  }
  if (!tk.coverageheight) {
    tk.coverageheight = 30;
  }
  if (!tk.rowspace) {
    tk.rowspace = 5;
  }
  tk.tklabel.attr("y", 20);
  tk.Tvafaxis = tk.gleft.append("g");
  tk.Nvafaxis = tk.gleft.append("g");
  tk.aiaxis = tk.gleft.append("g");
  tk.label_tumor = tk.gleft.append("text").attr("font-family", font).attr("font-size", block.labelfontsize).attr("dominant-baseline", "central").attr("text-anchor", "end").attr("x", block.tkleftlabel_xshift).attr("fill-opacity", 0.6).text("Tumor");
  tk.label_germline = tk.gleft.append("text").attr("font-family", font).attr("font-size", block.labelfontsize).attr("dominant-baseline", "central").attr("text-anchor", "end").attr("x", block.tkleftlabel_xshift).attr("fill-opacity", 0.6).text("Germline");
  tk.label_ai = tk.gleft.append("text").attr("font-family", font).attr("font-size", block.labelfontsize).attr("dominant-baseline", "central").attr("text-anchor", "end").attr("x", block.tkleftlabel_xshift).attr("fill-opacity", 0.6).text("abs(T-G)");
  tk.Tcovaxis = tk.gright.append("g");
  tk.Ncovaxis = tk.gright.append("g");
  tk.label_tumorcoverage = tk.gright.append("text").attr("font-family", font).attr("font-size", block.labelfontsize).attr("dominant-baseline", "central").attr("x", 10).attr("fill-opacity", 0.6).text("T coverage");
  tk.label_germlinecoverage = tk.gright.append("text").attr("font-family", font).attr("font-size", block.labelfontsize).attr("dominant-baseline", "central").attr("x", 10).attr("fill-opacity", 0.6).text("G coverage");
  tk.config_handle = block.maketkconfighandle(tk).on("click", (event) => {
    tk.tkconfigtip.clear().showunder(tk.config_handle.node());
    configPanel(tk, block);
  });
}
function tkarg(tk, block, width, rglst) {
  const a = {
    rglst: rglst || block.tkarg_rglst(),
    genome: block.genome.name,
    regionspace: block.regionspace,
    width: width || block.width,
    coveragemax: tk.coveragemax,
    gtotalcutoff: tk.gtotalcutoff,
    gmafrestrict: tk.gmafrestrict,
    vafheight: tk.vafheight,
    coverageheight: tk.coverageheight,
    rowspace: tk.rowspace,
    dotsize: tk.dotsize,
    devicePixelRatio: window.devicePixelRatio > 1 ? window.devicePixelRatio : 1
  };
  if (tk.file) {
    a.file = tk.file;
  } else {
    a.url = tk.url;
    if (tk.indexURL) a.indexURL = tk.indexURL;
  }
  return a;
}
function loadTk(tk, block) {
  if (tk.uninitialized) {
    makeTk(tk, block);
    delete tk.uninitialized;
  }
  block.tkcloakon(tk);
  dofetch3("tkaicheck", { body: tkarg(tk, block) }).then((data) => {
    if (data.error) throw { message: data.error };
    const imgh = tk.vafheight * 3 + tk.rowspace * 4 + tk.coverageheight * 2;
    tk.height_main = tk.toppad + imgh + tk.bottompad;
    tk.img.attr("width", block.width).attr("height", imgh).attr("xlink:href", data.src);
    if (data.coveragemax) {
      tk.coveragemax = data.coveragemax;
    }
    if (!data.nodata) {
      const scale = linear().domain([0, 1]).range([tk.vafheight, 0]);
      let y = 0;
      axisstyle({
        axis: tk.Tvafaxis.attr("transform", "translate(0," + y + ")").call(
          axisLeft().scale(scale).tickValues([0, 1])
        ),
        color: "black",
        showline: true
      });
      tk.label_tumor.attr("y", y + tk.vafheight * 3 / 4);
      y = tk.vafheight + tk.rowspace + tk.coverageheight + tk.rowspace;
      axisstyle({
        axis: tk.Nvafaxis.attr("transform", "translate(0," + y + ")").call(
          axisLeft().scale(scale).tickValues([0, 1])
        ),
        color: "black",
        showline: true
      });
      tk.label_germline.attr("y", y + tk.vafheight / 2);
      y = 2 * (tk.vafheight + tk.rowspace + tk.coverageheight + tk.rowspace);
      axisstyle({
        axis: tk.aiaxis.attr("transform", "translate(0," + y + ")").call(axisLeft().scale(scale).tickValues([0, 1])),
        color: "black",
        showline: true
      });
      tk.label_ai.attr("y", y + tk.vafheight / 2);
      const scale2 = linear().domain([0, tk.coveragemax]).range([tk.coverageheight, 0]);
      y = tk.vafheight + tk.rowspace;
      axisstyle({
        axis: tk.Tcovaxis.attr("transform", "translate(0," + y + ")").call(
          axisRight().scale(scale2).tickValues([0, tk.coveragemax])
        ),
        color: "black",
        showline: true
      });
      tk.label_tumorcoverage.attr("y", y + tk.coverageheight / 2);
      y = 2 * (tk.vafheight + tk.rowspace) + tk.coverageheight + tk.rowspace;
      axisstyle({
        axis: tk.Ncovaxis.attr("transform", "translate(0," + y + ")").call(
          axisRight().scale(scale2).tickValues([0, tk.coveragemax])
        ),
        color: "black",
        showline: true
      });
      tk.label_germlinecoverage.attr("y", y + tk.coverageheight / 2);
    }
    return null;
  }).catch((obj) => {
    tk.img.attr("width", 1).attr("height", 1);
    if (obj.stack) {
      console.log(obj.stack);
    }
    return obj.message;
  }).then((errtext) => {
    block.tkcloakoff(tk, { error: errtext });
    block.block_setheight();
    for (const panel of tk.subpanels) {
      loadTksubpanel(tk, block, panel);
    }
  });
}
function loadTksubpanel(tk, block, panel) {
  block.tkcloakon_subpanel(panel);
  const par = tkarg(tk, block, panel.width, [
    {
      chr: panel.chr,
      start: panel.start,
      stop: panel.stop,
      width: panel.width
    }
  ]);
  dofetch3("tkaicheck", { body: par }).then((data) => {
    if (data.error) throw { message: data.error };
    panel.img.attr("width", panel.width).attr("height", tk.vafheight * 3 + tk.rowspace * 4 + tk.coverageheight * 2).attr("xlink:href", data.src);
    return null;
  }).catch((obj) => {
    panel.img.attr("width", 1).attr("height", 1);
    if (obj.stack) {
      console.log(obj.stack);
    }
    return obj.message;
  }).then((errtext) => {
    block.tkcloakoff_subpanel(panel, { error: errtext });
  });
}
function configPanel(tk, block) {
  tk.tkconfigtip.clear();
  {
    const row = tk.tkconfigtip.d.append("div").style("margin-bottom", "15px");
    row.append("span").html("Coverage max:&nbsp;");
    row.append("input").attr("type", "number").style("width", "60px").property("value", tk.coveragemax).on("keyup", (event) => {
      if (!keyupEnter(event)) return;
      const s = event.target.value;
      if (s == "") return;
      const v = Number.parseInt(s);
      if (Number.isNaN(v) || v <= 1) {
        alert("coverage max must be positive integer");
        return;
      }
      tk.coveragemax = v;
      loadTk(tk, block);
    });
  }
  {
    const row = tk.tkconfigtip.d.append("div").style("margin-bottom", "15px");
    row.append("span").html("Marker dot size:&nbsp;");
    make_radios({
      holder: row,
      options: [
        { label: "1 pixel", value: 1, checked: tk.dotsize == 1 },
        { label: "2 pixels", value: 2, checked: tk.dotsize == 2 },
        { label: "3 pixels", value: 3, checked: tk.dotsize == 3 }
      ],
      callback: (v) => {
        tk.dotsize = v;
        loadTk(tk, block);
      },
      styles: {
        "margin-right": "5px"
      }
    });
  }
  {
    const row = tk.tkconfigtip.d.append("div").style("margin-bottom", "15px");
    row.append("span").html("Filter markers by minimum total germline coverage:&nbsp;");
    row.append("input").attr("type", "number").style("width", "60px").property("value", tk.gtotalcutoff || 0).on("keyup", (event) => {
      if (!keyupEnter(event)) return;
      const s = event.target.value;
      if (s == "") return;
      const v = Number.parseInt(s);
      if (Number.isNaN(v) || v < 0) {
        alert("coverage max must be non-negative integer");
        return;
      }
      if (v == tk.gtotalcutoff) return;
      tk.gtotalcutoff = v;
      loadTk(tk, block);
    });
    row.append("div").style("font-size", ".7em").style("opacity", 0.5).text("Set to 0 to use all markers");
  }
  {
    const row = tk.tkconfigtip.d.append("div").style("margin-bottom", "15px");
    row.append("span").html("Filter markers by narrowing range of germline B-allele fraction:&nbsp;");
    row.append("input").attr("type", "number").style("width", "60px").property("value", tk.gmafrestrict || 0).on("keyup", (event) => {
      if (!keyupEnter(event)) return;
      const s = event.target.value;
      if (s == "") return;
      const v = Number.parseFloat(s);
      if (Number.isNaN(v) || v < 0 || v > 0.5) {
        alert("Must enter a value between 0 and 0.5");
        return;
      }
      if (tk.gmafrestrict == v) return;
      tk.gmafrestrict = v;
      loadTk(tk, block);
      configPanel(tk, block);
    });
    row.append("div").style("font-size", ".7em").style("opacity", 0.5).text(
      (tk.gmafrestrict ? "Keeping markers with BAF range " + tk.gmafrestrict + " to " + (1 - tk.gmafrestrict) + ". " : "") + "Set to 0 to use all markers"
    );
  }
}
export {
  loadTk,
  loadTksubpanel
};
//# sourceMappingURL=block.tk.aicheck-AIZEND45.js.map
