import {
  SearchHandler,
  fillTermWrapper,
  table2col,
  termsettingInit
} from "./chunk-SA7APTJR.js";

// plots/summarizeMutationDiagnosis.ts
async function makeChartBtnMenu(holder, chartsInstance) {
  let dictTw;
  {
    const t = chartsInstance.app.vocabApi.termdbConfig.defaultTw4correlationPlot?.disease;
    if (!t) throw "defaultTw4correlationPlot missing";
    dictTw = structuredClone(t);
    await fillTermWrapper(dictTw, chartsInstance.app.vocabApi);
  }
  const table = table2col({
    holder: holder.append("div"),
    margin: "0px 10px 10px 10px",
    cellPadding: "10px"
  });
  {
    const [td1, td2] = table.addRow();
    td1.text("Mutation Variable");
    const searchDiv = td2.append("div");
    const geneSearchInst = new SearchHandler();
    geneSearchInst.init({
      holder: searchDiv,
      app: chartsInstance.app,
      // required to supply "opts.app.vocabApi" for the search ui
      genomeObj: chartsInstance.app.opts.genome,
      msg: "Hit ENTER to launch plot.",
      callback: async (geneTw) => {
        await fillTermWrapper(geneTw, chartsInstance.app.vocabApi);
        launchPlot({
          tw1: dictTw,
          tw2: geneTw,
          chartsInstance,
          holder
        });
      }
    });
    searchDiv.style("padding", "0px 0px 5px 0px");
  }
  {
    const [td1, td2] = table.addRow();
    td1.text("Compare Mutations Against");
    const pillDiv = td2.append("div"), waitDiv = td2.append("div").style("font-size", ".7em").text("LOADING ...");
    const pill = await termsettingInit({
      menuOptions: "{edit,replace}",
      /** presumably this usecase let it restrict to dictionary term ui, and hide genomic queries
      target="filter" works for gdc since in gdc ds it is overriding filter to dict
      but is not a general fix for non-gdc ds, which Replace menu will launch genomic+dict options
      maybe this is okay for non-gdc ds as the default dictTw is meaningful
      */
      usecase: { target: "filter" },
      vocabApi: chartsInstance.app.vocabApi,
      holder: pillDiv,
      callback: async (tw) => {
        waitDiv.text("LOADING ...");
        try {
          await pill.main(tw);
          dictTw = tw;
          waitDiv.text("Click to edit/replace the variable before searching gene.");
        } catch (e) {
          waitDiv.text("Error: " + (e.message || e));
        }
      }
    });
    try {
      await fillTermWrapper(dictTw, chartsInstance.app.vocabApi);
      await pill.main(dictTw);
      waitDiv.text("Click to edit/replace the variable before searching gene.");
    } catch (e) {
      waitDiv.text("Error: " + (e.message || e));
    }
  }
}
function launchPlot({ tw1, tw2, chartsInstance, holder }) {
  const chart = {
    config: {
      chartType: tw1?.term?.type == "survival" ? "survival" : "summary",
      // TODO define sandbox header with gene+term name
      term: tw1,
      term2: tw2
    }
  };
  chartsInstance.plotCreate(chart);
  holder.selectAll("*").remove();
  holder.append("div").style("margin", "20px").text("LOADING CHART ...");
  setTimeout(() => {
    holder.style("display", "none");
  }, 1e3);
}

export {
  makeChartBtnMenu,
  launchPlot
};
//# sourceMappingURL=chunk-ULPMYS5B.js.map
