import {
  NumRegularBinEditor
} from "./chunk-QQZ3SBF3.js";
import {
  NumCustomBinEditor
} from "./chunk-NGM3ZLIP.js";
import "./chunk-SA7APTJR.js";
import "./chunk-HJ6L54YS.js";
import {
  HandlerBase
} from "./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 "./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/NumDiscreteEditor.ts
var NumDiscreteEditor = class extends HandlerBase {
  constructor(opts, handler) {
    super(opts);
    this.dom = {};
    this.boundaryInclusion = "stopinclusive";
    this.activeTab = "regular-bin";
    this.termsetting = opts.termsetting;
    this.tw = opts.termsetting.tw;
    this.handler = handler;
    this.editorsByType = {
      "regular-bin": new NumRegularBinEditor(this),
      "custom-bin": new NumCustomBinEditor(this)
    };
    if (this.tw.term.bins?.default.type != "regular-bin") this.activeTab = "custom-bin";
  }
  setDefaultBoundaryInclusion() {
    if (this.tw.q.mode == "discrete") {
      if (this.tw.q.type == "regular-bin")
        this.boundaryInclusion = this.tw.q.startinclusive ? "startinclusive" : "stopinclusive";
      else this.boundaryInclusion = this.tw.q.lst[0].startinclusive ? "startinclusive" : "stopinclusive";
    } else {
      this.boundaryInclusion = "stopinclusive";
    }
  }
  async showEditMenu(div) {
    if (this.dom.boundaryInclusionDiv) {
      if (div.node().contains(this.dom.boundaryInclusionDiv.node())) {
        await this.handler.density.showViolin(this.dom.density_div);
        if (this.tabs.tabs) {
          const tab = this.tabs.tabs[this.activeTab == "regular-bin" ? 0 : 1];
          await this.editorsByType[this.activeTab].render(tab.contentHolder);
        }
        return;
      } else delete this.dom.boundaryInclusionDiv;
    }
    this.dom.density_div = div.append("div");
    await this.handler.density.showViolin(this.dom.density_div);
    const self = this.termsetting;
    if (self.term && !self.term.bins) {
      self.handler.showLoading(div);
      const tw = {
        term: self.term,
        q: self.q
        /*, $id: ''*/
      };
      try {
        await self.vocabApi.setTermBins(tw);
      } catch (e) {
        div.append("div").attr("class", "sja_errorbar").text(e);
      }
      this.handler.hideLoading();
    }
    this.tw = this.termsetting.tw;
    this.setDefaultBoundaryInclusion();
    this.renderBoundaryInclusionInput(div);
    this.mayShowValueconversionMsg(div);
    this.renderTypeInputs(div);
  }
  renderBoundaryInclusionInput(div) {
    this.dom.boundaryInclusionDiv = div.append("div");
    this.dom.boundaryInclusionDiv.append("span").style("padding", "5px").style("color", "rgb(136, 136, 136)").html("Boundary Inclusion");
    const x = '<span style="font-family:Times;font-style:italic">x</span>';
    this.dom.boundaryInput = this.dom.boundaryInclusionDiv.append("select").style("margin-left", "10px").on("change", () => {
      this.boundaryInclusion = this.dom.boundaryInput.property("value");
      if (this.activeTab == "custom-bin") this.editorsByType[this.activeTab].handleInputChange();
      const q = this.editorsByType["regular-bin"].q;
      q.startinclusive = this.boundaryInclusion === "startinclusive";
      q.stopinclusive = this.boundaryInclusion === "stopinclusive";
    });
    const options = [];
    if (this.termsetting?.opts?.usecase?.target == "runChart2") {
    } else {
      options.push({ value: "stopinclusive", html: `start &lt; ${x} &le; end` });
    }
    options.push({ value: "startinclusive", html: `start &le; ${x} &lt; end` });
    this.dom.boundaryInput.selectAll("option").data(options).enter().append("option").property("value", (d) => d.value).property("selected", (d) => d.value === this.boundaryInclusion).html((d) => d.html);
  }
  mayShowValueconversionMsg(div) {
    const term = this.tw.term;
    if ("valueConversion" in term) {
      div.append("div").style("margin-bottom", "5px").style("opacity", 0.6).text(`Note: using values by the unit of ${term.valueConversion?.fromUnit}.`);
    }
  }
  renderTypeInputs(_div) {
    this.dom.binsDiv = _div.append("div");
    const div = this.dom.binsDiv.append("div").style("margin", "10px");
    if (this.tw.term.bins?.default.type == "custom-bin") {
      this.editorsByType["custom-bin"].render(div);
      return;
    }
    if (this.tw.term.bins?.default.type != "regular-bin")
      throw "self.bins.default.type is neither regular-bin or custom-bin, cannot render ui";
    const tabs = [
      {
        active: this.tw.q.type == "regular-bin",
        label: "Same bin size",
        callback: async (_, tab) => {
          if (tab.isInitialized) return;
          this.activeTab = "regular-bin";
          await this.editorsByType["regular-bin"].render(tab.contentHolder);
        }
      },
      {
        active: this.tw.q.type == "custom-bin",
        label: "Varying bin sizes",
        callback: async (event, tab) => {
          if (tab.isInitialized) return;
          this.activeTab = "custom-bin";
          await this.editorsByType["custom-bin"].render(tab.contentHolder);
        }
      }
    ];
    this.tabs = new Tabs({ holder: div, tabs });
    this.tabs.main();
  }
  getEditedQ(destroyDom = true) {
    this.boundaryInclusion = this.dom.boundaryInput.property("value");
    return this.editorsByType[this.activeTab].getEditedQ(destroyDom);
    if (destroyDom) this.destroy();
  }
  undoEdits() {
    this.setDefaultBoundaryInclusion();
    this.dom.boundaryInput.property("value", this.boundaryInclusion);
    this.editorsByType[this.activeTab].undoEdits();
  }
  destroy() {
    for (const name of Object.keys(this.dom)) {
      this.dom[name].remove();
      delete this.dom[name];
    }
  }
};
export {
  NumDiscreteEditor
};
//# sourceMappingURL=NumDiscreteEditor-F46H5CME.js.map
