import {
  getDefaultGseaSettings
} from "./chunk-VHOEJBFP.js";
import "./chunk-UOVALHAJ.js";
import {
  PlotBase,
  getDefaultVolcanoSettings,
  validateVolcanoSettings
} from "./chunk-SA7APTJR.js";
import "./chunk-HJ6L54YS.js";
import "./chunk-LSEFWW72.js";
import {
  importPlot
} from "./chunk-SKREEF3H.js";
import {
  Menu
} from "./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 {
  copyMerge,
  getCompInit
} from "./chunk-M3J4MINX.js";
import "./chunk-PF4DSFDR.js";
import "./chunk-QH6W3NVF.js";
import {
  DNA_METHYLATION,
  GENE_EXPRESSION,
  PROTEOME_DAP,
  SINGLECELL_CELLTYPE,
  termType2label
} from "./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/diffAnalysis/view/DiffAnalysisView.ts
var DiffAnalysisView = class {
  constructor(app, config, dom) {
    this.app = app;
    this.config = config;
    this.dom = dom;
    setRenderers(this);
    this.tabsData = this.getTabsOptions(this);
    this.tabs = new Tabs({ holder: this.dom.tabsDiv, tabs: this.tabsData });
    this.tabs.main();
  }
  update(plotConfig) {
    const activeTabIndex = this.tabsData.findIndex((tab) => tab.id == plotConfig.childType);
    this.tabs.update(activeTabIndex);
  }
};
function setRenderers(self) {
  self.getTabsOptions = (self2) => {
    const tabs = [
      {
        active: self2.config.childType === "volcano",
        id: "volcano",
        label: "Volcano",
        isVisible: () => true,
        // isVisible: () => self.config.termType === TermTypes.GENE_EXPRESSION,
        getPlotConfig: () => {
          return {
            childType: "volcano"
          };
        },
        callback: self2.tabCallback
      },
      {
        active: self2.config.childType === "gsea",
        id: "gsea",
        label: "Gene Set Enrichment Analysis",
        isVisible: () => true,
        // isVisible: () => self.config.termType === TermTypes.GENE_EXPRESSION,
        getPlotConfig: () => {
          return {
            childType: "gsea"
          };
        },
        callback: self2.tabCallback
      }
    ];
    return tabs;
  };
  self.tabCallback = async (event, tab) => {
    if (!event || !tab || !tab.id) return;
    const plotConfig = tab.getPlotConfig();
    await self.app.dispatch({
      type: "plot_edit",
      id: self.config.id,
      config: plotConfig
    });
  };
}

// plots/diffAnalysis/DifferentialAnalysis.ts
var DifferentialAnalysis = class extends PlotBase {
  constructor(opts, api) {
    super(opts, api);
    this.type = "differentialAnalysis";
    this.components = {
      plots: {}
    };
    this.termType = opts.termType;
    const holder = opts.holder.classed("sjpp-diff-analysis-main", true);
    const controls = opts.controls ? holder : holder.append("div");
    const div = holder.append("div").style("padding", "5px").style("display", "inline-block").style("vertical-align", "top");
    const tabsDiv = div.append("div").attr("id", "sjpp-diff-analysis-tabs").style("display", "inline-block");
    const plots = div.append("div").attr("id", "sjpp-diff-analysis-tabs-content");
    this.dom = {
      controls: controls.style("display", "inline-block"),
      div,
      tabsDiv,
      plots,
      tip: new Menu({ padding: "" })
    };
    this.plotsControlsDiv = {};
    this.plotsDiv = {};
    if (opts.parentId) this.parentId = opts.parentId;
    if (opts.header) {
      this.dom.header = {
        title: opts.header.append("span").style("margin-right", "5px").style("color", "darkslategray"),
        plot: opts.header.append("span").style("font-size", "0.7em").style("opacity", 0.6)
      };
    }
  }
  static {
    this.type = "differentialAnalysis";
  }
  getState(appState) {
    const config = appState.plots.find((p) => p.id === this.id);
    if (!config) {
      throw new Error(
        `No plot with id='${this.id}' found. Did you set this.id before this.api = getComponentApi(this)?`
      );
    }
    return {
      config
    };
  }
  reactsTo(action) {
    if (action.type.includes("cache_termq")) return true;
    if (action.type.startsWith("plot_")) {
      return action.id === this.id || action.id == this.parentId;
    }
    if (action.type.startsWith("filter")) return true;
    if (action.type.startsWith("cohort")) return true;
    if (action.type == "app_refresh") return true;
  }
  async init(appState) {
    const state = this.getState(appState);
    const config = structuredClone(state.config);
    this.plotTabs = new DiffAnalysisView(this.app, config, this.dom);
  }
  async setComponent(config) {
    this.plotsControlsDiv[config.childType] = this.dom.controls.append("div");
    this.plotsDiv[config.childType] = this.dom.plots.append("div");
    const opts = {
      app: this.app,
      holder: this.plotsDiv[config.childType],
      id: this.id,
      parent: this.api,
      controls: this.plotsControlsDiv[config.childType],
      termType: config.termType
    };
    const _ = await importPlot(config.childType, `unsupported childType='${config.childType}'`);
    this.components.plots[config.childType] = await _.componentInit(opts);
  }
  async main() {
    const config = structuredClone(this.state.config);
    if (config.chartType != this.type) return;
    if (!this.components.plots[config.childType]) await this.setComponent(config);
    for (const childType in this.components.plots) {
      const chart = this.components.plots[childType];
      if (chart.type != config.childType) {
        this.plotsDiv[chart.type].style("display", "none");
        this.plotsControlsDiv[chart.type].style("display", "none");
      }
    }
    this.plotsDiv[config.childType].style("display", "");
    this.plotsControlsDiv[config.childType].style("display", "");
    if (this.dom.header) {
      if (config.tw) this.dom.header.title.text(config.tw.term.name);
      if (config.headerText) this.dom.header.title.text(config.headerText);
      const typeStr = termType2label(config.termType).toUpperCase();
      this.dom.header.plot.text(` DIFFERENTIAL ${typeStr} ANALYSIS`);
    }
    if (this.plotTabs) this.plotTabs.update(config);
  }
};
var DiffAnalysisInit = getCompInit(DifferentialAnalysis);
var componentInit = DiffAnalysisInit;
var enabledTermTypes = [GENE_EXPRESSION, SINGLECELL_CELLTYPE, DNA_METHYLATION, PROTEOME_DAP];
function getPlotConfig(opts) {
  if (!opts.termType) throw new Error(".termType is required");
  if (!enabledTermTypes.includes(opts.termType))
    throw new Error(`termType = '${opts.termType}' not supported by Differential Analysis`);
  const config = {
    chartType: "differentialAnalysis",
    childType: "volcano",
    termType: opts.termType,
    settings: {},
    highlightedData: opts.highlightedData || [],
    hidePlotFilter: true
    //TODO: Support filtering and reactivity in child plots
  };
  if (opts?.tw?.term?.name && opts.headerText)
    throw new Error("Cannot provide both tw.term.name and headerText. Please choose one to use as the plot title.");
  if (opts.termType == SINGLECELL_CELLTYPE) {
    Object.assign(config, {
      categoryName: opts.categoryName || "",
      termId: opts.termId || "",
      sample: opts.sample || { sID: "", eID: "" }
    });
  }
  config.settings.volcano = getDefaultVolcanoSettings(opts.overrides, opts);
  config.settings.gsea = getDefaultGseaSettings(opts.overrides);
  validateVolcanoSettings(config, opts);
  return copyMerge(config, opts);
}
export {
  DiffAnalysisInit,
  componentInit,
  getPlotConfig
};
//# sourceMappingURL=DifferentialAnalysis-5YQQLJKR.js.map
