import {
  mayHydrateDictTwLst,
  renderTable
} from "./chunk-SA7APTJR.js";
import "./chunk-HJ6L54YS.js";
import {
  HandlerBase
} from "./chunk-LSEFWW72.js";
import "./chunk-SKREEF3H.js";
import "./chunk-HYOEWQ5P.js";
import "./chunk-HBW42TDT.js";
import "./chunk-G6O3URDN.js";
import "./chunk-FN5XPUPH.js";
import "./chunk-IIT367QZ.js";
import "./chunk-RZGEKL77.js";
import "./chunk-UI7OCM2A.js";
import "./chunk-YZK37YCC.js";
import "./chunk-7EMWHCVW.js";
import "./chunk-7IYJZZQI.js";
import "./chunk-M3J4MINX.js";
import "./chunk-PF4DSFDR.js";
import "./chunk-QH6W3NVF.js";
import "./chunk-NOEAT6CX.js";
import "./chunk-4UWS5Y3N.js";
import "./chunk-DD4R5P6W.js";
import "./chunk-JNITUVXP.js";
import "./chunk-KSGA62R2.js";
import "./chunk-LOZEKOES.js";
import "./chunk-TOU7EVFQ.js";
import "./chunk-OAWQ6LOO.js";
import "./chunk-TLT4YIG3.js";
import "./chunk-KYBIQBXE.js";
import "./chunk-I6Y4O3RR.js";
import "./chunk-OMR2DT66.js";
import "./chunk-DQC5FFGV.js";
import "./chunk-HFNDKYVF.js";

// termsetting/handlers/termCollection.ts
var TermCollectionHandler = class extends HandlerBase {
  //main menu dom elements before drag and drop divs are added
  constructor(opts) {
    super(opts);
    this.dom = {};
    this.termsetting = opts.termsetting;
    this.tw = opts.termsetting.tw;
    this.dom.holder = opts.holder || this.termsetting.dom.tip.d;
  }
  async showEditMenu(div) {
    const self = this.termsetting;
    div.selectAll("*").remove();
    const termIds = self.vocabApi.termdbConfig.termCollections?.find((c) => c.name === self.term.name)?.termIds || [];
    const terms = [];
    const toBeHydrated = [];
    for (const id of termIds) {
      const term = self.term.termlst.find((t) => t.id === id);
      if (term) terms.push(term);
      else toBeHydrated.push({ id });
    }
    if (toBeHydrated.length) {
      await mayHydrateDictTwLst(toBeHydrated, self.vocabApi);
      terms.push(...toBeHydrated.map((tw) => tw.term));
    }
    const groupDiv = div.append("div");
    const noButtonCallback = (i, node) => {
      terms[i].checked = node.checked;
    };
    const callback = this.tw.term.memberType == "numeric" ? addNumericTable(self, groupDiv, terms, noButtonCallback) : addCategoricalTable(self, groupDiv, terms, noButtonCallback);
    div.append("div").append("div").style("float", "right").style("padding", "6px 20px").append("button").attr("class", "sjpp_apply_btn sja_filter_tag_btn").text("Apply").on("click", callback);
  }
  getPillStatus() {
    return this.tw.getStatus(this.termsetting.usecase);
  }
  // inherited from HanlderBase
  // getPillName(d: PillData) {}
};
function addNumericTable(self, div, terms, noButtonCallback) {
  const rows = [];
  for (const term of terms) {
    const checked = self.q.numerators?.find((tid) => tid === term.id) ? "checked" : "";
    rows.push([{ value: term.name }, { html: `<input type='checkbox' ${checked} />` }]);
  }
  const selectedRows = terms.map((term, index) => self.term.termlst.find((t) => t.id === term.id) ? index : -1).filter((index) => index !== -1);
  const columns = [{ label: "VARIABLES" }, { label: "Use for sorting" }];
  renderTable({
    rows,
    columns,
    div,
    maxWidth: "30vw",
    maxHeight: "40vh",
    noButtonCallback,
    striped: false,
    showHeader: true,
    //false,
    selectedRows,
    columnButtons: void 0,
    //Leave until table.js is typed
    buttons: void 0
  });
  return () => {
    const q = self.q;
    const trs = div.select("table").select("tbody").node().querySelectorAll("tr");
    self.term.termlst = terms.filter((_, i) => trs[i].querySelectorAll("td")[1].querySelector("input")?.checked);
    q.numerators = terms.filter((term, i) => {
      const checked = trs[i].querySelectorAll("td")[1].querySelector("input")?.checked;
      return checked === true;
    }).map((t) => t.id);
    self.api.runCallback();
  };
}
function addCategoricalTable(self, div, terms, noButtonCallback) {
  const rows = [];
  for (const term of terms) {
    rows.push([{ value: term.name }]);
  }
  const selectedRows = terms.map((term, index) => self.term.termlst.find((t) => t.id === term.id) ? index : -1).filter((index) => index !== -1);
  const columns = [{ label: "VARIABLES" }];
  renderTable({
    rows,
    columns,
    div: div.append("div").style("margin-top", "10px"),
    maxWidth: "30vw",
    maxHeight: "40vh",
    noButtonCallback,
    striped: false,
    showHeader: true,
    //false,
    selectedRows,
    columnButtons: void 0,
    //Leave until table.js is typed
    buttons: void 0
  });
  const categoryDiv = div.append("div").style("margin-top", "15px");
  const values = Object.assign({}, ...self.tw.term.termlst?.map((t) => t.values || {}) ?? []);
  const categoryTable = categoryDiv.append("div");
  const ckSource = self.tw.q.categoryKeys || self.tw.term.categoryKeys || [];
  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
  });
  return () => {
    const trs = div.select("table").select("tbody").node().querySelectorAll("tr");
    self.term.termlst = terms.filter((term, i) => {
      const checked = trs[i].querySelectorAll("td")[1].querySelector("input")?.checked;
      return checked === true;
    });
    const catTrs = categoryTable.select("table").select("tbody").node().querySelectorAll("tr");
    self.q.categoryKeys = ckSource.map((ck, i) => {
      const checked = catTrs[i].querySelectorAll("td")[1].querySelector("input")?.checked;
      return { key: ck.key, shown: !!checked };
    });
    self.api.runCallback();
  };
}
function fillTW(tw) {
  if (!tw.type) tw.type = "termCollection";
  if (!tw.q) tw.q = { mode: "continuous", type: "compositePercentage" };
  else tw.q.mode = "continuous";
  return tw;
}
export {
  TermCollectionHandler,
  fillTW
};
//# sourceMappingURL=termCollection-GMDDL3L7.js.map
