import {
  keyupEnter
} from "./chunk-SA7APTJR.js";
import "./chunk-HJ6L54YS.js";
import {
  HandlerBase
} from "./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 "./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";

// termsetting/handlers/NumSplineEditor.ts
var NumSplineEditor = class extends HandlerBase {
  constructor(opts, handler) {
    super(opts);
    this.dom = {};
    this.default_knots_count = 4;
    this.opts = opts;
    this.handler = handler;
    this.tw = handler.tw;
  }
  async getDefaultQ() {
    const tw = this.termsetting.tw;
    if (tw.q.mode == "spline") {
      return JSON.parse(JSON.stringify(tw.q));
    }
    return await this.getKnots(this.default_knots_count);
  }
  async getKnots(knot_count) {
    const middle_knot_count = knot_count - 2;
    const t = this.tw.term;
    const knots = [];
    const percentile_lst = [5];
    const second_knot_perc = (90 / (middle_knot_count + 1)).toFixed(0);
    for (let i = 1; i < middle_knot_count + 1; i++) {
      percentile_lst.push(i * second_knot_perc);
    }
    percentile_lst.push(95);
    const values = await this.getPercentile2Value(percentile_lst);
    for (const val of values) {
      knots.push({ value: val.toFixed(t.type == "integer" ? 0 : 2) });
    }
    return {
      mode: "spline",
      knots
    };
  }
  async getPercentile2Value(percentile_lst) {
    const vocabApi = this.termsetting.vocabApi;
    const data = await vocabApi.getPercentile(this.tw.term, percentile_lst, vocabApi.state?.termfilter);
    if (data.error || !data.values.length || !data.values.every((v) => Number.isFinite(v)))
      throw "cannot get median value: " + (data.error || "no data");
    const perc_values = [...new Set(data.values)];
    return perc_values;
  }
  async showEditMenu(div) {
    this.tw = this.handler.tw;
    this.q = await this.getDefaultQ();
    if (this.dom.density_div) {
      if (this.handler.dom.editDiv?.node().contains(this.dom.density_div.node())) {
        await this.handler.density.showViolin(this.dom.density_div, this.getBoundaryOpts());
        return;
      } else {
        this.dom.density_div.remove();
        delete this.dom.density_div;
      }
    }
    this.dom.density_div = div.append("div");
    await this.handler.density.showViolin(this.dom.density_div);
    await this.handler.density.setBinLines(this.getBoundaryOpts());
    this.dom.knots_div = div.append("div").style("padding", "5px");
    this.renderCustomSplineInputs();
    this.renderAutoSplineInputs();
  }
  getBoundaryOpts() {
    return {
      values: this.q.knots.map((k) => {
        return {
          //this.q.lst[0].map(bin => ({ x: bin.startunbounded ? bin.stop : bin.start, isDraggable: true })),
          x: k.value,
          isDraggable: true
        };
      }),
      callback: (d, value) => {
        this.q.knots[d.index].value = value;
        this.dom.customKnotsInput.property("value", this.q.knots.map((k) => k.value).join("\n"));
      }
    };
  }
  /******************* Functions for Custom Spline knots *******************/
  renderCustomSplineInputs() {
    this.dom.custom_knots_div = this.dom.knots_div.append("div");
    this.dom.custom_knots_div.append("div").style("display", "inline-block").style("vertical-align", "top").style("padding", "3px 15px").style("font-weight", "normal").style("color", "rgb(136, 136, 136)").html("Knots");
    this.dom.customKnotsInput = this.dom.custom_knots_div.append("div").style("display", "inline-block").style("padding", "3px 15px").style("padding-left", "5px").append("textarea").style("height", "100px").style("width", "100px").text(this.q.knots.map((d) => d.value).join("\n")).on("change", () => {
      this.q.knots = this.processKnotsInputs();
      this.handler.density.setBinLines(this.getBoundaryOpts());
    }).on("keyup", async (event) => {
      if (!keyupEnter(event) && event.key != 8) return;
      this.q.knots = this.processKnotsInputs();
      this.handler.density.setBinLines(this.getBoundaryOpts());
    });
    this.dom.custom_knots_div.append("div").style("display", "inline-block").style("vertical-align", "top").style("font-size", ".7em").style("padding", "3px 15px").style("padding-left", "5px").style("color", "rgb(136, 136, 136)").html(`Enter knot values, one knot per line.</br> 
				Adjust knot by dragging on the vertical line.</br>
			 	Or autocompute knots from below.`);
  }
  // apply custom knots if changed from density plot to textarea
  updateCustomSplineInputs() {
    this.dom.customKnotsInput.property("value", this.q.knots.map((d) => d.value).join("\n"));
  }
  // apply custom knots to this.q.knots
  processKnotsInputs() {
    const data = this.dom.customKnotsInput.property("value").split("\n").filter((d) => d != "").map((d) => +d).sort((a, b) => a - b).map((d) => {
      const knot = {
        value: +d
      };
      return knot;
    });
    return data;
  }
  /******************* Functions for Auto Spline knots *******************/
  renderAutoSplineInputs() {
    const div = this.dom.knots_div;
    const knot_count = 4;
    const default_knot_count = knot_count;
    this.dom.knot_select_div = div.append("div");
    this.dom.knot_select_div.append("div").style("display", "inline-block").style("margin-left", "15px").style("color", "rgb(136, 136, 136)").html("Autocompute");
    const knot_ct_select = this.dom.knot_select_div.append("select").attr("data-testid", "num-spline-editor-num-knots").style("margin-left", "10px").style("margin-bottom", "7px");
    for (let i = default_knot_count - 1; i < default_knot_count + 5; i++) {
      knot_ct_select.append("option").attr("value", i).html(i);
    }
    knot_ct_select.node().value = this.q.knots?.length || this.default_knots_count;
    this.dom.knot_select_div.append("div").style("display", "inline-block").style("margin-left", "10px").style("color", "rgb(136, 136, 136)").html("knots");
    this.dom.knot_select_div.append("button").style("margin", "15px").text("Compute").on("click", async () => {
      this.dom.knots_div.style("pointer-events", "none").style("opacity", 0.5);
      const desired_knots_ct = Number.parseInt(knot_ct_select.node().value);
      let requested_knots_ct = desired_knots_ct;
      while (this.q.knots.length != desired_knots_ct) {
        if (requested_knots_ct > 99) break;
        const result = await this.getKnots(requested_knots_ct);
        this.q.knots = result.knots;
        requested_knots_ct = requested_knots_ct + 1;
      }
      this.dom.knots_div.style("pointer-events", "auto").style("opacity", 1);
      this.updateCustomSplineInputs();
      this.handler.density.setBinLines(this.getBoundaryOpts());
    });
    this.dom.knot_select_div.append("div").style("display", "inline-block").style("font-size", ".7em").style("padding", "3px 15px").style("padding-left", "5px").style("color", "rgb(136, 136, 136)").html("Will overwrite existing values.");
  }
  getEditedQ(destroyDom = true) {
    const knots = this.processKnotsInputs();
    if (destroyDom) {
      for (const name of Object.keys(this.dom)) {
        this.dom[name].remove();
        delete this.dom[name];
      }
    }
    return {
      mode: "spline",
      knots
    };
  }
  async undoEdits() {
    this.q = await this.getDefaultQ();
    this.dom.knots_div.selectAll("*").remove();
    await this.handler.density.setBinLines(this.getBoundaryOpts());
    this.renderCustomSplineInputs();
    this.renderAutoSplineInputs();
  }
};
export {
  NumSplineEditor
};
//# sourceMappingURL=NumSplineEditor-JP2V2L4Y.js.map
