import {
  addGeneSearchbox,
  first_genetrack_tolist,
  getDNAMethUnit,
  sayerror
} from "./chunk-SA7APTJR.js";
import {
  Menu
} from "./chunk-HYOEWQ5P.js";
import {
  DNA_METHYLATION
} from "./chunk-NOEAT6CX.js";

// termdb/handlers/dnaMethylation.ts
var SearchHandler = class {
  init(opts) {
    this.opts = opts;
    this.callback = opts.callback;
    this.app = opts.app;
    const holder = opts.holder.append("div").style("margin", "10px 0px");
    this.dom = {};
    this.dom.errDiv = holder.append("div").style("margin", "5px 0px").style("display", "none");
    this.dom.geneSearchDiv = holder.append("div");
    this.dom.blockDiv = holder.append("div").style("display", "none").style("margin", "15px 4px");
    const geneSearch = addGeneSearchbox({
      tip: new Menu({ padding: "0px" }),
      genome: opts.genomeObj,
      row: this.dom.geneSearchDiv,
      callback: async () => {
        try {
          this.dom.errDiv.style("display", "none");
          await this.handleGeneSearch(geneSearch);
        } catch (e) {
          this.dom.errDiv.style("display", "block");
          sayerror(this.dom.errDiv, "Error: " + (e.message || e));
          if (e.stack) console.log(e.stack);
        }
      }
    });
  }
  async handleGeneSearch(geneSearch) {
    if (geneSearch.geneSymbol) {
      const { chr, start, stop } = geneSearch;
      if (!chr || !Number.isInteger(start) || !Number.isInteger(stop))
        throw new Error("unable to retrieve gene coordinate");
      this.dom.blockDiv.selectAll("*").remove();
      this.dom.blockDiv.style("display", "block");
      this.dom.blockDiv.append("div").style("opacity", 0.6).text("Navigate genome browser to desired region");
      const arg = {
        holder: this.dom.blockDiv,
        genome: this.opts.genomeObj,
        // genome obj
        chr,
        start,
        stop,
        tklst: [],
        nobox: true,
        width: 500,
        hidegenelegend: true,
        debugmode: this.opts.debug
      };
      first_genetrack_tolist(this.opts.genomeObj, arg.tklst);
      const _ = await import("./block-TTN2IQAH.js");
      this.blockInstance = new _.Block(arg);
      this.dom.submitBtn = this.dom.blockDiv.append("div").attr("data-testid", "sjpp-dnaMethylation-submitDiv").style("margin", "10px 0px").append("button").style("border", "none").style("border-radius", "20px").style("padding", "10px 15px").text("Submit Region").on("click", async () => {
        const { chr: chr2, start: start2, stop: stop2 } = this.blockInstance.rglst[0];
        const term = this.makeTerm({ chr: chr2, start: start2, stop: stop2 });
        await this.callback(term);
      });
    } else if (geneSearch.chr && Number.isInteger(geneSearch.start) && Number.isInteger(geneSearch.stop)) {
      const { chr } = geneSearch;
      let { start, stop } = geneSearch;
      if (geneSearch.actualposition?.len <= 1) {
        start = geneSearch.actualposition.position;
        stop = start + 1;
      }
      const term = this.makeTerm({ chr, start, stop });
      await this.callback(term);
    } else {
      throw new Error("invalid gene search input");
    }
  }
  makeTerm(opts) {
    const { chr, start, stop } = opts;
    if (!chr || !Number.isInteger(start) || !Number.isInteger(stop)) throw new Error("invalid coordinate");
    const unit = getDNAMethUnit("region", this.app.vocabApi);
    const term = {
      chr,
      start,
      stop,
      type: DNA_METHYLATION,
      unit,
      genomicFeatureType: "region"
    };
    return term;
  }
};

export {
  SearchHandler
};
//# sourceMappingURL=chunk-2XCUERHJ.js.map
