import {
  t0_t2_defaultQ
} from "./chunk-C5ZMAQXQ.js";
import "./chunk-KI5KI3ZJ.js";
import "./chunk-V2OJLJSK.js";
import {
  PlotBase,
  controlsInit,
  defaultUiLabels,
  fillTermWrapper,
  term0_term2_defaultQ
} from "./chunk-SA7APTJR.js";
import "./chunk-HJ6L54YS.js";
import "./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 {
  copyMerge,
  getCompInit
} from "./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";

// plots/summaryInput.ts
var SummaryInputPlot = class _SummaryInputPlot extends PlotBase {
  constructor(opts, api) {
    super(opts, api);
    this.components = {};
    // expected class-specific props
    this.configTermKeys = ["term", "term0", "term2"];
    this.type = _SummaryInputPlot.type;
    this.dom = {
      header: opts.header.html("Correlation Input"),
      holder: opts.holder,
      controls: opts.holder.append("div"),
      submit: opts.holder.append("div").style("position", "relative").style("margin", "10px").style("max-width", "200px")
    };
  }
  static {
    this.type = "summaryInput";
  }
  getState(appState) {
    const config = appState.plots.find((p) => p.id === this.id);
    if (!config) {
      throw `No plot with id='${this.id}' found. Did you set this.id before this.api = getComponentApi(this)?`;
    }
    return {
      termfilter: appState.termfilter,
      config,
      // quick fix to skip history tracking as needed
      _scope_: appState._scope_
    };
  }
  async init(appState) {
    const state = this.getState(appState);
    this.renderSubmit();
    await this.setControls(state.config);
  }
  async setControls(config) {
    const controlLabels = config.controlLabels;
    const inputs = [
      {
        type: "term",
        configKey: "term",
        testids: {
          suffix: "summaryInputControlTerm"
        },
        usecase: { target: "summaryInput", detail: "term" },
        label: controlLabels.term1?.label || "Primary variable",
        processConfig: (config2) => {
          const { term, term2, term0, term2_surv, term0_surv } = this.state.config;
          const editedType = config2.term?.term?.type;
          if (editedType === term?.term?.type) return;
          if (editedType == "survival") {
            if (term2 && term2.term?.name != term2_surv?.term?.name)
              config2.term2 = { term: structuredClone(term2.term), q: {} };
            if (term0 && term0.term?.name != term0_surv?.term?.name)
              config2.term0 = { term: structuredClone(term0.term), q: {} };
          } else {
            if (term2_surv && term2_surv.term?.name != term2?.term?.name)
              config2.term2 = { term: structuredClone(term2_surv.term), q: {} };
            if (term0_surv && term0_surv.term.name != term0?.term?.name)
              config2.term0 = { term: structuredClone(term0_surv.term), q: {} };
          }
        }
      },
      {
        type: "term",
        configKey: "term2",
        testids: {
          suffix: "summaryInputControlTerm2"
        },
        usecase: { target: "summaryInput", detail: "term2" },
        label: controlLabels.term2.label,
        defaultQ4fillTW: term0_term2_defaultQ,
        getDisplayStyle: (plot) => plot.config.term?.term.type == "survival" ? "none" : "table-row"
      },
      {
        type: "term",
        configKey: "term0",
        testids: {
          suffix: "summaryInputControlTerm0"
        },
        usecase: { target: "summaryInput", detail: "term0" },
        label: controlLabels.term0.label,
        defaultQ4fillTW: term0_term2_defaultQ,
        numericEditMenuVersion: this.opts.numericEditMenuVersion || ["discrete"],
        getDisplayStyle: (plot) => plot.config.term?.term.type == "survival" ? "none" : "table-row"
      },
      {
        type: "term",
        configKey: "term2_surv",
        testids: {
          suffix: "summaryInputControlTerm2surv"
        },
        usecase: { target: "summaryInput", detail: "term2" },
        label: controlLabels.term2.label,
        defaultQ4fillTW: t0_t2_defaultQ,
        getDisplayStyle: (plot) => plot.config.term?.term.type == "survival" ? "table-row" : "none"
      },
      {
        type: "term",
        configKey: "term0_surv",
        testids: {
          suffix: "summaryInputControlTerm0surv"
        },
        usecase: { target: "summaryInput", detail: "term0" },
        label: controlLabels.term0.label,
        defaultQ4fillTW: t0_t2_defaultQ + "_surv",
        numericEditMenuVersion: this.opts.numericEditMenuVersion || ["discrete"],
        getDisplayStyle: (plot) => plot.config.term?.term.type == "survival" ? "table-row" : "none"
      }
    ];
    this.components.controls = await controlsInit({
      app: this.app,
      id: this.id,
      holder: this.dom.controls.style("display", "inline-block"),
      isOpen: true,
      showTopBar: false,
      inputs,
      // may comment out loadingMasks option to be able to click the submit button and
      // manually test submitting before a config tw is fully filled, there should only be
      // a console warning and no displayed error messages in the app
      loadingMasks: [this.dom.submitMask]
    });
  }
  renderSubmit() {
    this.dom.submitBtn = this.dom.submit.append("button").property("disabled", true).style("border", "none").style("border-radius", "20px").style("padding", "10px 15px").attr("data-testid", "sjpp-summaryInput-submitBtn").text("Submit").on("click", () => {
      const { term, term2, term0, term2_surv, term0_surv, filter } = structuredClone(this.config);
      if (!term) throw "config.term is missing";
      const chartType = term.term.type == "survival" ? "survival" : "summary";
      this.app.dispatch({
        type: "app_refresh",
        subactions: [
          {
            type: "plot_create",
            config: {
              chartType,
              term,
              term2: chartType == "survival" ? term2_surv : term2,
              term0: chartType == "survival" ? term0_surv : term0,
              filter
            }
          },
          {
            type: "plot_delete",
            id: this.id
          }
        ]
      });
    });
    this.dom.submitMask = this.dom.submit.append("div").style("position", "absolute").style("top", 0).style("left", 0).style("height", "100%").style("width", "100%").style("background-color", `rgba(255,255,255,0.7)`).style("display", "none");
  }
  async main() {
    this.config = await this.getMutableConfig();
    this.dom.submitBtn.property("disabled", !this.config.term).style("cursor", this.config.term ? "pointer" : "default");
    this.dom.submitMask.style("display", "none");
  }
};
var summaryInputInit = getCompInit(SummaryInputPlot);
var componentInit = summaryInputInit;
async function getPlotConfig(opts, app) {
  try {
    if (opts.term) {
      const defaultQ = opts.term.bins || opts.term.q ? void 0 : { geneVariant: { type: "predefined-groupset" } };
      await fillTermWrapper(opts.term, app.vocabApi, defaultQ);
    }
    if (opts.term2) {
      const defaultQ = opts.term2.bins || opts.term2.q ? void 0 : opts.term.term.type == "survival" ? t0_t2_defaultQ : term0_term2_defaultQ;
      await fillTermWrapper(opts.term2, app.vocabApi, defaultQ);
    }
    if (opts.term0) {
      const defaultQ = opts.term0.bins || opts.term0.q ? void 0 : opts.term.term.type == "survival" ? t0_t2_defaultQ : term0_term2_defaultQ;
      await fillTermWrapper(opts.term0, app.vocabApi, defaultQ);
    }
  } catch (e) {
    if (e.stack) console.log(e.stack);
    throw `${e} [summaryInput getPlotConfig()]`;
  }
  const config = {
    chartType: "summaryInput",
    settings: {},
    controlLabels: Object.assign({}, defaultUiLabels, app.vocabApi.termdbConfig.uiLabels || {})
  };
  return copyMerge(config, opts);
}
export {
  componentInit,
  getPlotConfig,
  summaryInputInit
};
//# sourceMappingURL=summaryInput-4JO6MHP4.js.map
