import {
  addGeneSearchbox,
  isoformSelect,
  sayerror
} from "./chunk-SA7APTJR.js";
import {
  Menu
} from "./chunk-HYOEWQ5P.js";
import {
  dofetch3
} from "./chunk-7EMWHCVW.js";
import {
  ISOFORM_EXPRESSION
} from "./chunk-NOEAT6CX.js";

// termdb/handlers/isoformExpression.ts
var SearchHandler = class {
  constructor() {
    this.currentGene = null;
  }
  init(opts) {
    this.callback = opts.callback;
    this.app = opts.app;
    const holder = opts.holder.append("div").style("padding", "10px 0px");
    this.dom = {
      errDiv: holder.append("div").style("margin", "5px 0px").style("display", "none")
    };
    const geneSearch = addGeneSearchbox({
      tip: new Menu({ padding: "0px" }),
      genome: opts.genomeObj,
      row: holder,
      searchOnly: "gene",
      callback: async () => {
        try {
          this.dom.errDiv.style("display", "none");
          if (!geneSearch.geneSymbol) throw new Error("No gene selected");
          if (geneSearch.geneSymbol === this.currentGene) return;
          this.currentGene = geneSearch.geneSymbol;
          if (this.dom.isoformDiv) this.dom.isoformDiv.remove();
          this.dom.isoformDiv = holder.append("div");
          await this.showIsoforms(geneSearch.geneSymbol, opts.genomeObj);
        } catch (e) {
          this.dom.errDiv.style("display", "block");
          sayerror(this.dom.errDiv, "Error: " + (e instanceof Error ? e.message : String(e)));
        }
      }
    });
  }
  async showIsoforms(gene, genomeObj) {
    if (!gene) throw new Error("No gene selected");
    const data = await dofetch3("genelookup", { body: { genome: genomeObj.name, input: gene, deep: 1 } });
    if (!data.gmlst?.length) throw new Error(`No isoforms found for ${gene}`);
    const enstCandidates = data.gmlst.filter((gm) => gm.isoform?.startsWith("ENST"));
    if (enstCandidates.length === 0) throw new Error(`No Ensembl transcript isoforms found for ${gene}`);
    const { available } = await dofetch3("termdb/isoformAvailability", {
      body: {
        genome: genomeObj.name,
        dslabel: this.app.vocabApi.vocab.dslabel,
        isoforms: enstCandidates.map((gm) => gm.isoform)
      }
    });
    const availableSet = new Set(available || []);
    const enstModels = enstCandidates.filter((gm) => availableSet.has(gm.isoform));
    if (enstModels.length === 0) throw new Error(`No isoforms with data found for ${gene}`);
    if (gene !== this.currentGene) return;
    const div = this.dom.isoformDiv;
    div.append("div").style("margin-bottom", "8px").style("opacity", 0.65).text(`${gene} \u2014 select isoform(s):`);
    isoformSelect({
      holder: div,
      allgm: enstModels,
      multiSelect: true,
      submitLabel: "Create Collection",
      onMultiSelect: (selected) => {
        if (selected.length === 1) {
          this.selectIsoform(selected[0].isoform, gene);
        } else {
          this.selectCollection(selected, gene);
        }
      }
    });
  }
  getUnit() {
    return this.app.vocabApi.termdbConfig.queries.isoformExpression?.unit || "TPM";
  }
  selectIsoform(isoform, gene) {
    const name = `${isoform} ${this.getUnit()}`;
    this.callback({ isoform, gene, name, type: ISOFORM_EXPRESSION });
  }
  selectCollection(gms, gene) {
    const unit = this.getUnit();
    const termlst = gms.map((gm) => ({
      id: gm.isoform,
      name: gm.isoform,
      type: ISOFORM_EXPRESSION,
      isoform: gm.isoform
    }));
    this.callback({
      type: "termCollection",
      isCustom: true,
      memberType: "numeric",
      name: `${gene} Isoforms (${unit})`,
      termlst,
      propsByTermId: {},
      isleaf: true
    });
  }
};
function filterIsoforms(gmlst, availableItems) {
  const itemSet = new Set(availableItems);
  return gmlst.filter((gm) => gm.isoform?.startsWith("ENST") && (itemSet.size === 0 || itemSet.has(gm.isoform)));
}

export {
  SearchHandler,
  filterIsoforms
};
//# sourceMappingURL=chunk-CC3GFVV6.js.map
