import {
  DownloadMenu,
  ListSamples,
  filterJoin,
  getFilterItemByTag,
  renderTable
} from "./chunk-SA7APTJR.js";
import {
  SINGLECELL_GENE_EXPRESSION
} from "./chunk-NOEAT6CX.js";
import {
  niceNumLabels
} from "./chunk-DD4R5P6W.js";

// plots/violin.interactivity.js
function setInteractivity(self) {
  self.getChartImages = function() {
    const charts = [];
    for (const [key, chart] of Object.entries(self.data.charts)) {
      const title = self.getChartTitle(chart.chartId);
      const name = `${self.config.term.term.name}  ${title}`;
      const chartDiv = chart.chartDiv;
      charts.push({ name, svg: chartDiv.select("svg") });
    }
    return charts;
  };
  self.download = function(event) {
    if (!self.state) return;
    const name2svg = self.getChartImages();
    const dm = new DownloadMenu(name2svg, self.config.term.term.name);
    dm.show(event.clientX, event.clientY, event.target);
  };
  self.displayLabelClickMenu = function(t1, t2, plot, event) {
    if (!t2) return;
    if (self.config.term.term.type == SINGLECELL_GENE_EXPRESSION) return;
    const label = t1.q.mode === "continuous" ? "term2" : "term";
    const options = [];
    if (this.app.getState().nav.header_mode !== "only_buttons")
      options.push({
        label: `Add filter: ${plot.label.split(",")[0]}`,
        testid: "sjpp-violinLabOpt-addf",
        callback: getAddFilterCallback(self, plot)
      });
    options.push({
      label: `Hide: ${plot.label}`,
      testid: "sjpp-violinLabOpt-hide",
      callback: () => {
        const term = self.config[label];
        const isHidden = true;
        self.app.dispatch({
          type: "plot_edit",
          id: self.id,
          config: {
            [label]: {
              isAtomic: true,
              term: term.term,
              q: getUpdatedQfromClick(plot, term, isHidden)
            }
          }
        });
      }
    });
    if (self.state.displaySampleIds && self.state.hasVerifiedToken) {
      options.push({
        label: `List samples`,
        testid: "sjpp-violinLabOpt-list",
        callback: async () => {
          const [start, end] = [self.data.min, self.data.max];
          await self.callListSamples(event, plot, start, end);
        }
      });
    }
    self.displayMenu(event, options);
  };
  self.displayBrushMenu = function(t1, t2, self2, plot, event, scale, isH) {
    const selection = event.selection;
    const [start, end] = isH ? [scale.invert(selection[0]), scale.invert(selection[1])] : [scale.invert(selection[1]), scale.invert(selection[0])];
    const options = [];
    if (this.app.getState().nav.header_mode === "with_tabs")
      options.push({
        label: `Add filter`,
        testid: "sjpp-violinBrushOpt-addf",
        callback: getAddFilterCallback(self2, plot, start, end)
      });
    if (self2.state.displaySampleIds && self2.state.hasVerifiedToken) {
      options.push({
        label: `List samples`,
        testid: "sjpp-violinBrushOpt-list",
        callback: async () => self2.callListSamples(event.sourceEvent, plot, start, end)
      });
    }
    self2.displayMenu(event.sourceEvent, options, start, end);
  };
  self.displayMenu = function(event, options, start, end) {
    const tip = self.dom.clicktip.clear().show(event.clientX, event.clientY);
    const isBrush = start != null && end != null;
    if (isBrush) {
      const [niceStart, niceEnd] = self.config.term.term.type == "integer" ? [Math.round(start), Math.round(end)] : niceNumLabels([start, end]);
      tip.d.append("div").style("margin", "10px").text(`From ${niceStart} to ${niceEnd}`);
    }
    tip.d.append("div").selectAll("div").data(options).enter().append("div").attr("class", "sja_menuoption sja_sharp_border").attr("data-testid", (d) => d.testid).text((d) => d.label).on("click", async (event2, d) => {
      if (event2.target._clicked) return;
      event2.target._clicked = true;
      event2.target.textContent = "Loading...";
      await d.callback();
      tip.hide();
    });
  };
  self.getSampleList = function(plot, start, end) {
    const { term, term2, term0 } = self.config;
    const bins = self.data.bins;
    const rangeStart = start !== void 0 ? start : null;
    const rangeStop = end !== void 0 ? end : null;
    const ls = new ListSamples({
      app: self.app,
      termfilter: self.state.termfilter,
      term,
      term2,
      term0,
      plot,
      bins,
      start: rangeStart,
      end: rangeStop
    });
    return ls;
  };
  self.callListSamples = async function(event, plot, start, end) {
    const ls = self.getSampleList(plot, start, end);
    const data = await ls.getData();
    const [rows, columns] = ls.setTableData(data);
    const tip = self.dom.sampletabletip;
    tip.clear().show(event.clientX, event.clientY, false);
    renderTable({
      rows,
      columns,
      div: tip.d,
      showLines: true,
      maxHeight: "40vh",
      resize: true,
      dataTestId: "sjpp-listsampletable"
    });
  };
  self.labelHideLegendClicking = function(t2, plot) {
    self.dom.legendDiv.selectAll(".sjpp-htmlLegend").on("click", (event) => {
      event.stopPropagation();
      const d = event.target.__data__;
      const termNum = t2?.term.type === "condition" || t2?.term.type === "samplelst" || t2?.term.type === "categorical" || (t2?.term.type === "float" || t2?.term.type === "integer") && self.config.term?.q.mode === "continuous" ? "term2" : "term";
      const term = self.config[termNum];
      if (t2) {
        for (const key of Object.keys(term?.q?.hiddenValues)) {
          if (d.text === key) {
            delete term.q.hiddenValues[key];
          }
        }
        const isHidden = false;
        self.app.dispatch({
          type: "plot_edit",
          id: self.id,
          config: {
            [termNum]: {
              isAtomic: true,
              term: term.term,
              q: getUpdatedQfromClick(plot, term, isHidden)
            }
          }
        });
      }
    }).on("mouseover", (event) => {
      const q = event.target.__data__;
      if (q === void 0) return;
      if (q.isHidden === true && q.isClickable === true) {
        self.dom.hovertip.clear().show(event.clientX, event.clientY).d.append("span").text("Click to unhide plot");
      }
    }).on("mouseout", function() {
      self.dom.hovertip.hide();
    });
  };
}
function getAddFilterCallback(self, plot, rangeStart, rangeStop) {
  const ls = self.getSampleList(plot, rangeStart, rangeStop);
  return () => {
    const filterUiRoot = getFilterItemByTag(self.state.termfilter.filter, "filterUiRoot");
    const filter = filterJoin([filterUiRoot, ls.tvslst]);
    filter.tag = "filterUiRoot";
    self.app.dispatch({
      type: "filter_replace",
      filter
    });
  };
}
function getUpdatedQfromClick(plot, term, isHidden = false) {
  const label = plot.label;
  const valueId = term?.term?.values ? term?.term?.values?.[label]?.label : label;
  const id = !valueId ? label : valueId;
  const q = term.q;
  if (!q.hiddenValues) q.hiddenValues = {};
  if (isHidden) q.hiddenValues[id] = 1;
  else delete q.hiddenValues[id];
  return q;
}

export {
  setInteractivity
};
//# sourceMappingURL=chunk-MYF2BWKC.js.map
