import {
  renderTable
} from "./chunk-SA7APTJR.js";

// termdb/handlers/termCollection.ts
var SearchHandler = class {
  async init(opts) {
    this.callback = opts.callback;
    this.app = opts.app;
    opts.holder.style("display", "");
    const tableDiv = opts.holder.append("div");
    const termlst = opts.details.termlst ?? [];
    renderTable({
      columns: [{ label: "VARIABLES" }],
      rows: termlst.map((t) => {
        return [{ value: t.name }];
      }),
      div: tableDiv,
      maxWidth: "30vw",
      maxHeight: "40vh",
      noButtonCallback: () => {
      },
      // FIXME to supply a real callback
      striped: false,
      showHeader: true,
      //false,
      selectAll: true,
      columnButtons: void 0,
      //Leave until table.js is typed
      buttons: void 0
    });
    let categoryTable;
    let ckSource = [];
    if (opts.details.categoryKeys) {
      ckSource = opts.details.categoryKeys;
      const categoryDiv = opts.holder.append("div").style("margin-top", "15px");
      const values = opts.details.termlst[0].values || {};
      categoryTable = categoryDiv.append("div");
      renderTable({
        columns: [{ label: "CATEGORIES" }],
        rows: ckSource.map((ck) => {
          return [{ value: values[ck.key]?.label ?? ck.key, checked: ck.shown }];
        }),
        div: categoryTable,
        maxWidth: "30vw",
        maxHeight: "40vh",
        noButtonCallback: () => {
        },
        // FIXME to supply a real callback
        striped: false,
        showHeader: true,
        //false,
        selectAll: true,
        columnButtons: void 0,
        //Leave until table.js is typed
        buttons: void 0
      });
    }
    opts.holder.append("div").style("float", "right").style("padding", "6px 20px").append("button").attr("class", "sjpp_apply_btn sja_filter_tag_btn").text("Select").on("click", () => {
      const trs = tableDiv.select("table").select("tbody").node().querySelectorAll("tr");
      const selectedTermlst = termlst.filter((term, i) => {
        const checked = trs[i]?.querySelectorAll("td")[1]?.querySelector("input")?.checked;
        return checked === true;
      });
      if (selectedTermlst.length === 0) {
        alert("Please select at least one term");
        return;
      }
      const propsByTermId = {};
      if (opts.details.propsByTermId) {
        for (const t of selectedTermlst) {
          if (opts.details.propsByTermId[t.id]) propsByTermId[t.id] = opts.details.propsByTermId[t.id];
        }
      }
      let categoryKeys;
      if (categoryTable) {
        const trs2 = categoryTable.select("table").select("tbody").node().querySelectorAll("tr");
        categoryKeys = ckSource.map((ck, i) => {
          const checked = trs2[i].querySelectorAll("td")[1].querySelector("input")?.checked;
          return { key: ck.key, shown: !!checked };
        });
      }
      opts.callback({
        type: "termCollection",
        termIds: selectedTermlst.map((i) => i.id),
        termlst: selectedTermlst,
        name: opts.details.name,
        valueTransform: opts.details.valueTransformByPlots?.[opts.usecase.target],
        // memberType = ds.cohort.termdb.termCollections[].type for client code
        memberType: opts.details.memberType || opts.details.type,
        categoryKeys,
        isleaf: true,
        propsByTermId
      });
    });
  }
};

export {
  SearchHandler
};
//# sourceMappingURL=chunk-54EMVNPW.js.map
