import {
  displaySampleTable,
  getFilterName,
  makelabel
} from "./chunk-YKXUK7HJ.js";
import "./chunk-SKMFMGCD.js";
import "./chunk-B6CSYKZJ.js";
import "./chunk-UJKW42DI.js";
import "./chunk-V2OJLJSK.js";
import "./chunk-WKNI3HRQ.js";
import "./chunk-VGYTAYFK.js";
import {
  fillbar,
  filterInit,
  getNormalRoot,
  renderTable
} from "./chunk-SA7APTJR.js";
import "./chunk-HJ6L54YS.js";
import "./chunk-LSEFWW72.js";
import "./chunk-SKREEF3H.js";
import "./chunk-HYOEWQ5P.js";
import {
  Tabs
} from "./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 {
  violinRenderer
} from "./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";

// mds3/leftlabel.sample.js
function makeSampleLabel(data, tk, block, laby) {
  if (!tk.leftlabels.doms.samples) {
    tk.leftlabels.doms.samples = makelabel(tk, block, laby);
  }
  if (data.sampleTotalNumber) {
    tk.leftlabels.doms.samples.attr("class", "sja_clbtext2").style("opacity", 1).text(`${data.sampleTotalNumber} sample${data.sampleTotalNumber > 1 ? "s" : ""}`).attr("data-testid", "sjpp_mds3tk_samples_label").on("click", async (event) => {
      tk.menutip.clear().showunder(event.target);
      await mayShowSummary(tk, block);
      const buttonrow = tk.menutip.d.append("div").style("margin", "10px");
      menu_listSamples(buttonrow, data, tk, block);
    });
  } else {
    tk.leftlabels.doms.samples.text("No samples").attr("class", "").style("opacity", 0.5).on("click", null);
  }
}
function makeSampleFilterLabel(data, tk, block, laby) {
  if (!tk.leftlabels.doms.filterObj) {
    tk.leftlabels.doms.filterObj = makelabel(tk, block, laby);
    tk.leftlabels.doms.filterObj.attr("data-testid", "sjpp_mds3tk_leftlabel_samplefilter");
  }
  tk.leftlabels.doms.filterObj.text(getFilterName(tk.filterObj)).on("click", async (event) => {
    tk.menutip.clear().showunder(event.target);
    const arg = {
      holder: tk.menutip.d.append("div").style("margin", "10px"),
      vocabApi: tk.mds.termdb.vocabApi,
      callback: (f) => {
        tk.filterObj = f;
        tk.load();
      }
    };
    mayAddGetCategoryArgs(arg, block);
    filterInit(arg).main(tk.filterObj);
  });
}
function mayAddGetCategoryArgs(arg, block) {
  if (block.usegm) {
    arg.getCategoriesArguments = { currentGeneNames: [block.usegm.name] };
  } else {
    arg.getCategoriesArguments = { rglst: structuredClone(block.rglst) };
  }
}
async function mayShowSummary(tk, block) {
  if (!tk.mds.variant2samples.twLst) {
    return;
  }
  const div = tk.menutip.d.append("div").style("margin", "10px");
  const wait = div.append("div").text("Loading...");
  try {
    const { summary } = await tk.mds.getSamples({ isSummary: true });
    tk.leftlabels.__samples_data = summary;
    wait.remove();
    await showSummary4terms(summary, div.append("div").attr("data-testid", "sja_mds3samplesummarydiv"), tk, block);
  } catch (e) {
    wait.text(`Error: ${e.message || e}`);
    if (e.stack) console.log(e.stack);
  }
}
async function showSummary4terms(data, div, tk, block) {
  const tabs = [];
  for (const { termid, numbycategory } of data) {
    tabs.push({
      label: tk.mds.variant2samples.twLst.find((i) => i.term.id == termid).term.name + (numbycategory ? `<span style="font-size:.8em;float:right;margin-left: 5px;">n=${numbycategory.length}</span>` : ""),
      keydownCallback: function(event) {
        setTimeout(() => {
          const tr = this.contentHolder.select("tbody").select("tr").node();
          if (!tr) return;
          tr.focus();
        }, 100);
      }
    });
  }
  new Tabs({
    holder: div,
    tabsPosition: "vertical",
    linePosition: "right",
    tabs
  }).main();
  for (const [i, d] of data.entries()) {
    const holder = tabs[i].contentHolder.style("padding-left", "20px");
    if (d.numbycategory) {
      holder.append("div").text("Click a category to create new track.").style("margin-bottom", "10px").style("font-size", ".8em").style("opacity", 0.5);
      showSummary4oneTerm(d.termid, holder, d.numbycategory, tk, block);
      continue;
    }
    if (d.density_data) {
      if (!Number.isFinite(d.density_data.min) || !Number.isFinite(d.density_data.max)) {
        holder.append("div").text("No data");
        continue;
      }
      holder.append("div").text("Select a range to create new track.").style("margin-bottom", "10px").style("font-size", ".8em").style("opacity", 0.5);
      showDensity4oneTerm(d.termid, holder, d, tk, block);
      continue;
    }
    throw "unknown summary data";
  }
}
function showSummary4oneTerm(termid, div, numbycategory, tk, block) {
  const tw = tk.mds.variant2samples.twLst.find((i) => i.term.id == termid);
  if (!tw) throw "showSummary4oneTerm(): tw not found from variant2samples.twLst";
  const rows = [];
  for (const [category_key, count, total] of numbycategory) {
    const row = [
      { value: tw.term.values?.[category_key]?.label || category_key },
      { html: total == void 0 ? "" : fillbar(null, { f: count / total, v1: count, v2: total }) },
      { html: count + (total ? ' <span style="font-size:.8em">/ ' + total + "</span>" : "") }
    ];
    rows.push(row);
  }
  renderTable({
    div,
    rows,
    columns: [
      {
        nowrap: true
        // to force all category values to show in one line without wrap. otherwise they wrap and column width appears fixed
      },
      {},
      {}
    ],
    showHeader: false,
    singleMode: true,
    noRadioBtn: true,
    noButtonCallback: (i) => {
      clickCategory(numbycategory[i][0]);
    }
  });
  async function clickCategory(category) {
    tk.menutip.clear();
    const term = await tk.mds.termdb.vocabApi.getterm(termid);
    if (!term.values || Object.keys(term.values).length == 0) {
      term.values = {};
      for (const c of numbycategory) {
        term.values[c[0]] = { label: c[0], samplecount: c[1] };
      }
    }
    const tvs = {
      type: "tvs",
      tvs: { term, values: [{ key: category }] }
    };
    createSubTk(tk, block, tvs);
  }
}
function getNewFilter(tk, tvs) {
  if (tk.filterObj) {
    return getNormalRoot({
      type: "tvslst",
      join: "and",
      in: true,
      lst: [tk.filterObj, tvs]
    });
  }
  return {
    type: "tvslst",
    in: true,
    join: "",
    lst: [tvs]
  };
}
async function showDensity4oneTerm(termid, div, data, tk, block) {
  const term = await tk.mds.termdb.vocabApi.getterm(termid);
  const callback = async (range) => {
    tk.menutip.clear();
    const tvs = {
      type: "tvs",
      tvs: { term, ranges: [{ start: range.range_start, stop: range.range_end }] }
    };
    createSubTk(tk, block, tvs);
  };
  const vr = new violinRenderer({
    holder: div,
    rd: data.density_data,
    width: 400,
    height: 100,
    radius: 8,
    callback,
    scaleFactor: term.valueConversion ? term.valueConversion.scaleFactor : 1
  });
  vr.render();
}
function createSubTk(tk, block, tvs) {
  const tk2 = block.block_addtk_template(tk.duplicateTk(getNewFilter(tk, tvs)));
  tk2.subtk = true;
  block.tk_load(tk2);
}
function menu_listSamples(buttonrow, data, tk, block) {
  buttonrow.append("div").text(`List ${data.sampleTotalNumber} sample${data.sampleTotalNumber > 1 ? "s" : ""}`).attr("class", "sja_menuoption sja_mds3_slb_sampletablebtn").attr("data-testid", "sjpp-mds3-list-samples-option").on("click", async () => {
    tk.menutip.clear();
    const wait = tk.menutip.d.append("div").text("Loading...").style("margin", "15px");
    try {
      const { samples } = await tk.mds.getSamples();
      await displaySampleTable(samples, {
        div: tk.menutip.d,
        tk,
        block
      });
      wait.remove();
    } catch (e) {
      wait.text(e.message || e);
      console.log(e);
    }
  });
}
export {
  makeSampleFilterLabel,
  makeSampleLabel
};
//# sourceMappingURL=leftlabel.sample-42AX4KTV.js.map
