import {
  agedx
} from "./chunk-32AI7I4C.js";
import {
  NumericHandler
} from "./chunk-ZXTHADSC.js";
import "./chunk-WHI6466S.js";
import {
  termjson
} from "./chunk-DHPLHIVP.js";
import {
  require_tape
} from "./chunk-QWOE5YTB.js";
import {
  TwRouter
} 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 "./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 {
  select_default
} from "./chunk-I6Y4O3RR.js";
import "./chunk-OMR2DT66.js";
import "./chunk-DQC5FFGV.js";
import {
  __toESM
} from "./chunk-HFNDKYVF.js";

// termsetting/handlers/test/NumCustomBinEditor.unit.spec.ts
var import_tape = __toESM(require_tape(), 1);
var pct50 = 6.3475409836;
var pct70 = 1000.36986301355;
async function getNumericHandler(opts = {}) {
  const term = JSON.parse(JSON.stringify(termjson.agedx));
  const rawTw = {
    term,
    q: {
      mode: "discrete",
      type: "custom-bin",
      preferredBins: "median"
    }
  };
  let handler = {};
  const vocabApi = {
    state: {
      termfilter: { filter: { type: "tvslst", in: "", lst: [] } }
    },
    getViolinBox() {
      return agedx;
    },
    getPercentile() {
      return {
        values: handler?.editHandler?.q.cutoffPercentile === 70 ? [pct70] : [pct50]
      };
    }
  };
  const tw = await TwRouter.initRaw(rawTw, { vocabApi });
  handler = new NumericHandler({
    termsetting: {
      tw,
      term: rawTw.term,
      q: rawTw.q,
      opts: {
        numericEditMenuVersion: ["discrete"],
        usecase: opts.usecase
      },
      api: {
        runCallback() {
        }
      },
      dom: {
        tip: {
          hide() {
          }
        }
      },
      vocabApi
    }
  });
  handler.density_data = { min: agedx.min, max: agedx.max };
  const holder = select_default("body").append("div").style("position", "relative").style("width", "fit-content").style("margin", "20px").style("padding", "5px").style("border", "1px solid #000");
  handler.dom.editDiv = holder;
  await handler.setEditHandler(handler.tabs[0]);
  const binsEditor = handler.editHandler.editorsByType["custom-bin"];
  return {
    rawTw,
    tw,
    handler,
    editHandler: handler.editHandler,
    binsEditor,
    holder,
    destroy: () => {
      if ("destroy" in handler.editHandler) handler.editHandler.destroy();
      if (typeof handler.destroy == "function") handler.destroy();
      holder.remove();
    }
  };
}
(0, import_tape.default)("\n", (test) => {
  test.comment("-***- NumCustomBinEditor.unit.spec -***-");
  test.end();
});
(0, import_tape.default)("handler methods", async (test) => {
  const { binsEditor, destroy } = await getNumericHandler();
  test.deepEqual(
    binsEditor.getDefaultQ(),
    {
      mode: "discrete",
      type: "custom-bin",
      isAtomic: true,
      lst: [
        {
          startunbounded: true,
          stop: 6.35,
          stopinclusive: false,
          label: "<6.35",
          range: '<span style="font-family:Times;font-style:italic;">x</span> <6.35'
        },
        {
          start: 6.35,
          startinclusive: true,
          stopunbounded: true,
          label: "\u22656.35",
          range: '<span style="font-family:Times;font-style:italic;">x</span> \u22656.35'
        }
      ],
      hiddenValues: {}
    },
    `should give the expected default tw.q object`
  );
  binsEditor.tw.q = { mode: "continuous" };
  test.deepEqual(
    binsEditor.getDefaultQ(),
    {
      mode: "discrete",
      type: "custom-bin",
      lst: [
        {
          startunbounded: true,
          startinclusive: false,
          stopinclusive: false,
          stop: 2355,
          label: "<2355",
          range: '<span style="font-family:Times;font-style:italic;">x</span> <2355'
        },
        {
          stopunbounded: true,
          startinclusive: true,
          stopinclusive: false,
          start: 2355,
          label: "\u22652355",
          range: '<span style="font-family:Times;font-style:italic;">x</span> \u22652355'
        }
      ]
    },
    `should give the expected default tw.q object if the mode is not discrete`
  );
  if (test._ok) destroy();
  test.end();
});
(0, import_tape.default)("initial rendered UI", async (test) => {
  const { editHandler, binsEditor, holder, destroy } = await getNumericHandler();
  await editHandler.showEditMenu(holder);
  test.equal(binsEditor.dom.customBinBoundaryInput?.size(), 1, `should render a textarea input for custom bin entries`);
  const textInputs = binsEditor.dom.inputsDiv.node().querySelectorAll(`input[type='text']`);
  test.equal(textInputs.length, 2, `should render 2 number inputs to match tw.q.lst[]`);
  const ranges = binsEditor.dom.inputsDiv.node().querySelectorAll('div[name="range"]');
  test.equal(ranges[0].innerText, "x <6.35", `should render the expected first bin range`);
  test.equal(ranges[1].innerText, "x \u22656.35", `should render the expected last bin range`);
  const binInputs = binsEditor.dom.inputsDiv.node().querySelectorAll("input");
  test.equal(binInputs[0].value, "<6.35", `should render the expected first bin input value`);
  test.equal(binInputs[1].value, "\u22656.35", `should render the expected last bin input value`);
  if (test._ok) destroy();
  test.end();
});
(0, import_tape.default)("edit interactivity", async (test) => {
  const { editHandler, binsEditor, holder, destroy } = await getNumericHandler();
  await editHandler.showEditMenu(holder);
  const newKnots = [10, 200, 600, 3e3];
  binsEditor.dom.customBinBoundaryInput.property("value", newKnots.join("\n"));
  binsEditor.dom.customBinBoundaryInput.node().dispatchEvent(new Event("change", { bubbles: true }));
  const lines = [...editHandler.handler.density.dom.binsize_g.node().querySelectorAll("line")];
  test.deepEqual(
    lines.map((l) => l.getAttribute("x1")),
    ["11", "31", "74", "328"],
    `should re-render the draggable lines in the updated x-positions`
  );
  const input0 = binsEditor.dom.customBinLabelInput.node();
  input0.value = "- TEST -";
  input0.dispatchEvent(new Event("change", { bubbles: true }));
  test.deepEqual(
    binsEditor.getEditedQ(false),
    {
      mode: "discrete",
      type: "custom-bin",
      lst: [
        {
          startunbounded: true,
          stop: 10,
          startinclusive: false,
          stopinclusive: true,
          label: "- TEST -",
          range: '<span style="font-family:Times;font-style:italic;">x</span> \u226410'
        },
        {
          start: 10,
          startinclusive: false,
          stopinclusive: true,
          stop: 200,
          label: ">10 to 200",
          range: '10 < <span style="font-family:Times;font-style:italic;">x</span> \u2264 200'
        },
        {
          start: 200,
          startinclusive: false,
          stopinclusive: true,
          stop: 600,
          label: ">200 to 600",
          range: '200 < <span style="font-family:Times;font-style:italic;">x</span> \u2264 600'
        },
        {
          start: 600,
          startinclusive: false,
          stopinclusive: true,
          stop: 3e3,
          label: ">600 to 3000",
          range: '600 < <span style="font-family:Times;font-style:italic;">x</span> \u2264 3000'
        },
        {
          start: 3e3,
          startinclusive: false,
          stopinclusive: false,
          stopunbounded: true,
          label: ">3000",
          range: '<span style="font-family:Times;font-style:italic;">x</span> >3000'
        }
      ]
    },
    `should give the expected edited q object`
  );
  const inputsDiv = binsEditor.dom.inputsDiv.node();
  await binsEditor.render(editHandler.dom.binsDiv.node().firstChild);
  test.equal(
    inputsDiv,
    binsEditor.dom.inputsDiv.node(),
    `should not re-render the menu when the holder has not changed`
  );
  editHandler.undoEdits();
  {
    const lines2 = [...editHandler.handler.density.dom.binsize_g.node().querySelectorAll("line")];
    test.equal(lines2.length, 1, `should render 1 draggable line in the density plot`);
    test.deepEqual(
      lines2.map((l) => l.getAttribute("x1")),
      ["11"],
      `should render the draggable lines in the expected x-positions`
    );
  }
  if (test._ok) destroy();
  test.end();
});
//# sourceMappingURL=NumCustomBinEditor.unit.spec-YHAUSGPN.js.map
