import {
  spliceevent_phrase_default
} from "./chunk-WSDUD52Y.js";
import {
  font
} 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 {
  IN_frame,
  exoncolor
} 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/spliceevent.exonskip.diagram.js
function spliceevent_exonskip_diagram_default(arg) {
  const evt = arg.event;
  const holder = arg.holder.append("div").style("vertical-align", "top");
  if (!evt.isaltexon && !evt.isskipexon) {
    holder.append("div").text(".isskipexon or .isaltexon is not set for event");
    return;
  }
  if (!arg.nophrase) {
    holder.append("div").html(spliceevent_phrase_default(evt));
  }
  const diagram = arg.holder.append("svg").style("display", "inline-block");
  const canonicaljunctionreadcounttext = /* @__PURE__ */ new Map();
  const canonicaljlst = [];
  const exonwidth = 40;
  const exonheight = 20;
  const junctionheight = 20;
  const intronwidth = 20;
  const xpad = 20;
  const ypad = 20;
  const fontsize = 14;
  const diagramg = diagram.append("g").attr("transform", "translate(" + xpad + "," + ypad + ")");
  const skipexonidxbegin = evt.skippedexon[0];
  const skipexonidxend = evt.skippedexon[evt.skippedexon.length - 1];
  let showup1exon = false;
  let showdown1exon = false;
  if (evt.isskipexon) {
    if (skipexonidxbegin > 1) {
      showup1exon = true;
    }
    if (evt.down1junction) {
      showdown1exon = true;
    }
  }
  diagram.attr(
    "width",
    xpad + (showup1exon ? exonwidth + intronwidth : 0) + exonwidth + intronwidth + (exonwidth + intronwidth) * evt.skippedexon.length + exonwidth + (showdown1exon ? exonwidth + intronwidth : 0) + xpad
  ).attr("height", ypad + fontsize + junctionheight + exonheight + junctionheight + fontsize + ypad);
  const exony = fontsize + junctionheight;
  const sample2jrc = /* @__PURE__ */ new Map();
  let xoff = 0;
  if (showup1exon) {
    diagramg.append("rect").attr("x", xoff).attr("y", exony).attr("width", exonwidth).attr("height", exonheight).attr("fill", exoncolor);
    diagramg.append("text").text("e" + (skipexonidxbegin - 1)).attr("x", xoff + exonwidth / 2).attr("y", exony + exonheight / 2).attr("text-anchor", "middle").attr("dominant-baseline", "central").attr("fill", "white").attr("font-size", fontsize);
    xoff += exonwidth + intronwidth;
    let nojunction = true;
    let readcount = "?";
    if (evt.up1junction) {
      if (evt.junctionB.data) {
        if (evt.junctionB.data.length == 1) {
          const sampleid = evt.junctionB.data[0].tkid;
          if (evt.up1junction.data) {
            for (const sampledata of evt.up1junction.data) {
              if (sampledata.tkid == sampleid) {
                nojunction = false;
                readcount = sampledata.v;
                break;
              }
            }
          }
        } else if (evt.junctionB.data.length > 1) {
          let readcountsum = 0;
          let samplecount = 0;
          for (const sample1 of evt.junctionB.data) {
            for (const sample2 of evt.up1junction.data) {
              if (sample1.tkid == sample2.tkid) {
                readcountsum += sample2.v;
                samplecount++;
                break;
              }
            }
          }
          if (samplecount > 0) {
            nojunction = false;
            readcount = Math.ceil(readcountsum / samplecount);
          }
        }
      } else {
        nojunction = false;
        readcount = evt.up1junction.v;
      }
    }
    diagramg.append("path").attr(
      "d",
      "M" + (xoff - intronwidth) + "," + (exony + exonheight) + "L" + (xoff - intronwidth / 2) + "," + (exony + exonheight + junctionheight) + "L" + xoff + "," + (exony + exonheight)
    ).attr("stroke", exoncolor).attr("stroke-dasharray", nojunction ? "2,2" : "none").attr("fill", "none");
    if (!nojunction) {
      const text = diagramg.append("text").text(readcount).attr("x", xoff - intronwidth / 2).attr("y", exony + exonheight + junctionheight + 2).attr("font-size", fontsize).attr("fill", "black").attr("font-family", font).attr("text-anchor", "middle").attr("dominant-baseline", "hanging");
      canonicaljunctionreadcounttext.set(evt.up1junction.start + "." + evt.up1junction.stop, text);
      canonicaljlst.push([evt.up1junction.start, evt.up1junction.stop]);
    }
  }
  diagramg.append("rect").attr("x", xoff).attr("y", exony).attr("width", exonwidth).attr("height", exonheight).attr("fill", exoncolor);
  diagramg.append("text").text("e" + skipexonidxbegin).attr("x", xoff + exonwidth / 2).attr("y", exony + exonheight / 2).attr("text-anchor", "middle").attr("dominant-baseline", "central").attr("fill", "white").attr("font-size", fontsize);
  xoff += exonwidth + intronwidth;
  for (let exonidx = skipexonidxbegin; exonidx <= skipexonidxend + 1; exonidx++) {
    const islastone = exonidx == skipexonidxend + 1;
    diagramg.append("rect").attr("x", xoff).attr("y", exony).attr("width", exonwidth).attr("height", exonheight).attr("fill", islastone ? exoncolor : "none").attr("stroke", islastone ? "none" : exoncolor).attr("shape-rendering", "crispEdges");
    diagramg.append("text").text("e" + (exonidx + 1)).attr("x", xoff + exonwidth / 2).attr("y", exony + exonheight / 2).attr("text-anchor", "middle").attr("dominant-baseline", "central").attr("fill", islastone ? "white" : exoncolor).attr("font-size", fontsize);
    const thisjunction = evt.junctionAlst[exonidx - skipexonidxbegin];
    diagramg.append("path").attr(
      "d",
      "M" + (xoff - intronwidth) + "," + (exony + exonheight) + "L" + (xoff - intronwidth / 2) + "," + (exony + exonheight + junctionheight) + "L" + xoff + "," + (exony + exonheight)
    ).attr("stroke", exoncolor).attr("stroke-dasharray", thisjunction ? "none" : "2,2").attr("fill", "none");
    if (thisjunction) {
      let readcount = "?";
      if (evt.junctionB.data) {
        if (evt.junctionB.data.length == 1) {
          if (!thisjunction.data) {
            console.error(".data missing from junctionA");
          } else {
            for (const sampledata of thisjunction.data) {
              if (sampledata.tkid == evt.junctionB.data[0].tkid) {
                readcount = sampledata.v;
                break;
              }
            }
          }
        } else if (evt.junctionB.data.length > 1) {
          readcount = "e" + exonidx + "-" + (exonidx + 1);
          for (const thissample of evt.junctionB.data) {
            if (!sample2jrc.has(thissample.tkid)) {
              sample2jrc.set(thissample.tkid, {
                sampleobj: thissample,
                readcountlst: []
              });
            }
            let thissamplereadcount = 0;
            for (const sampledata of thisjunction.data) {
              if (sampledata.tkid == thissample.tkid) {
                thissamplereadcount = sampledata.v;
                break;
              }
            }
            sample2jrc.get(thissample.tkid).readcountlst.push(thissamplereadcount);
          }
        }
      } else {
        readcount = thisjunction.v;
      }
      const text = diagramg.append("text").text(readcount).attr("x", xoff - intronwidth / 2).attr("y", exony + exonheight + junctionheight + 2).attr("font-size", fontsize).attr("fill", "black").attr("font-family", font).attr("text-anchor", "middle").attr("dominant-baseline", "hanging");
      canonicaljunctionreadcounttext.set(thisjunction.start + "." + thisjunction.stop, text);
      canonicaljlst.push([thisjunction.start, thisjunction.stop]);
    }
    xoff += exonwidth + intronwidth;
  }
  {
    const midx = xoff - exonwidth - intronwidth - ((exonwidth + intronwidth) * evt.skippedexon.length + intronwidth) / 2;
    diagramg.append("path").attr(
      "d",
      "M" + ((showup1exon ? exonwidth + intronwidth : 0) + exonwidth) + "," + exony + "L" + midx + "," + (exony - junctionheight) + "L" + (xoff - exonwidth - intronwidth) + "," + exony
    ).attr("fill", "none").attr("stroke", evt.color);
    let jBreadcount = "?";
    if (evt.junctionB.data) {
      if (evt.junctionB.data.length == 1) {
        jBreadcount = evt.junctionB.data[0].v;
      } else {
        jBreadcount = "e" + skipexonidxbegin + "-" + (skipexonidxend + 2);
        for (const thissample of evt.junctionB.data) {
          if (!sample2jrc.has(thissample.tkid)) {
            console.error("sample has junctionB but no junctionA! " + thissample.tkid);
            continue;
          }
          let readcount = 0;
          for (const sampledata of evt.junctionB.data) {
            if (sampledata.tkid == thissample.tkid) {
              readcount = sampledata.v;
              break;
            }
          }
          sample2jrc.get(thissample.tkid).readcountlst.unshift(readcount);
        }
      }
    } else {
      jBreadcount = evt.junctionB.v;
    }
    if (typeof evt.frame == "boolean") {
      jBreadcount += ", " + (evt.frame == IN_frame ? "in frame" : "out of frame");
    }
    const bclab = diagramg.append("text").text(jBreadcount).attr("x", midx).attr("y", exony - junctionheight - 2).attr("font-size", fontsize).attr("fill", evt.color).attr("font-family", font).attr("text-anchor", "middle");
  }
  if (showdown1exon) {
    diagramg.append("rect").attr("x", xoff).attr("y", exony).attr("width", exonwidth).attr("height", exonheight).attr("fill", exoncolor);
    diagramg.append("text").text("e" + (skipexonidxend + 3)).attr("x", xoff + exonwidth / 2).attr("y", exony + exonheight / 2).attr("text-anchor", "middle").attr("dominant-baseline", "central").attr("fill", "white").attr("font-size", fontsize);
    let nojunction = true;
    let readcount = "?";
    if (evt.down1junction) {
      if (evt.junctionB.data) {
        if (evt.junctionB.data.length == 1) {
          const sampleid = evt.junctionB.data[0].tkid;
          if (evt.down1junction.data) {
            for (const sampledata of evt.down1junction.data) {
              if (sampledata.tkid == sampleid) {
                nojunction = false;
                readcount = sampledata.v;
                break;
              }
            }
          }
        } else if (evt.junctionB.data.length > 1) {
          let readcountsum = 0;
          let samplecount = 0;
          for (const sample1 of evt.junctionB.data) {
            for (const sample2 of evt.down1junction.data) {
              if (sample1.tkid == sample2.tkid) {
                readcountsum += sample2.v;
                samplecount++;
                break;
              }
            }
          }
          if (samplecount > 0) {
            nojunction = false;
            readcount = Math.ceil(readcountsum / samplecount);
          }
        }
      } else {
        nojunction = false;
        readcount = evt.down1junction.v;
      }
    }
    diagramg.append("path").attr(
      "d",
      "M" + (xoff - intronwidth) + "," + (exony + exonheight) + "L" + (xoff - intronwidth / 2) + "," + (exony + exonheight + junctionheight) + "L" + xoff + "," + (exony + exonheight)
    ).attr("stroke", exoncolor).attr("stroke-dasharray", nojunction ? "2,2" : "none").attr("fill", "none");
    if (!nojunction) {
      const text = diagramg.append("text").text(readcount).attr("x", xoff - intronwidth / 2).attr("y", exony + exonheight + junctionheight + 2).attr("font-size", fontsize).attr("fill", "black").attr("font-family", font).attr("text-anchor", "middle").attr("dominant-baseline", "hanging");
      canonicaljunctionreadcounttext.set(evt.down1junction.start + "." + evt.down1junction.stop, text);
      canonicaljlst.push([evt.down1junction.start, evt.down1junction.stop]);
    }
  }
  return [canonicaljunctionreadcounttext, canonicaljlst];
}
export {
  spliceevent_exonskip_diagram_default as default
};
//# sourceMappingURL=spliceevent.exonskip.diagram-LAOIOIST.js.map
