import {
  NumericDensity
} from "./chunk-WHI6466S.js";
import {
  HandlerBase
} from "./chunk-LSEFWW72.js";
import {
  Tabs
} from "./chunk-HBW42TDT.js";

// termsetting/handlers/NumericHandler.ts
var NumericHandler = class extends HandlerBase {
  constructor(opts) {
    super(opts);
    this.tabs = [];
    this.handlerByMode = {};
    this.dom = {};
    this.opts = opts;
    this.termsetting = opts.termsetting;
    this.tw = opts.termsetting.tw;
    this.tabs = this.setTabData();
    this.density = new NumericDensity(opts);
  }
  getPillStatus() {
    this.tw = this.termsetting.tw;
    return this.tw.getStatus(this.termsetting.usecase, this.termsetting.data);
  }
  setTabData() {
    const self = this.termsetting;
    const tabs = [];
    const callback = async (event, tabData) => {
      if (event) event.stopPropagation();
      try {
        await this.setEditHandler(tabData);
        await this.editHandler.showEditMenu(tabData.contentHolder);
      } catch (e) {
        this.dom.errdiv.style("display", "").text(e);
      }
    };
    const numTabs = self.opts.numericEditMenuVersion.length;
    if (self.opts.numericEditMenuVersion.includes("continuous")) {
      tabs.push({
        mode: "continuous",
        label: self.term.type == "survival" ? "Time to Event" : "Continuous",
        callback,
        active: this.tw.q.mode === "continuous" || numTabs === 1
      });
    }
    if (self.opts.numericEditMenuVersion.includes("discrete")) {
      tabs.push({
        mode: "discrete",
        label: self.term.type == "survival" ? "Exit code" : "Discrete",
        callback,
        active: this.tw.q.mode === "discrete" || numTabs === 1
      });
    }
    if (self.opts.numericEditMenuVersion.includes("spline")) {
      tabs.push({
        mode: "spline",
        label: "Cubic spline",
        callback,
        active: this.tw.q.mode === "spline" || numTabs === 1
      });
    }
    if (self.opts.numericEditMenuVersion.includes("binary")) {
      tabs.push({
        mode: "binary",
        label: "Binary",
        callback,
        active: this.tw.q.mode === "binary" || numTabs === 1
      });
    }
    return tabs;
  }
  async setEditHandler(tabData) {
    if (!this.handlerByMode[tabData.mode]) {
      switch (tabData.mode) {
        case "continuous": {
          const { NumContEditor } = await import("./NumContEditor-4CSTHVRX.js");
          this.handlerByMode.continuous = new NumContEditor(this.opts, this);
          break;
        }
        case "discrete": {
          const { NumDiscreteEditor } = await import("./NumDiscreteEditor-F46H5CME.js");
          this.handlerByMode.discrete = new NumDiscreteEditor(this.opts, this);
          break;
        }
        case "binary": {
          const { NumBinaryEditor } = await import("./NumBinaryEditor-WMSF7HIO.js");
          this.handlerByMode.binary = new NumBinaryEditor(this.opts, this);
          break;
        }
        case "spline": {
          const { NumSplineEditor } = await import("./NumSplineEditor-JP2V2L4Y.js");
          this.handlerByMode.spline = new NumSplineEditor(this.opts, this);
          break;
        }
        default:
          throw `unexpected numeric tabData.mode='${tabData.mode}'`;
          break;
      }
    }
    this.editHandler = this.handlerByMode[tabData.mode];
  }
  async showEditMenu(div) {
    try {
      this.showLoading(div);
      this.dom.errdiv = div.append("div").attr("class", "sja_errorbar").style("display", "none");
      this.tw = this.termsetting.tw;
      const self = this.tw;
      for (const t of this.tabs) {
        t.active = this.tabs.length === 1 || self.q.mode == t.mode || t.mode == "continuous" && !self.q.mode;
      }
      this.density_data = await this.density.setData();
      await this.setEditHandler(this.tabs.find((t) => t.active));
      this.dom.editDiv = div.append("div").attr("data-testid", "sjpp-num-ts-edit-div");
      this.dom.btnDiv = div.append("div");
      this.renderButtons(this.dom.btnDiv);
      if (this.tabs.length > 1) {
        this.dom.topBar = this.dom.editDiv.append("div").style("padding", "10px");
        this.dom.topBar.append("span").html("Use as&nbsp;");
        new Tabs({
          holder: this.dom.topBar.append("div").style("display", "inline-block"),
          contentHolder: this.dom.editDiv.append("div"),
          noTopContentStyle: true,
          tabs: this.tabs
        }).main();
      } else {
        await this.editHandler.showEditMenu(this.dom.editDiv);
      }
      this.dom.loadingDiv.style("display", "none");
    } catch (e) {
      this.hideLoading();
      this.dom.errdiv.style("display", "").text(typeof e == "object" ? e.message || e.error || e : e);
    }
  }
  renderButtons(btnDiv) {
    btnDiv.append("button").style("margin", "5px").attr("data-testId", "sjpp_numeric_edit_apply").html("Apply").on("click", () => {
      this.termsetting.q = this.editHandler.getEditedQ();
      this.termsetting.dom.tip.hide();
      this.termsetting.api.runCallback();
    });
    btnDiv.append("button").style("margin", "5px").attr("data-testId", "sjpp_numeric_edit_reset").html("Reset").on("click", () => {
      this.editHandler.undoEdits();
    });
  }
  destroy() {
    for (const s of Object.values(this.dom)) {
      if (typeof s.remove == "function") s.remove();
    }
    this.density.destroy();
  }
};

export {
  NumericHandler
};
//# sourceMappingURL=chunk-ZXTHADSC.js.map
