import {
  agedx
} from "./chunk-32AI7I4C.js";
import {
  NumericDensity
} from "./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/NumericDensity.unit.spec.ts
var import_tape = __toESM(require_tape(), 1);
async function getNumericDensity(opts = {}) {
  const term = JSON.parse(JSON.stringify(termjson.agedx));
  const rawTw = {
    term,
    q: {
      mode: "continuous"
    }
  };
  const tw = await TwRouter.initRaw(rawTw);
  const density = new NumericDensity({
    termsetting: {
      tw,
      term: rawTw.term,
      q: rawTw.q,
      opts: {
        // numericEditMenuVersion: ['continuous', 'discrete', 'binary', 'spline'],
        // usecase: opts.usecase
        ...opts
      },
      api: {
        runCallback() {
        }
      },
      dom: {
        tip: {
          hide() {
          }
        }
      },
      vocabApi: {
        getViolinBox() {
          return agedx;
        }
      }
    }
  });
  const holder = select_default("body").append("div").style("position", "fixed").style("top", 500).style("left", 500).style("width", "fit-content").style("margin", "20px").style("padding", "5px").style("border", "1px solid #000");
  return {
    rawTw,
    tw,
    holder,
    density,
    destroy: () => {
      density.destroy();
      holder.remove();
    }
  };
}
function simulateDrag(elem, xOffset) {
  const box = elem.getBoundingClientRect();
  const draggedX = box.x + xOffset;
  elem.dispatchEvent(
    new MouseEvent("mousedown", {
      bubbles: true,
      cancelable: true,
      clientX: box.x,
      clientY: box.y,
      view: window
    })
  );
  elem.dispatchEvent(
    new MouseEvent("mouseup", {
      bubbles: true,
      cancelable: true,
      clientX: draggedX,
      clientY: 150,
      view: window
    })
  );
}
(0, import_tape.default)("violin as density plot", async (test) => {
  const { density, holder, destroy } = await getNumericDensity();
  await density.showViolin(holder);
  test.equal(holder.selectAll("svg").size(), 1, `should render a density plot svg`);
  test.equal(holder.selectAll("svg image").size(), 1, `should render an image inside the svg`);
  if (test._ok) destroy();
  test.end();
});
(0, import_tape.default)("1 draggable line without lastVisibleLine", async (test) => {
  test.timeoutAfter(20);
  test.plan(4);
  const { density, holder, destroy } = await getNumericDensity();
  await density.showViolin(holder);
  let hasTestedCallback = false;
  const values = [
    { x: 100, isDraggable: true, movesWithLineIndex: -1 },
    { x: 1e3, movesWithLineIndex: 0 },
    { x: 3e3, movesWithLineIndex: 0 }
  ];
  density.setBinLines({
    values,
    callback(lineData, value) {
      if (hasTestedCallback === true) return;
      hasTestedCallback = true;
      const comparableKeys = Object.keys(values[0]);
      test.deepEqual(
        Object.fromEntries(Object.entries(lineData).filter((kv) => comparableKeys.includes(kv[0]))),
        values[0],
        `should provide a copy of the draggable line data as the first argument to callback`
      );
      test.true((value - 1516.62) / value < 0.01, `should provide the value as the second argument to callback`);
      test.deepEqual(
        lines.filter((l) => l.style.display !== "none").map((l, i) => Math.abs(Math.floor(Number(l.getAttribute("x1")) - x1b4drag[i] - xOffset)) < 2),
        // check that the difference between expected and actual x values is less than a few pixels
        [true, true, true],
        `should also move non-draggable lines in the expected x-positions of the density plot`
      );
      if (test._ok) destroy();
      test.end();
    }
  });
  const lines = [...density.dom.binsize_g.node().querySelectorAll("line")];
  const x1b4drag = [21, 116, 328];
  test.deepEqual(
    lines.filter((l) => l.style.display !== "none").map((l) => Number(l.getAttribute("x1"))),
    x1b4drag,
    `should render 3 visible lines in the expected x-positions of the density plot`
  );
  const xOffset = 150;
  simulateDrag(lines[0], xOffset);
});
(0, import_tape.default)("1 draggable line with lastVisibleLine", async (test) => {
  test.timeoutAfter(20);
  test.plan(4);
  const { density, holder, destroy } = await getNumericDensity();
  await density.showViolin(holder);
  let hasTestedCallback = false;
  const values = [
    { x: 100, isDraggable: true, movesWithLineIndex: -1 },
    { x: 500, movesWithLineIndex: 0 },
    { x: 1e3, movesWithLineIndex: 0 },
    { x: 2e3, isDraggable: true, isLastVisibleLine: true }
  ];
  density.setBinLines({
    values,
    callback(lineData, value) {
      if (hasTestedCallback === true) return;
      hasTestedCallback = true;
      const comparableKeys = Object.keys(values[0]);
      test.deepEqual(
        Object.fromEntries(Object.entries(lineData).filter((kv) => comparableKeys.includes(kv[0]))),
        values[0],
        `should provide a copy of the draggable line data as the first argument to callback`
      );
      test.true((value - 1516.62) / value < 0.01, `should provide the value as the second argument to callback`);
      const expectedXDiff = [0, 0, 44];
      test.deepEqual(
        lines.filter((l) => l.style.display !== "none").map(
          (l, i) => Math.abs(Math.floor(Number(l.getAttribute("x1")) - x1b4drag[i] - xOffset) + expectedXDiff[i]) < 2
        ),
        // check that the difference between expected and actual x values is less than a few pixels
        [true, true, true],
        `should also move non-draggable lines in the expected x-positions of the density plot`
      );
      if (test._ok) destroy();
      test.end();
    }
  });
  const lines = [...density.dom.binsize_g.node().querySelectorAll("line")];
  const x1b4drag = [21, 63, 116, 222];
  test.deepEqual(
    lines.filter((l) => l.style.display !== "none").map((l) => Number(l.getAttribute("x1"))),
    x1b4drag,
    `should render 3 visible lines in the expected x-positions of the density plot`
  );
  const xOffset = 150;
  simulateDrag(lines[0], xOffset);
});
//# sourceMappingURL=NumericDensity.unit.spec-N45PCLOT.js.map
