import {
  roundValueAuto
} from "./chunk-TLT4YIG3.js";
import {
  brushX
} from "./chunk-KYBIQBXE.js";
import {
  select_default
} from "./chunk-I6Y4O3RR.js";

// filter/tvs.density.js
function addBrushes(self, new_brush_location) {
  const brushes = self.num_obj.brushes;
  const maxvalue = self.num_obj.density_data.max;
  const minvalue = self.num_obj.density_data.min;
  const rawDecile = (maxvalue - minvalue) / 10;
  const decile = self.tvs.term.type == "integer" ? Math.floor(rawDecile) : rawDecile;
  for (const [i, r] of self.num_obj.ranges.entries()) {
    const _b = brushes.find((b) => b.orig === r);
    let brush;
    if (!_b) {
      brush = { orig: r, range: JSON.parse(JSON.stringify(r)) };
      brushes.push(brush);
    } else {
      brush = _b;
    }
    if (r.start === "") {
      if (new_brush_location == "center") brush.range.start = minvalue + decile * 4;
      else brush.range.start = minvalue + decile * 8;
    }
    if (r.stop === "") {
      if (new_brush_location == "center") brush.range.stop = minvalue + decile * 6;
      else brush.range.stop = Math.floor(maxvalue);
    }
  }
  const range_brushes = self.num_obj.brush_g.selectAll(".range_brush").data(brushes, (d) => brushes.indexOf(d));
  range_brushes.exit().remove();
  range_brushes.each(function(d, i) {
    select_default(this).selectAll(".overlay").style("pointer-events", "all");
  });
  range_brushes.enter().append("g").attr("class", "range_brush").each(function(brush, i) {
    applyBrush(self, this, brush, i);
  });
}
function applyBrush(self, elem, brush) {
  if (!brush.elem) brush.elem = select_default(elem);
  const range = brush.range;
  const plot_size = self.num_obj.plot_size;
  const xscale = self.num_obj.xscale;
  const maxvalue = self.num_obj.density_data.max;
  const minvalue = self.num_obj.density_data.min;
  brush.d3brush = brushX().extent([
    [0, 0],
    [plot_size.width, plot_size.height]
  ]).on("brush", function(event, d) {
    const s = event.selection;
    if (!s) return;
    const inputRange = brush.rangeInput.getRange();
    if (inputRange?.value != void 0) {
      brush.range = inputRange;
      return;
    }
    updateTempRanges(xscale, s, range, inputRange, minvalue, maxvalue, self.tvs.term.type);
    const [start, stop] = setStartStopDisplays(range, inputRange);
    brush.rangeInput.getInput().node().value = `${start} x ${stop}`;
  }).on("end", function() {
    brush.elem.selectAll(".overlay").style("pointer-events", "none");
  });
  const brush_start = range.startunbounded ? minvalue : range.start;
  const brush_stop = range.stopunbounded ? maxvalue : range.stop;
  brush.init = () => {
    if (range.value == void 0)
      brush.elem.call(brush.d3brush).call(brush.d3brush.move, [brush_start, brush_stop].map(xscale));
  };
  if (range.startunbounded) delete range.start;
  if (range.stopunbounded) delete range.stop;
  brush.elem.selectAll(".selection").style(
    "fill",
    brush.orig.start === "" && brush.orig.stop === "" || JSON.stringify(range) != JSON.stringify(brush.orig) ? "#23cba7" : "#777777"
  );
}
function updateTempRanges(xscale, s, range, inputRange, minvalue, maxvalue, type) {
  range.start = convertRangeValue(xscale, s[0]);
  range.stop = convertRangeValue(xscale, s[1]);
  const min = roundValueAuto(Number(minvalue));
  const max = roundValueAuto(Number(maxvalue));
  if (range.start < min) range.start = min;
  if (range.stop > max) range.stop = max;
  range.startunbounded = min == range.start && inputRange.startunbounded;
  range.stopunbounded = max == range.stop && inputRange.stopunbounded;
  if (type == "integer") {
    range.start = range.startunbounded ? "" : Math.round(range.start);
    range.stop = range.stopunbounded ? "" : Math.round(range.stop);
  }
}
function setStartStopDisplays(range, inputRange) {
  const start = range.startunbounded ? "" : inputRange.startinclusive ? `${range.start} <=` : `${range.start} <`;
  const stop = range.stopunbounded ? "" : inputRange.stopinclusive ? `<= ${range.stop}` : `< ${range.stop}`;
  return [start, stop];
}
function convertRangeValue(xscale, sidx) {
  const value = Number(xscale.invert(sidx));
  return roundValueAuto(value);
}
function addNewBrush(self, new_brush_location = "end", callback) {
  const new_range = { start: "", stop: "", index: self.tvs.ranges.length };
  self.num_obj.ranges.push(new_range);
  const brush = { orig: new_range, range: JSON.parse(JSON.stringify(new_range)) };
  self.num_obj.brushes.push(brush);
  addBrushes(self, new_brush_location);
  if (callback) callback();
  brush.init();
}

export {
  addBrushes,
  updateTempRanges,
  setStartStopDisplays,
  addNewBrush
};
//# sourceMappingURL=chunk-ESEQBXTM.js.map
