import { __name as l, log as y } from "./chunk-AGHRB4JF.js";
import P from "../../../../../../cytoscape@3.33.1/node_modules/cytoscape/dist/cytoscape.esm.js";
import q from "../../../../../../../../_virtual/cytoscape-cose-bilkent.js";
import "../../../../../../d3-transition@3.0.1_d3-selection@3.0.0/node_modules/d3-transition/src/selection/index.js";
import "../../../../../../d3-zoom@3.0.0/node_modules/d3-zoom/src/transform.js";
import O from "../../../../../../d3-selection@3.0.0/node_modules/d3-selection/src/select.js";
P.use(q);
function D(t, o) {
  t.forEach((e) => {
    const r = {
      id: e.id,
      labelText: e.label,
      height: e.height,
      width: e.width,
      padding: e.padding ?? 0
    };
    Object.keys(e).forEach((i) => {
      ["id", "label", "height", "width", "padding", "x", "y"].includes(i) || (r[i] = e[i]);
    }), o.add({
      group: "nodes",
      data: r,
      position: {
        x: e.x ?? 0,
        y: e.y ?? 0
      }
    });
  });
}
l(D, "addNodes");
function I(t, o) {
  t.forEach((e) => {
    const r = {
      id: e.id,
      source: e.start,
      target: e.end
    };
    Object.keys(e).forEach((i) => {
      ["id", "start", "end"].includes(i) || (r[i] = e[i]);
    }), o.add({
      group: "edges",
      data: r
    });
  });
}
l(I, "addEdges");
function $(t) {
  return new Promise((o) => {
    const e = O("body").append("div").attr("id", "cy").attr("style", "display:none"), r = P({
      container: document.getElementById("cy"),
      // container to render in
      style: [
        {
          selector: "edge",
          style: {
            "curve-style": "bezier"
          }
        }
      ]
    });
    e.remove(), D(t.nodes, r), I(t.edges, r), r.nodes().forEach(function(a) {
      a.layoutDimensions = () => {
        const m = a.data();
        return { w: m.width, h: m.height };
      };
    });
    const i = {
      name: "cose-bilkent",
      // @ts-ignore Types for cose-bilkent are not correct?
      quality: "proof",
      styleEnabled: !1,
      animate: !1
    };
    r.layout(i).run(), r.ready((a) => {
      y.info("Cytoscape ready", a), o(r);
    });
  });
}
l($, "createCytoscapeInstance");
function X(t) {
  return t.nodes().map((o) => {
    const e = o.data(), r = o.position(), i = {
      id: e.id,
      x: r.x,
      y: r.y
    };
    return Object.keys(e).forEach((a) => {
      a !== "id" && (i[a] = e[a]);
    }), i;
  });
}
l(X, "extractPositionedNodes");
function Y(t) {
  return t.edges().map((o) => {
    const e = o.data(), r = o._private.rscratch, i = {
      id: e.id,
      source: e.source,
      target: e.target,
      startX: r.startX,
      startY: r.startY,
      midX: r.midX,
      midY: r.midY,
      endX: r.endX,
      endY: r.endY
    };
    return Object.keys(e).forEach((a) => {
      ["id", "source", "target"].includes(a) || (i[a] = e[a]);
    }), i;
  });
}
l(Y, "extractPositionedEdges");
async function v(t, o) {
  y.debug("Starting cose-bilkent layout algorithm");
  try {
    B(t);
    const e = await $(t), r = X(e), i = Y(e);
    return y.debug(`Layout completed: ${r.length} nodes, ${i.length} edges`), {
      nodes: r,
      edges: i
    };
  } catch (e) {
    throw y.error("Error in cose-bilkent layout algorithm:", e), e;
  }
}
l(v, "executeCoseBilkentLayout");
function B(t) {
  if (!t)
    throw new Error("Layout data is required");
  if (!t.config)
    throw new Error("Configuration is required in layout data");
  if (!t.rootNode)
    throw new Error("Root node is required");
  if (!t.nodes || !Array.isArray(t.nodes))
    throw new Error("No nodes found in layout data");
  if (!Array.isArray(t.edges))
    throw new Error("Edges array is required in layout data");
  return !0;
}
l(B, "validateLayoutData");
var _ = /* @__PURE__ */ l(async (t, o, {
  insertCluster: e,
  insertEdge: r,
  insertEdgeLabel: i,
  insertMarkers: a,
  insertNode: m,
  log: c,
  positionEdgeLabel: x
}, { algorithm: j }) => {
  const u = {}, p = {}, f = o.select("g");
  a(f, t.markers, t.type, t.diagramId);
  const C = f.insert("g").attr("class", "subgraphs"), E = f.insert("g").attr("class", "edgePaths"), A = f.insert("g").attr("class", "edgeLabels"), N = f.insert("g").attr("class", "nodes");
  c.debug("Inserting nodes into DOM for dimension calculation"), await Promise.all(
    t.nodes.map(async (s) => {
      if (s.isGroup) {
        const n = { ...s };
        p[s.id] = n, u[s.id] = n, await e(C, s);
      } else {
        const n = { ...s };
        u[s.id] = n;
        const d = await m(N, s, {
          config: t.config,
          dir: t.direction || "TB"
        }), g = d.node().getBBox();
        n.width = g.width, n.height = g.height, n.domId = d, c.debug(`Node ${s.id} dimensions: ${g.width}x${g.height}`);
      }
    })
  ), c.debug("Running cose-bilkent layout algorithm");
  const k = {
    ...t,
    nodes: t.nodes.map((s) => {
      const n = u[s.id];
      return {
        ...s,
        width: n.width,
        height: n.height
      };
    })
  }, b = await v(k, t.config);
  c.debug("Positioning nodes based on layout results"), b.nodes.forEach((s) => {
    const n = u[s.id];
    n != null && n.domId && (n.domId.attr(
      "transform",
      `translate(${s.x}, ${s.y})`
    ), n.x = s.x, n.y = s.y, c.debug(`Positioned node ${n.id} at center (${s.x}, ${s.y})`));
  }), b.edges.forEach((s) => {
    const n = t.edges.find((d) => d.id === s.id);
    n && (n.points = [
      { x: s.startX, y: s.startY },
      { x: s.midX, y: s.midY },
      { x: s.endX, y: s.endY }
    ]);
  }), c.debug("Inserting and positioning edges"), await Promise.all(
    t.edges.map(async (s) => {
      await i(A, s);
      const n = u[s.start ?? ""], d = u[s.end ?? ""];
      if (n && d) {
        const g = b.edges.find((h) => h.id === s.id);
        if (g) {
          c.debug("APA01 positionedEdge", g);
          const h = { ...s }, w = r(
            E,
            h,
            p,
            t.type,
            n,
            d,
            t.diagramId
          );
          x(h, w);
        } else {
          const h = {
            ...s,
            points: [
              { x: n.x || 0, y: n.y || 0 },
              { x: d.x || 0, y: d.y || 0 }
            ]
          }, w = r(
            E,
            h,
            p,
            t.type,
            n,
            d,
            t.diagramId
          );
          x(h, w);
        }
      }
    })
  ), c.debug("Cose-bilkent rendering completed");
}, "render"), S = _;
export {
  S as render
};
