import {
  storeInit
} from "./chunk-HKSNADSX.js";
import {
  recoverInit
} from "./chunk-4KY4XKJV.js";
import {
  AppBase,
  sayerror,
  vocabInit
} from "./chunk-SA7APTJR.js";
import {
  importPlot
} from "./chunk-SKREEF3H.js";
import {
  Menu
} from "./chunk-HYOEWQ5P.js";
import {
  AppApi
} from "./chunk-M3J4MINX.js";

// plots/plot.app.ts
var PlotApp = class _PlotApp extends AppBase {
  constructor(opts, api) {
    super(opts);
    this.components = {};
    this.wasDestroyed = false;
    this.api = api;
    this.type = _PlotApp.type;
    this.dom = this.getDom(opts);
  }
  static {
    this.type = "app";
  }
  getDom(opts) {
    const dom = {
      holder: opts.holder,
      errdiv: opts.holder.append("div"),
      plotDiv: opts.holder.append("div")
    };
    const controls = opts.violin?.mode == "minimal" ? null : opts.holder.append("div").style("white-space", "nowrap");
    if (controls) {
      dom.plotControls = controls.append("div").style("display", "inline-block");
      dom.recoverControls = controls.append("div").style("display", "inline-block");
    }
    return dom;
  }
  async preApiFreeze(api) {
    try {
      api.tip = new Menu({ padding: "5px" });
      api.printError = (e) => this.printError(e);
      const vocab = this.opts.state.vocab;
      api.vocabApi = this.opts.vocabApi ? this.opts.vocabApi : await vocabInit({
        app: api,
        state: {
          vocab: {
            // either (genome + dslabel) XOR (terms) can be undefined
            genome: vocab?.genome || this.opts.state.genome,
            dslabel: vocab?.dslabel || this.opts.state.dslabel,
            terms: vocab?.terms
          }
        },
        fetchOpts: this.opts.fetchOpts
      });
      this.opts.state.vocab = api.vocabApi.vocab;
    } catch (e) {
      console.log(`preApiFreeze error`, e);
      throw e;
    }
  }
  async init() {
    try {
      this.opts.state.nav = { header_mode: "hidden" };
      this.store = await storeInit({ app: this.api, state: this.opts.state });
      this.state = await this.store.copyState();
      this.components = {
        plots: {}
      };
      if (this.opts.app?.features?.includes("recover"))
        this.components.recover = await recoverInit({
          app: this.api,
          holder: this.dom.recoverControls,
          // TODO: ???? may limit the tracked state to only the filter, activeCohort ???
          getState: (appState) => appState,
          //reactsTo: action => true, //action.type != 'plot_edit' || action.type == 'app_refresh',
          maxHistoryLen: 10
        });
      if (this.opts.app?.doNotAwaitInitRender) {
        this.api.dispatch();
      } else {
        await this.api.dispatch();
      }
    } catch (e) {
      this.printError(e);
      throw e;
    }
  }
  async main() {
    this.api.vocabApi.main();
    for (const id in this.components.plots) {
      const plot = this.components.plots[id];
      if (!this.state.plots.find((p) => p.id === plot.id)) {
        plot.destroy();
        delete this.components.plots[id];
      }
    }
    for (const plot of this.state.plots.values()) {
      if (plot.parentId) continue;
      if (!this.components.plots[plot.id]) {
        const holder = this.opts?.app?.getPlotHolder ? this.opts.app.getPlotHolder(plot, this.dom.holder) : this.dom.holder.append("div");
        if (!this.dom.plotDiv) this.dom.plotDiv = holder;
        const { componentInit } = await importPlot(plot.chartType);
        const plotApi = await componentInit({
          id: plot.id,
          app: this.api,
          holder,
          controls: this.dom.plotControls
        });
        this.components.plots[plot.id] = plotApi;
      }
    }
  }
  printError(e) {
    sayerror(this.dom.errdiv, "Error: " + (e.message || e));
    if (e.stack) console.log(e.stack);
    this.bus.emit("error");
  }
  skipPrevActionAbort(action) {
    if (!action) return false;
    if (action.type.startsWith("filter")) return false;
    if (action.type.startsWith("cohort")) return false;
    if (action.type == "app_refresh") {
      if (action.subactions) {
        return action.subactions.find((a) => a.type.startsWith("filter") || a.type.startsWith("cohort")) ? false : true;
      }
    }
    return true;
  }
  destroy() {
    if (this.dom?.holder) this.dom.holder.selectAll("*").remove();
  }
};
var appInit = AppApi.getInitFxn(PlotApp);

export {
  appInit
};
//# sourceMappingURL=chunk-N2SKTCDK.js.map
