import {
  PlotBase,
  controlsInit,
  downloadSVGsAsPdf
} from "./chunk-SA7APTJR.js";
import "./chunk-HJ6L54YS.js";
import "./chunk-LSEFWW72.js";
import {
  importPlot
} from "./chunk-SKREEF3H.js";
import "./chunk-HYOEWQ5P.js";
import "./chunk-HBW42TDT.js";
import "./chunk-G6O3URDN.js";
import "./chunk-FN5XPUPH.js";
import {
  icons
} from "./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/report/view/reportView.ts
var ReportView = class {
  constructor(report) {
    this.opts = report.opts;
    this.report = report;
    const leftDiv = this.opts.holder.insert("div").style("display", "inline-block");
    const controlsHolder = leftDiv.insert("div").style("display", "inline-block");
    const mainDiv = this.opts.holder.insert("div").style("display", "inline-block").style("vertical-align", "top").style("padding", "20px");
    const headerDiv = mainDiv.append("div").style("padding", "10px");
    const downloadDiv = headerDiv.append("div").style("display", "inline-block").style("padding-left", "10px").style("vertical-align", "middle");
    icons["pdf"](downloadDiv, {
      handler: () => this.report.downloadReport(),
      title: "Download the report",
      width: 20,
      height: 20
    });
    const plotsDiv = mainDiv.append("div");
    this.dom = {
      headerDiv,
      plotsDiv,
      header: this.opts.header,
      //holder,
      controlsHolder
    };
    if (this.dom.header) {
      this.dom.header.html(this.report.config.name || "Summary Report");
    }
  }
  getControlInputs() {
    const inputs = [];
    return inputs;
  }
};

// plots/report/report.ts
var Report = class _Report extends PlotBase {
  constructor(opts, api) {
    super(opts, api);
    this.components = {};
    this.type = _Report.type;
  }
  static {
    this.type = "report";
  }
  async init(appState) {
    this.config = appState.plots.find((p) => p.id === this.id);
    this.view = new ReportView(this);
    this.components = { plots: {} };
    const state = this.getState(appState);
    for (const section of state.config.sections) {
      const div = this.view.dom.plotsDiv.append("div");
      const headerIcon = div.append("span").style("margin-right", "10px").style("cursor", "pointer").text("\u25BC");
      div.append("div").style("display", "inline-block").style("font-size", "1.2em").style("margin", "10px 0px").style("font-weight", "bold").text(section.name);
      const sectionDiv = div.append("div").style("margin", "10px").style("display", "flex").style("flex-direction", "row").style("flex-wrap", "wrap").style("width", "100vw");
      headerIcon.on("click", () => {
        const display = sectionDiv.style("display");
        headerIcon.text(display === "none" ? "\u25BC" : "\u25B2");
        sectionDiv.style("display", display === "none" ? "flex" : "none");
      });
      for (const plot of section.plots) {
        if (this.components.plots[plot.id]) continue;
        await this.setPlot(plot, sectionDiv);
      }
    }
  }
  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 {
      config,
      plots: appState.plots.filter((p) => p.parentId === this.id),
      //this property is needed to indicate that child plots need to be added to the appState plots
      termfilter: appState.termfilter,
      vocab: appState.vocab,
      termdbConfig: appState.termdbConfig
    };
  }
  async main() {
    this.config = structuredClone(this.state.config);
    this.settings = this.config.settings.report;
  }
  async setPlot(plot, sectionDiv) {
    const plotDiv = sectionDiv.append("div").style("display", "inline-block").style("margin", "10px");
    const headerDiv = plotDiv.append("div");
    const headerIcon = headerDiv.append("span").style("margin", "10px").style("cursor", "pointer").text("\u25BC");
    const header = headerDiv.append("div").style("display", "inline-block").style("font-size", "1.1em").style("margin-top", "10px");
    headerIcon.on("click", () => {
      const display = holder.style("display");
      headerIcon.text(display === "none" ? "\u25BC" : "\u25B2");
      holder.style("display", display === "none" ? "block" : "none");
    });
    const holder = plotDiv.append("div");
    const opts = structuredClone(plot);
    opts.header = header;
    opts.holder = holder;
    opts.app = this.app;
    opts.parentId = this.id;
    const { componentInit: componentInit2 } = await importPlot(opts.chartType);
    this.components.plots[opts.id] = await componentInit2(opts);
  }
  async setControls() {
    this.view.dom.controlsHolder.selectAll("*").remove();
    const inputs = this.view.getControlInputs();
    this.components = {
      controls: await controlsInit({
        app: this.app,
        id: this.id,
        holder: this.view.dom.controlsHolder,
        inputs
      })
    };
  }
  getChartImages() {
    const chartImagesAll = [];
    for (const section of this.config.sections) {
      for (const plotConfig of section.plots) {
        const plot = this.components.plots[plotConfig.id];
        if (plot?.getChartImages) {
          const chartImages = plot.getChartImages();
          for (const chartImage of chartImages) {
            if (chartImage.name.trim()) chartImage.name = `${section.name} / ${chartImage.name}`;
            else chartImage.name = section.name;
            chartImagesAll.push(chartImage);
          }
        }
      }
    }
    return chartImagesAll;
  }
  async downloadReport() {
    const chartImagesAll = this.getChartImages();
    const globalFilterImg = await this.app.getComponents("nav.filter").getFilterImage();
    const filterImg = await this.opts.getFilterImage();
    const filterImgs = [];
    if (globalFilterImg) filterImgs.push(globalFilterImg);
    if (filterImg) filterImgs.push(filterImg);
    downloadSVGsAsPdf(chartImagesAll, "report", "landscape", filterImgs);
  }
};
async function getPlotConfig(opts, app) {
  const settings = getDefaultReportSettings();
  const plot = {
    settings: {
      controls: {
        isOpen: false
        // control panel is hidden by default
      },
      report: settings,
      startColor: {},
      //dict to store the start color of the gradient for each chart when using continuous color
      stopColor: {}
      //dict to store the stop color of the gradient for each chart when using continuous color
    }
  };
  try {
    const config = app.vocabApi?.termdbConfig?.plotConfigByCohort?.default?.report;
    opts.filter = app.vocabApi?.termdbConfig?.authFilter;
    copyMerge(plot, config, opts);
    return plot;
  } catch (e) {
    console.log(e);
    throw `${e} [sampleScatter getPlotConfig()]`;
  }
}
var reportInit = getCompInit(Report);
var componentInit = reportInit;
function getDefaultReportSettings() {
  return {};
}
export {
  Report,
  componentInit,
  getDefaultReportSettings,
  getPlotConfig,
  reportInit
};
//# sourceMappingURL=report-ZOVQCOGQ.js.map
