import {
  renderTable
} from "./chunk-SA7APTJR.js";
import "./chunk-HJ6L54YS.js";
import "./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 {
  validateTermCollectionTvs
} from "./chunk-7IYJZZQI.js";
import "./chunk-M3J4MINX.js";
import "./chunk-PF4DSFDR.js";
import "./chunk-QH6W3NVF.js";
import "./chunk-NOEAT6CX.js";
import "./chunk-4UWS5Y3N.js";
import {
  format_val_text
} from "./chunk-7XFGYYRV.js";
import {
  NumericRangeInput
} from "./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-ESEQBXTM.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";

// filter/tvs.termCollection.ts
var handler = {
  type: "termCollection",
  term_name_gen,
  get_pill_label,
  getSelectRemovePos,
  fillMenu,
  setTvsDefaults
};
function setTvsDefaults(tvs) {
  if (!tvs.ranges) tvs.ranges = [];
}
async function fillMenu(self, div, tvs) {
  tvs = structuredClone(tvs);
  div.selectAll("*").remove();
  div = div.append("div").style("font-size", "0.8em");
  let details = self.opts.vocabApi.termdbConfig.termCollections?.find((c) => c.name === tvs.term.name);
  if (!details) {
    if (tvs.term.isCustom && tvs.term.termlst?.length) {
      details = { termlst: tvs.term.termlst, type: tvs.term.memberType || "numeric" };
    } else {
      throw new Error(`No termCollection found for name=${tvs.term.name}`);
    }
  }
  if (details.type !== "numeric") throw new Error("filter only supports numeric term collection");
  const rangeInput = renderRangeInput(div, tvs, applyRange);
  const getTableData = await addFilterTable({ holder: div, tvs, details, vocabApi: self.opts.vocabApi });
  async function applyRange(tvs2) {
    const tvsProps = getTableData();
    if (!tvsProps) return;
    tvs2.term.termlst = tvsProps.termlst;
    tvs2.term.numerators = tvsProps.numerators;
    self.dom.tip.hide();
    self.opts.callback({ term: tvs2.term, ranges: [rangeInput.getRange()] });
  }
}
function renderRangeInput(div, tvs, applyRange) {
  const termrange = tvs.term.range || {};
  const range = tvs.ranges && tvs.ranges[0] ? tvs.ranges[0] : termrange;
  const num_div = div.append("div");
  num_div.selectAll("*").remove();
  const table = num_div.append("table");
  const tr = table.append("tr");
  tr.append("td").text("Percentage 0 to 100");
  const equation_td = tr.append("td");
  range.min = 0;
  range.max = 100;
  const rangeInput = new NumericRangeInput(equation_td, range, () => applyRange(tvs));
  tr.append("td").attr("class", "sja_filter_tag_btn sjpp_apply_btn").style("border-radius", "13px").style("margin", "5px").style("margin-left", "10px").style("text-align", "center").style("font-size", ".8em").text("APPLY").on("click", async () => {
    rangeInput.parseRange();
  });
  return rangeInput;
}
async function addFilterTable(opts) {
  if (!opts.tvs.term.numerators) opts.tvs.term.numerators = opts.tvs.term.termlst.map((t) => t.id);
  const termlst = opts.tvs.term.termlst || [];
  const numerators = opts.tvs.term.numerators || [];
  const rows = [];
  for (const term of opts.details.termlst) {
    const numeratorChecked = numerators?.find((tid) => tid === term.id) ? "checked" : "";
    const denominatorChecked = opts.tvs.term.termlst.find((t) => t.id === term.id) ? "checked" : "";
    rows.push([
      { value: term.name },
      { html: `<input type='checkbox' ${numeratorChecked} />` },
      { html: `<input type='checkbox' ${denominatorChecked} />` }
    ]);
  }
  const selectedRows = opts.details.termlst.map((term, index) => termlst.find((t) => t.id === term.id) ? index : -1).filter((index) => index !== -1);
  const columns = [{ label: "Variables" }, { label: "Use in numerator" }, { label: "Use in denominator" }];
  const tableDiv = opts.holder.append("div");
  renderTable({
    rows,
    columns,
    div: tableDiv,
    maxWidth: "30vw",
    maxHeight: "40vh",
    striped: false,
    showHeader: true,
    selectedRows,
    columnButtons: void 0,
    buttons: void 0
  });
  return () => {
    const trs = tableDiv.select("table").select("tbody").node().querySelectorAll("tr");
    const numerators2 = opts.details.termlst.filter((term, i) => {
      const checked = trs[i].querySelectorAll("td")[2].querySelector("input")?.checked;
      return checked === true;
    }).map((t) => t.id);
    const termlst2 = opts.details.termlst.filter((term, i) => {
      const checked = trs[i].querySelectorAll("td")[3].querySelector("input")?.checked;
      return checked === true;
    });
    try {
      validateTermCollectionTvs(
        numerators2,
        termlst2.map((i) => i.id)
      );
      return { numerators: numerators2, termlst: termlst2 };
    } catch (e) {
      window.alert(e.message);
    }
  };
}
function getSelectRemovePos(j) {
  return j;
}
function term_name_gen(d) {
  return `Percentage(${d.term.numerators.join("+")})`;
}
function get_pill_label(tvs) {
  return { txt: format_val_text(tvs.ranges[0], tvs.term) };
}
export {
  addFilterTable,
  handler
};
//# sourceMappingURL=tvs.termCollection-L527XN4X.js.map
