import {
  keyupEnter
} from "./chunk-SA7APTJR.js";
import {
  require_debounce
} from "./chunk-LSEFWW72.js";
import {
  Menu
} from "./chunk-HYOEWQ5P.js";
import {
  dofetch,
  dofetch3
} from "./chunk-7EMWHCVW.js";
import {
  __toESM
} from "./chunk-HFNDKYVF.js";

// src/gene.js
var import_debounce = __toESM(require_debounce(), 1);
var tip;
function gene_searchbox(p) {
  if (!tip) {
    tip = new Menu({ padding: "" });
    tip.d.style("z-index", 1e3);
  }
  const input = p.div.append("input").attr("placeholder", "Search gene").style("width", p.width || "100px");
  const printdiv = p.resultdiv || (p.tip ? p.tip.d : tip.d);
  function fold() {
    if (p.resultdiv) {
      p.resultdiv.selectAll("*").remove();
    } else if (p.tip) {
      p.tip.hide();
    } else {
      tip.hide();
    }
  }
  input.on("keyup", (event) => {
    const str = event.target.value;
    if (str.length <= 1) {
      fold();
      return;
    }
    if (keyupEnter(event)) {
      const hitgene = printdiv.select(".sja_menuoption");
      if (hitgene.size() > 0) {
        p.callback(hitgene.text());
        fold();
      }
      return;
    }
    debouncer();
  });
  input.node().focus();
  function genesearch() {
    dofetch("genelookup", { genome: p.genome, input: input.property("value") }).then((data) => {
      if (data.error) throw data.error;
      if (!data.hits) throw ".hits[] missing";
      if (p.resultdiv) {
        p.resultdiv.selectAll("*").remove();
      } else if (p.tip) {
        p.tip.clear().showunder(input.node());
      } else {
        tip.clear().showunder(input.node());
      }
      for (const name of data.hits) {
        printdiv.append("div").attr("class", "sja_menuoption").text(name).on("click", () => {
          p.callback(name);
          fold();
        });
      }
    }).catch((err) => {
      printdiv.append("div").text(err.message || err);
      if (err.stack) console.log(err.stack);
    });
  }
  const debouncer = (0, import_debounce.debounce)(genesearch, 300);
}
function findgenemodel_bysymbol(genome, str) {
  return dofetch3("genelookup", {
    body: {
      deep: 1,
      input: str,
      genome
    }
  }).then((data) => {
    if (data.error) throw data.error;
    if (!data.gmlst || data.gmlst.length == 0) return null;
    return data.gmlst;
  }).catch((e) => {
    throw e;
  });
}

export {
  gene_searchbox,
  findgenemodel_bysymbol
};
//# sourceMappingURL=chunk-LTOBEUM2.js.map
