import {
  axisstyle,
  font,
  make_table_2col
} 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 {
  category10_default
} from "./chunk-KSGA62R2.js";
import {
  axisBottom,
  axisLeft
} from "./chunk-LOZEKOES.js";
import "./chunk-TOU7EVFQ.js";
import {
  format,
  linear,
  ordinal
} from "./chunk-OAWQ6LOO.js";
import "./chunk-TLT4YIG3.js";
import "./chunk-KYBIQBXE.js";
import {
  select_default
} from "./chunk-I6Y4O3RR.js";
import "./chunk-OMR2DT66.js";
import "./chunk-DQC5FFGV.js";
import "./chunk-HFNDKYVF.js";

// src/old/plot.vaf2cov.js
function plot_vaf2cov(arg) {
  for (const i of arg.data) {
    if (!i.sampleobj) i.sampleobj = {};
  }
  let width = arg.width || 200;
  let height = arg.height || 200;
  const gray = arg.color || "#999";
  let marksize;
  let maxtotal = arg.maxtotal || 0;
  if (arg.automax) {
    for (const i of arg.data) {
      maxtotal = Math.max(maxtotal, i.total);
    }
  }
  let maxf = 1;
  const xbin = [];
  const ybin = [];
  const bincount = arg.bincount || 20;
  for (let i = 0; i < bincount; i++) {
    xbin.push(0);
    ybin.push(0);
  }
  {
    const xbs = maxtotal / bincount;
    const ybs = maxf / bincount;
    for (const i of arg.data) {
      if (i.total >= maxtotal) {
        xbin[bincount - 1]++;
      } else {
        xbin[Math.floor(i.total / xbs)]++;
      }
      ybin[Math.floor((i.maf == 1 ? 0.99 : i.maf) / ybs)]++;
    }
  }
  const xbinmax = Math.max(...xbin);
  const ybinmax = Math.max(...ybin);
  const xscale = linear().domain([0, maxtotal]), yscale = linear().domain([0, maxf]), xbinscale = linear().domain([0, xbinmax]), ybinscale = linear().domain([0, ybinmax]);
  const svg = arg.holder.append("svg").style("margin", "10px");
  const xlab = svg.append("text").text("Coverage").attr("text-anchor", "middle").attr("fill", gray).attr("font-family", font);
  const ylabg = svg.append("g");
  const ylab = ylabg.append("text").text("VAF").attr("text-anchor", "middle").attr("dominant-baseline", "middle").attr("fill", gray).attr("font-family", font).attr("transform", "rotate(-90)");
  const xaxis = svg.append("g");
  const yaxis = svg.append("g");
  const boxg = svg.append("g");
  const box = boxg.append("rect").attr("stroke", gray).attr("stroke-dasharray", "2,2").attr("fill", "none").attr("shape-rendering", "crispEdges");
  const midline = boxg.append("line").attr("stroke", gray).attr("stroke-dasharray", "2,2").attr("shape-rendering", "crispEdges");
  const ybing = svg.append("g");
  const ybinbar = ybing.selectAll().data(ybin).enter().append("rect");
  const ybinaxis = svg.append("g");
  const xbing = svg.append("g");
  const xbinbar = xbing.selectAll().data(xbin).enter().append("rect");
  const xbinaxis = svg.append("g");
  let gtg = null, gtlab, gt, gtl1, gtl2, gtname;
  if (arg.genotype) {
    const gtcolor = ordinal(category10_default);
    const set = /* @__PURE__ */ new Set();
    for (const d of arg.data) {
      if (d.genotype) {
        set.add(d.genotype);
        d.color = gtcolor(d.genotype);
      }
    }
    const lst = [...set];
    gtg = svg.append("g");
    gtlab = gtg.append("text").text("Genotype").attr("dominant-baseline", "central").attr("font-family", font);
    gt = gtg.selectAll().data(lst).enter().append("g");
    gtl1 = gt.append("line").attr("stroke", (d) => gtcolor(d));
    gtl2 = gt.append("line").attr("stroke", (d) => gtcolor(d));
    gtname = gt.append("text").text((d) => d).attr("fill", (d) => gtcolor(d)).attr("dominant-baseline", "central").attr("font-family", font);
  }
  const spg = boxg.selectAll().data(arg.data).enter().append("g");
  const spgl1 = spg.append("line").attr("stroke-opacity", 0.6).attr("stroke", (d) => d.color ? d.color : d.sampleobj.color || arg.samplecolor).each(function(d) {
    d.crosshair1 = select_default(this);
  });
  const spgl2 = spg.append("line").attr("stroke-opacity", 0.6).attr("stroke", (d) => d.color ? d.color : d.sampleobj.color || arg.samplecolor).each(function(d) {
    d.crosshair2 = select_default(this);
  });
  const spgkick = spg.append("circle").attr("fill", "white").attr("fill-opacity", 0).on("mouseover", (event, d) => {
    d.crosshair1.attr("stroke-width", 3).attr("x1", -marksize - 2).attr("y1", -marksize - 2).attr("x2", marksize + 2).attr("y2", marksize + 2);
    d.crosshair2.attr("stroke-width", 3).attr("x1", marksize + 2).attr("y1", -marksize - 2).attr("x2", -marksize - 2).attr("y2", marksize + 2);
    arg.tip.clear();
    arg.tip.show(event.clientX, event.clientY);
    const lst = [{ k: "mut", v: d.mut }, { k: "total", v: d.total }];
    if (d.genotype) {
      lst.push({ k: "genotype", v: d.genotype });
    }
    for (const k in d.sampleobj) {
      if (k == "color") continue;
      lst.push({ k, v: d.sampleobj[k] });
    }
    make_table_2col(arg.tip.d, lst).style("margin", "none");
    if (arg.mouseover) {
      arg.mouseover(d);
    }
  }).on("mouseout", (event, d) => {
    d.crosshair1.attr("stroke-width", 1).attr("x1", -marksize).attr("y1", -marksize).attr("x2", marksize).attr("y2", marksize);
    d.crosshair2.attr("stroke-width", 1).attr("x1", marksize).attr("y1", -marksize).attr("x2", -marksize).attr("y2", marksize);
    arg.tip.hide();
    if (arg.mouseout) {
      arg.mouseout(d);
    }
  });
  if (arg.click) {
    spgkick.on("click", (event, d) => {
      arg.click(d);
    });
  }
  const drag = svg.append("text").text("drag to resize").attr("class", "sja_clbtext").attr("font-size", 13).attr("text-anchor", "end").attr("fill", gray).on("mousedown", (event) => {
    event.preventDefault();
    const b = select_default(document.body);
    const x0 = event.clientX, y0 = event.clientY, width0 = width, height0 = height;
    b.on("mousemove", (event2) => {
      width = width0 + event2.clientX - x0;
      height = height0 + event2.clientY - y0;
      resize();
    });
    b.on("mouseup", () => {
      b.on("mousemove", null).on("mouseup", null);
    });
  });
  function resize() {
    const fontsize = Math.max(12, Math.min(width, height) / 25);
    const pad2 = height / 20;
    marksize = Math.ceil(fontsize / 3);
    const ticksize = marksize, axisw = ticksize + fontsize * 3, axish = ticksize + 20, pad = fontsize * 1.3, pad0 = fontsize * 1.6, barheight = height / 5, barwidth = width / 5;
    xscale.range([0, width]);
    yscale.range([height, 0]);
    xbinscale.range([barheight, 0]);
    ybinscale.range([0, barwidth]);
    svg.attr("width", fontsize + axisw + pad0 + width + pad + barwidth + pad2 + ticksize).attr("height", fontsize / 2 + barheight + pad + height + pad0 + axish + ticksize + fontsize);
    xlab.attr("font-size", fontsize).attr("x", fontsize + axisw + pad0 + width / 2).attr("y", fontsize / 2 + barheight + pad + height + pad0 + axish + ticksize + fontsize - 5);
    ylabg.attr("transform", "translate(" + fontsize + "," + (fontsize / 2 + barheight + pad + height / 2) + ")");
    ylab.attr("font-size", fontsize);
    xaxis.attr(
      "transform",
      "translate(" + (fontsize + axisw + pad0) + "," + (fontsize / 2 + barheight + pad + height + pad0) + ")"
    ).call(
      axisBottom().scale(xscale).ticks(4).tickSize(ticksize)
    );
    axisstyle({
      axis: xaxis,
      color: gray,
      fontsize,
      showline: true
    });
    yaxis.attr("transform", "translate(" + (fontsize + axisw) + "," + (fontsize / 2 + barheight + pad) + ")").call(
      axisLeft().scale(yscale).ticks(5).tickSize(ticksize)
    );
    axisstyle({
      axis: yaxis,
      color: gray,
      fontsize,
      showline: true
    });
    boxg.attr("transform", "translate(" + (fontsize + axisw + pad0) + "," + (fontsize / 2 + barheight + pad) + ")");
    box.attr("width", width).attr("height", height);
    midline.attr("y1", height / 2).attr("x2", width).attr("y2", height / 2);
    spg.attr(
      "transform",
      (d) => "translate(" + xscale(d.total > maxtotal ? maxtotal : d.total) + "," + yscale(d.maf) + ")"
    );
    spgl1.attr("x1", -marksize).attr("y1", -marksize).attr("x2", marksize).attr("y2", marksize);
    spgl2.attr("x1", marksize).attr("y1", -marksize).attr("x2", -marksize).attr("y2", marksize);
    spgkick.attr("r", marksize);
    ybing.attr(
      "transform",
      "translate(" + (fontsize + axisw + pad0 + width + pad) + "," + (fontsize / 2 + barheight + pad + height) + ")"
    );
    const binh = height / bincount;
    ybinbar.attr("y", (d, i) => -binh * (i + 1)).attr("width", (d) => ybinscale(d)).attr("height", binh).attr("fill", gray);
    ybinaxis.attr(
      "transform",
      "translate(" + (fontsize + axisw + pad0 + width + pad) + "," + (fontsize / 2 + barheight + pad + height + pad0) + ")"
    ).call(
      axisBottom().scale(ybinscale).tickValues([0, ybinmax]).tickFormat(format("d"))
    );
    axisstyle({
      axis: ybinaxis,
      color: gray,
      showline: true
    });
    xbing.attr("transform", "translate(" + (fontsize + axisw + pad0) + "," + (fontsize / 2 + barheight) + ")");
    const binw = width / bincount;
    xbinbar.attr("x", (d, i) => binw * i).attr("y", (d) => xbinscale(d) - barheight).attr("height", (d) => barheight - xbinscale(d)).attr("width", binw).attr("fill", gray);
    xbinaxis.attr("transform", "translate(" + (fontsize + axisw) + "," + fontsize / 2 + ")").call(
      axisLeft().scale(xbinscale).tickValues([0, xbinmax]).tickFormat(format("d"))
    );
    axisstyle({
      axis: xbinaxis,
      color: gray,
      showline: true
    });
    drag.attr("x", fontsize + axisw + pad0 + width + pad + barwidth + pad2 + ticksize - 5).attr("y", fontsize / 2 + barheight + pad + height + pad0 + axish + ticksize + fontsize - 5);
    if (gtg) {
      gtg.attr("transform", "translate(" + (fontsize + axisw + pad0 + width + pad) + "," + fontsize / 2 + ")");
      gtlab.attr("font-size", fontsize);
      gt.attr("transform", (d, i) => {
        return "translate(0," + (fontsize / 2 + 3 + (fontsize + 1) * i + fontsize / 2) + ")";
      });
      gtl1.attr("y1", -fontsize / 2).attr("x2", fontsize).attr("y2", fontsize / 2);
      gtl2.attr("x1", fontsize).attr("y1", -fontsize / 2).attr("y2", fontsize / 2);
      gtname.attr("x", fontsize + 5).attr("font-size", fontsize);
    }
  }
  resize();
  return spg;
}
export {
  plot_vaf2cov as default
};
//# sourceMappingURL=plot.vaf2cov-NGD5PCV4.js.map
