import { j as t, e as P } from "./externalizeComponent-Dc3fViZA.js";
import * as x from "three";
import { Matrix4 as w, Vector3 as L, Quaternion as z } from "three";
import { ConvexGeometry as te } from "three-stdlib";
import { useThree as ne, useFrame as re, Canvas as H } from "@react-three/fiber";
import { Environment as Re, Lightformer as V, Line as U, useGLTF as ve, Bounds as Y } from "@react-three/drei";
import { useTheme as Me, Card as _e, Box as b, Typography as q, Divider as X, Button as K } from "@mui/material";
import { observer as Te } from "mobx-react-lite";
import Le, { useRef as R, useEffect as T, useCallback as _, useState as G, Suspense as oe, useMemo as Ve } from "react";
import { useTranslation as Ge } from "react-i18next";
import { N as ke, V as se, c as ie, b as Z, M as v } from "./interpolation-6ZZN4S_x.js";
import { ErrorBoundary as ae } from "react-error-boundary";
function Fe(e) {
  switch (e.shape_type) {
    case "convex_hull":
      return new te(
        e.vertices.map(
          (n) => new x.Vector3(
            n[0] / 1e3,
            n[1] / 1e3,
            n[2] / 1e3
          )
        )
      );
    case "box":
      return new x.BoxGeometry(
        e.size_x / 1e3,
        e.size_y / 1e3,
        e.size_z / 1e3
      );
    case "sphere":
      return new x.SphereGeometry(e.radius / 1e3);
    case "capsule":
      return new x.CapsuleGeometry(
        e.radius / 1e3,
        e.cylinder_height / 1e3
      );
    case "cylinder":
      return new x.CylinderGeometry(
        e.radius / 1e3,
        e.radius / 1e3,
        e.height / 1e3
      );
    case "rectangle":
      return new x.BoxGeometry(e.size_x / 1e3, e.size_y / 1e3, 0);
    default:
      return console.warn(`${e.shape_type} is not supported`), new x.BufferGeometry();
  }
}
function Ie({
  name: e,
  collider: r,
  children: n
}) {
  var u, l;
  const i = ((u = r.pose) == null ? void 0 : u.position) ?? [0, 0, 0], o = ((l = r.pose) == null ? void 0 : l.orientation) ?? [0, 0, 0];
  return r.margin && console.warn(`${e} margin is not supported`), /* @__PURE__ */ t.jsx(
    "mesh",
    {
      name: e,
      position: new x.Vector3(
        i[0],
        i[1],
        i[2]
      ).divideScalar(1e3),
      rotation: new x.Euler(o[0], o[1], o[2], "XYZ"),
      geometry: Fe(r.shape),
      children: n
    }
  );
}
function Se({
  name: e,
  colliders: r,
  meshChildrenProvider: n,
  ...i
}) {
  return /* @__PURE__ */ t.jsx("group", { name: e, ...i, children: Object.entries(r).map(([o, u]) => /* @__PURE__ */ t.jsx(
    Ie,
    {
      name: o,
      collider: u,
      children: n(o, u)
    },
    o
  )) });
}
function rt({
  scene: e,
  meshChildrenProvider: r
}) {
  const n = e.colliders;
  return /* @__PURE__ */ t.jsx("group", { children: n && /* @__PURE__ */ t.jsx(
    Se,
    {
      meshChildrenProvider: r,
      colliders: n
    }
  ) });
}
function Q() {
  return /* @__PURE__ */ t.jsx(Re, { children: /* @__PURE__ */ t.jsx(Ae, {}) });
}
function Ae({ positions: e = [2, 0, 2, 0, 2, 0, 2, 0] }) {
  return /* @__PURE__ */ t.jsxs(t.Fragment, { children: [
    /* @__PURE__ */ t.jsx(
      V,
      {
        intensity: 5,
        "rotation-x": Math.PI / 2,
        position: [0, 5, -9],
        scale: [10, 10, 1]
      }
    ),
    /* @__PURE__ */ t.jsx("group", { rotation: [0, 0.5, 0], children: /* @__PURE__ */ t.jsx("group", { children: e.map((r, n) => /* @__PURE__ */ t.jsx(
      V,
      {
        form: "circle",
        intensity: 5,
        rotation: [Math.PI / 2, 0, 0],
        position: [r, 4, n * 4],
        scale: [3, 1, 1]
      },
      n
    )) }) }),
    /* @__PURE__ */ t.jsx(
      V,
      {
        intensity: 40,
        "rotation-y": Math.PI / 2,
        position: [-5, 1, -1],
        scale: [20, 0.1, 1]
      }
    ),
    /* @__PURE__ */ t.jsx(
      V,
      {
        intensity: 20,
        "rotation-y": -Math.PI,
        position: [-5, -2, -1],
        scale: [20, 0.1, 1]
      }
    ),
    /* @__PURE__ */ t.jsx(
      V,
      {
        "rotation-y": Math.PI / 2,
        position: [-5, -1, -1],
        scale: [20, 0.5, 1],
        intensity: 5
      }
    ),
    /* @__PURE__ */ t.jsx(
      V,
      {
        "rotation-y": -Math.PI / 2,
        position: [10, 1, 0],
        scale: [20, 1, 1],
        intensity: 10
      }
    ),
    /* @__PURE__ */ t.jsx(
      V,
      {
        form: "ring",
        color: "white",
        intensity: 5,
        scale: 10,
        position: [-15, 4, -18],
        target: [0, 0, 0]
      }
    )
  ] });
}
function Pe(e) {
  if (e.length < 3)
    return console.log("Not enough vertices to define a plane"), { isCoplanar: !1 };
  const r = new x.Vector3(e[0].x, e[0].y, e[0].z), n = new x.Vector3(e[1].x, e[1].y, e[1].z), i = new x.Vector3(e[2].x, e[2].y, e[2].z), o = new x.Vector3().subVectors(n, r), u = new x.Vector3().subVectors(i, r), l = new x.Vector3().crossVectors(o, u).normalize();
  for (let f = 3; f < e.length; f++) {
    const c = new x.Vector3(e[f].x, e[f].y, e[f].z), d = new x.Vector3().subVectors(c, r), h = l.dot(d);
    if (Math.abs(h) > 1e-6)
      return console.log("Vertices are not on the same plane"), { isCoplanar: !1 };
  }
  return { isCoplanar: !0, normal: l };
}
function ot({
  safetyZones: e,
  ...r
}) {
  return /* @__PURE__ */ t.jsx("group", { ...r, children: e.map((n, i) => {
    let o = [];
    return n.geometry && (n.geometry.compound ? o = n.geometry.compound.child_geometries : n.geometry.convex_hull && (o = [n.geometry])), o.map((u, l) => {
      if (!u.convex_hull) return null;
      const f = u.convex_hull.vertices.map(
        (h) => new x.Vector3(h.x / 1e3, h.y / 1e3, h.z / 1e3)
      ), c = Pe(f);
      if (c.isCoplanar && c.normal) {
        const s = new x.Vector3().addVectors(
          f[0],
          c.normal.multiplyScalar(1e-4)
        );
        f.push(s);
      }
      let d;
      try {
        d = new te(f);
      } catch (h) {
        return console.log("Error creating ConvexGeometry:", h), null;
      }
      return /* @__PURE__ */ t.jsx("mesh", { geometry: d, children: /* @__PURE__ */ t.jsx(
        "meshStandardMaterial",
        {
          attach: "material",
          color: "#009f4d",
          opacity: 0.2,
          depthTest: !1,
          depthWrite: !1,
          transparent: !0,
          polygonOffset: !0,
          polygonOffsetFactor: -l
        },
        i
      ) }, `${i}-${l}`);
    });
  }) });
}
function st({
  trajectory: e,
  ...r
}) {
  var i;
  const n = ((i = e.trajectory) == null ? void 0 : i.map((o) => o.tcp_pose ? new x.Vector3(
    o.tcp_pose.position.x / 1e3,
    o.tcp_pose.position.z / 1e3,
    -o.tcp_pose.position.y / 1e3
  ) : null).filter((o) => o !== null)) || [];
  return /* @__PURE__ */ t.jsx("group", { ...r, children: n.length > 0 && /* @__PURE__ */ t.jsx(
    U,
    {
      points: n,
      lineWidth: 3,
      polygonOffset: !0,
      polygonOffsetFactor: 10,
      polygonOffsetUnits: 10
    }
  ) });
}
const O = /* @__PURE__ */ new Map();
async function C(e, r) {
  if (O.has(e))
    return O.get(e);
  const n = (async () => {
    var l;
    const i = r || "", o = new ke({ instanceUrl: i }), u = o.api.motionGroupModels;
    (l = u.axios) != null && l.interceptors && u.axios.interceptors.request.use((f) => {
      var c;
      return (c = f.url) != null && c.includes("/glb") && (f.responseType = "blob"), f;
    });
    try {
      const f = await o.api.motionGroupModels.getMotionGroupGlbModel(e);
      return URL.createObjectURL(f);
    } catch (f) {
      throw console.error("Failed to fetch model:", f), f;
    }
  })();
  return O.set(e, n), n;
}
function le(e) {
  function r(n) {
    return n.children.length === 0 ? [n] : [n, ...n.children.flatMap((i) => r(i))];
  }
  return r(e).filter((n) => ue(n));
}
function ce(e) {
  return e.name.endsWith("_FLG");
}
function ue(e) {
  return /_J[0-9]+$/.test(e.name);
}
function Ce(e, r) {
  let n;
  function i(o) {
    if (ce(o)) {
      if (n)
        throw Error(
          `Found multiple flange groups in robot model ${r}; first ${n.name} then ${o.name}. Only one _FLG group is allowed.`
        );
      n = o;
    }
    ue(o), o.children.map(i);
  }
  if (i(e.scene), !n)
    throw Error(
      `No flange group found in robot model ${r}. Flange must be identified with a name ending in _FLG.`
    );
  return { gltf: e };
}
function fe({
  rapidlyChangingMotionState: e,
  dhParameters: r,
  onRotationChanged: n,
  children: i
}) {
  const o = R([]), u = R([]), l = R(null), { invalidate: f } = ne();
  T(() => {
    const s = e.joint_position.filter(
      (a) => a !== void 0
    );
    return l.current = new se(s, {
      tension: 120,
      // Controls spring stiffness - higher values create faster, more responsive motion
      friction: 20,
      // Controls damping - higher values reduce oscillation and create smoother settling
      threshold: 1e-3
    }), () => {
      var a;
      (a = l.current) == null || a.destroy();
    };
  }, []), re((s, a) => {
    if (l.current) {
      const m = l.current.update(a);
      d(), m || f();
    }
  });
  function c(s) {
    s && (u.current = le(s), d(), f());
  }
  function d() {
    var a;
    const s = ((a = l.current) == null ? void 0 : a.getCurrentValues()) || [];
    if (n)
      n(u.current, s);
    else
      for (const [m, g] of u.current.entries()) {
        const p = r[m], j = p.theta || 0, y = p.reverse_rotation_direction ? -1 : 1;
        g.rotation.y = y * (s[m] || 0) + j;
      }
  }
  const h = _(() => {
    const s = e.joint_position.filter(
      (a) => a !== void 0
    );
    requestAnimationFrame(() => {
      var a;
      o.current = s, (a = l.current) == null || a.setTarget(s);
    });
  }, [e]);
  return T(() => {
    h();
  }, [e, h]), ie(() => {
    h();
  }), /* @__PURE__ */ t.jsx("group", { ref: c, children: i });
}
const De = "line", Oe = "mesh";
function ze({
  rapidlyChangingMotionState: e,
  dhParameters: r,
  ...n
}) {
  const i = new w(), o = R([]), u = R([]);
  Le.useEffect(() => {
    o.current = new Array(r.length).fill(null), u.current = new Array(r.length).fill(null);
  }, [r.length]);
  function l(d, h) {
    const s = new L(), a = new z(), m = new L();
    i.decompose(s, a, m);
    const g = s.clone(), p = new w().makeRotationY(
      d.theta + h * (d.reverse_rotation_direction ? -1 : 1)
    ).multiply(new w().makeTranslation(0, d.d / 1e3, 0)).multiply(new w().makeTranslation(d.a / 1e3, 0, 0)).multiply(new w().makeRotationX(d.alpha));
    return i.multiply(p), i.decompose(s, a, m), { a: g, b: s };
  }
  function f(d, h, s, a) {
    if (!r)
      return;
    const m = r[d];
    if (!m)
      return;
    const { a: g, b: p } = l(m, a);
    h.geometry.setPositions([g.toArray(), p.toArray()].flat()), s.position.set(p.x, p.y, p.z);
  }
  function c(d, h) {
    i.identity();
    for (let s = 0; s < Math.min(d.length, h.length); s++) {
      const a = o.current[s], m = u.current[s];
      a && m && f(s, a, m, h[s]);
    }
  }
  return /* @__PURE__ */ t.jsx(t.Fragment, { children: /* @__PURE__ */ t.jsx(
    fe,
    {
      rapidlyChangingMotionState: e,
      dhParameters: r,
      onRotationChanged: c,
      children: /* @__PURE__ */ t.jsxs("group", { ...n, name: "Scene", children: [
        /* @__PURE__ */ t.jsxs("mesh", { children: [
          /* @__PURE__ */ t.jsx("sphereGeometry", { args: [0.01, 32, 32] }),
          /* @__PURE__ */ t.jsx("meshStandardMaterial", { color: "black", depthTest: !0 })
        ] }),
        r.map((d, h) => {
          const { a: s, b: a } = l(
            d,
            e.joint_position[h] ?? 0
          ), m = `dhrobot_J0${h}`;
          return /* @__PURE__ */ t.jsxs("group", { name: m, children: [
            /* @__PURE__ */ t.jsx(
              U,
              {
                ref: (g) => {
                  o.current[h] = g;
                },
                name: De,
                points: [s, a],
                color: "white",
                lineWidth: 5
              }
            ),
            /* @__PURE__ */ t.jsxs(
              "mesh",
              {
                ref: (g) => {
                  u.current[h] = g;
                },
                name: Oe,
                position: a,
                children: [
                  /* @__PURE__ */ t.jsx("sphereGeometry", { args: [0.01, 32, 32] }),
                  /* @__PURE__ */ t.jsx("meshStandardMaterial", { color: "black", depthTest: !0 })
                ]
              },
              "mesh_" + h
            )
          ] }, m);
        })
      ] })
    }
  ) });
}
const Ue = console.warn;
function de() {
  return T(() => {
    console.warn = (e) => {
      e !== "Cannot call the manual advancement of rafz whilst frameLoop is not set as demand" && Ue(e);
    };
  }, []), /* @__PURE__ */ t.jsx(t.Fragment, {});
}
function Ee(e) {
  return e.type === "Mesh";
}
function Be({
  url: e,
  flangeRef: r,
  postModelRender: n,
  ...i
}) {
  const o = ve(e);
  let u;
  try {
    u = Ce(o, "robot.glb").gltf;
  } catch (c) {
    throw c;
  }
  const l = _(
    (c) => {
      c && n && n();
    },
    [n]
  );
  function f(c) {
    try {
      return Ee(c) ? c.geometry ? /* @__PURE__ */ t.jsx(
        "mesh",
        {
          name: c.name,
          geometry: c.geometry,
          material: c.material,
          position: c.position,
          rotation: c.rotation
        },
        c.uuid
      ) : /* @__PURE__ */ t.jsx("group", { name: c.name, position: c.position, rotation: c.rotation }, c.uuid) : /* @__PURE__ */ t.jsx(
        "group",
        {
          name: c.name,
          position: c.position,
          rotation: c.rotation,
          ref: ce(c) ? r : void 0,
          children: c.children.map(f)
        },
        c.uuid
      );
    } catch (d) {
      return console.warn("Error rendering node", c.name, d), null;
    }
  }
  return /* @__PURE__ */ t.jsx("group", { ...i, dispose: null, ref: l, children: f(u.scene) });
}
function me({
  modelURL: e,
  flangeRef: r,
  postModelRender: n,
  ...i
}) {
  const [o, u] = G(null);
  return T(() => {
    (async () => {
      try {
        if (typeof e == "string")
          u(e);
        else {
          const f = await e;
          u(f);
        }
      } catch (f) {
        console.error("Failed to resolve model URL:", f);
      }
    })();
  }, [e]), o ? /* @__PURE__ */ t.jsx(
    Be,
    {
      url: o,
      flangeRef: r,
      postModelRender: n,
      ...i
    }
  ) : null;
}
const he = (e, r) => {
  e.userData.isGhost || (e.traverse((n) => {
    if (n instanceof x.Mesh) {
      n.material instanceof x.Material && (n.material.colorWrite = !1);
      const i = n.clone(), o = n.clone();
      i.material = new x.MeshStandardMaterial({
        depthTest: !0,
        depthWrite: !0,
        colorWrite: !1,
        polygonOffset: !0,
        polygonOffsetFactor: -1,
        side: x.DoubleSide
      }), i.userData.isGhost = !0, o.material = new x.MeshStandardMaterial({
        color: r,
        opacity: 0.3,
        depthTest: !0,
        depthWrite: !1,
        transparent: !0,
        polygonOffset: !0,
        polygonOffsetFactor: -2,
        side: x.DoubleSide
      }), o.userData.isGhost = !0, n.parent && (n.parent.add(i), n.parent.add(o));
    }
  }), e.userData.isGhost = !0);
}, pe = (e) => {
  if (!e.userData.isGhost) return;
  const r = [];
  e.traverse((n) => {
    var i;
    n instanceof x.Mesh && ((i = n.userData) != null && i.isGhost ? r.push(n) : n.material instanceof x.Material && (n.material.colorWrite = !0));
  }), r.forEach((n) => {
    n.parent && n.parent.remove(n);
  }), e.userData.isGhost = !1;
}, xe = P(
  ({
    rapidlyChangingMotionState: e,
    modelFromController: r,
    dhParameters: n,
    getModel: i = C,
    flangeRef: o,
    postModelRender: u,
    transparentColor: l,
    instanceUrl: f,
    ...c
  }) => {
    const [d, h] = G(null), s = _((m) => {
      h(m);
    }, []);
    T(() => {
      d && (l ? he(d, l) : pe(d));
    }, [d, l]);
    const a = /* @__PURE__ */ t.jsx(
      ze,
      {
        rapidlyChangingMotionState: e,
        dhParameters: n,
        ...c
      }
    );
    return /* @__PURE__ */ t.jsxs(
      ae,
      {
        fallback: a,
        onError: (m) => {
          console.warn(m);
        },
        children: [
          /* @__PURE__ */ t.jsx(oe, { fallback: a, children: /* @__PURE__ */ t.jsx("group", { ref: s, children: /* @__PURE__ */ t.jsx(
            fe,
            {
              rapidlyChangingMotionState: e,
              dhParameters: n,
              children: /* @__PURE__ */ t.jsx(
                me,
                {
                  modelURL: (() => {
                    const m = i(r, f);
                    if (!m) {
                      const g = new Blob([], { type: "model/gltf-binary" }), p = new File([g], `${r}.glb`, { type: "model/gltf-binary" });
                      return Promise.resolve(URL.createObjectURL(p));
                    }
                    return m;
                  })(),
                  postModelRender: u,
                  flangeRef: o,
                  ...c
                }
              )
            }
          ) }) }),
          /* @__PURE__ */ t.jsx(de, {})
        ]
      }
    );
  }
);
function We({
  connectedMotionGroup: e,
  getModel: r = C,
  flangeRef: n,
  transparentColor: i,
  postModelRender: o,
  ...u
}) {
  return e.dhParameters ? /* @__PURE__ */ t.jsx(
    xe,
    {
      rapidlyChangingMotionState: e.rapidlyChangingMotionState,
      modelFromController: e.modelFromController || "",
      dhParameters: e.dhParameters,
      getModel: r,
      flangeRef: n,
      transparentColor: i,
      postModelRender: o,
      ...u
    }
  ) : null;
}
const it = P(
  Te(
    ({
      robotName: e,
      programState: r,
      safetyState: n,
      operationMode: i,
      driveToHomeEnabled: o = !1,
      onDriveToHomePress: u,
      onDriveToHomeRelease: l,
      connectedMotionGroup: f,
      robotComponent: c = We,
      customContentComponent: d,
      className: h
    }) => {
      var W;
      const s = Me(), { t: a } = Ge(), [m, g] = G(!1), p = R(null), j = R(null), [y, D] = G(!1), [k, we] = G({ width: 400, height: 600 }), [$e, be] = G(0);
      T(() => {
        const A = () => {
          if (j.current) {
            const { offsetWidth: $, offsetHeight: N } = j.current;
            D($ > N), we({ width: $, height: N });
          }
        };
        A();
        const J = new ResizeObserver(A);
        return j.current && J.observe(j.current), () => {
          J.disconnect();
        };
      }, []);
      const E = _(() => {
        be((A) => A + 1);
      }, []), F = _(() => {
        !o || !u || (g(!0), u());
      }, [o, u]), I = _(() => {
        !o || !l || (g(!1), l());
      }, [o, l]), B = _(() => {
        m && l && (g(!1), l());
      }, [m, l]), M = y ? k.width < 350 : k.height < 200, S = y ? k.height < 310 : k.height < 450;
      return /* @__PURE__ */ t.jsx(
        _e,
        {
          ref: j,
          className: h,
          sx: {
            width: "100%",
            height: "100%",
            display: "flex",
            flexDirection: y ? "row" : "column",
            position: "relative",
            overflow: "hidden",
            minWidth: { xs: 180, sm: 220, md: 250 },
            minHeight: y ? { xs: 200, sm: 240, md: 260 } : { xs: 150, sm: 180, md: 220 },
            // Allow progressive hiding in portrait mode
            border: `1px solid ${s.palette.divider}`,
            borderRadius: "18px",
            boxShadow: "none",
            backgroundColor: ((W = s.palette.backgroundPaperElevation) == null ? void 0 : W[8]) || "#2A2A3F",
            backgroundImage: "none"
            // Override any gradient from elevation
          },
          children: y ? /* @__PURE__ */ t.jsxs(t.Fragment, { children: [
            /* @__PURE__ */ t.jsx(
              b,
              {
                sx: {
                  flex: "0 0 50%",
                  position: "relative",
                  height: "100%",
                  minHeight: "100%",
                  maxHeight: "100%",
                  borderRadius: 1,
                  m: { xs: 1.5, sm: 2, md: 3 },
                  mr: { xs: 0.75, sm: 1, md: 1.5 },
                  overflow: "hidden",
                  // Prevent content from affecting container size
                  display: M ? "none" : "block"
                },
                children: !M && /* @__PURE__ */ t.jsxs(
                  H,
                  {
                    orthographic: !0,
                    camera: {
                      position: [3, 2, 3],
                      zoom: 1
                    },
                    shadows: !0,
                    frameloop: "demand",
                    style: {
                      borderRadius: s.shape.borderRadius,
                      width: "100%",
                      height: "100%",
                      background: "transparent",
                      position: "absolute",
                      top: 0,
                      left: 0
                    },
                    dpr: [1, 2],
                    gl: { alpha: !0, antialias: !0 },
                    children: [
                      /* @__PURE__ */ t.jsx(Q, {}),
                      /* @__PURE__ */ t.jsx(Y, { fit: !0, observe: !0, margin: 1, maxDuration: 1, children: /* @__PURE__ */ t.jsx(
                        c,
                        {
                          connectedMotionGroup: f,
                          postModelRender: E
                        }
                      ) })
                    ]
                  }
                )
              }
            ),
            /* @__PURE__ */ t.jsxs(
              b,
              {
                sx: {
                  flex: "1",
                  display: "flex",
                  flexDirection: "column",
                  justifyContent: "flex-start",
                  width: M ? "100%" : "50%"
                },
                children: [
                  /* @__PURE__ */ t.jsxs(
                    b,
                    {
                      sx: {
                        p: { xs: 1.5, sm: 2, md: 3 },
                        pb: { xs: 1, sm: 1.5, md: 2 },
                        textAlign: "left"
                      },
                      children: [
                        /* @__PURE__ */ t.jsx(q, { variant: "h6", component: "h2", sx: { mb: 1 }, children: e }),
                        /* @__PURE__ */ t.jsx(
                          Z,
                          {
                            programState: r,
                            safetyState: n,
                            operationMode: i
                          }
                        )
                      ]
                    }
                  ),
                  /* @__PURE__ */ t.jsxs(
                    b,
                    {
                      sx: {
                        p: { xs: 1.5, sm: 2, md: 3 },
                        pt: 0,
                        flex: "1",
                        display: "flex",
                        flexDirection: "column",
                        justifyContent: "space-between"
                      },
                      children: [
                        !S && d && /* @__PURE__ */ t.jsxs(b, { children: [
                          /* @__PURE__ */ t.jsx(d, {}),
                          /* @__PURE__ */ t.jsx(
                            X,
                            {
                              sx: {
                                mt: 1,
                                mb: 0,
                                borderColor: s.palette.divider,
                                opacity: 0.5
                              }
                            }
                          )
                        ] }),
                        /* @__PURE__ */ t.jsx(
                          b,
                          {
                            sx: {
                              mt: !S && d ? "auto" : 0
                            },
                            children: /* @__PURE__ */ t.jsx(
                              b,
                              {
                                sx: {
                                  display: "flex",
                                  justifyContent: "flex-start",
                                  mt: { xs: 1, sm: 1.5, md: 2 },
                                  mb: { xs: 0.5, sm: 0.75, md: 1 }
                                },
                                children: /* @__PURE__ */ t.jsx(
                                  K,
                                  {
                                    ref: p,
                                    variant: "contained",
                                    color: "secondary",
                                    size: "small",
                                    disabled: !o,
                                    onMouseDown: F,
                                    onMouseUp: I,
                                    onMouseLeave: B,
                                    onTouchStart: F,
                                    onTouchEnd: I,
                                    sx: {
                                      textTransform: "none",
                                      px: 1.5,
                                      py: 0.5
                                    },
                                    children: a("RobotCard.DriveToHome.bt")
                                  }
                                )
                              }
                            )
                          }
                        )
                      ]
                    }
                  )
                ]
              }
            )
          ] }) : /* @__PURE__ */ t.jsx(t.Fragment, { children: /* @__PURE__ */ t.jsxs(
            b,
            {
              sx: {
                p: 3,
                height: "100%",
                display: "flex",
                flexDirection: "column"
              },
              children: [
                /* @__PURE__ */ t.jsxs(b, { children: [
                  /* @__PURE__ */ t.jsx(q, { variant: "h6", component: "h2", sx: { mb: 1 }, children: e }),
                  /* @__PURE__ */ t.jsx(
                    Z,
                    {
                      programState: r,
                      safetyState: n,
                      operationMode: i
                    }
                  )
                ] }),
                /* @__PURE__ */ t.jsx(
                  b,
                  {
                    sx: {
                      flex: M ? 0 : 1,
                      position: "relative",
                      minHeight: M ? 0 : { xs: 120, sm: 150, md: 200 },
                      height: M ? 0 : "auto",
                      borderRadius: 1,
                      overflow: "hidden",
                      display: M ? "none" : "block"
                    },
                    children: !M && /* @__PURE__ */ t.jsxs(
                      H,
                      {
                        orthographic: !0,
                        camera: {
                          position: [3, 2, 3],
                          zoom: 1
                        },
                        shadows: !0,
                        frameloop: "demand",
                        style: {
                          borderRadius: s.shape.borderRadius,
                          width: "100%",
                          height: "100%",
                          background: "transparent",
                          position: "absolute"
                        },
                        dpr: [1, 2],
                        gl: { alpha: !0, antialias: !0 },
                        children: [
                          /* @__PURE__ */ t.jsx(Q, {}),
                          /* @__PURE__ */ t.jsx(Y, { fit: !0, clip: !0, observe: !0, margin: 1, maxDuration: 1, children: /* @__PURE__ */ t.jsx(
                            c,
                            {
                              connectedMotionGroup: f,
                              postModelRender: E
                            }
                          ) })
                        ]
                      }
                    )
                  }
                ),
                /* @__PURE__ */ t.jsxs(b, { children: [
                  !S && d && /* @__PURE__ */ t.jsxs(t.Fragment, { children: [
                    /* @__PURE__ */ t.jsx(d, {}),
                    /* @__PURE__ */ t.jsx(
                      X,
                      {
                        sx: {
                          mt: 1,
                          mb: 0,
                          borderColor: s.palette.divider,
                          opacity: 0.5
                        }
                      }
                    )
                  ] }),
                  /* @__PURE__ */ t.jsx(
                    b,
                    {
                      sx: {
                        display: "flex",
                        justifyContent: "flex-start",
                        mt: !S && d ? { xs: 1, sm: 2, md: 5 } : { xs: 0.5, sm: 1, md: 2 },
                        mb: { xs: 0.5, sm: 0.75, md: 1 }
                      },
                      children: /* @__PURE__ */ t.jsx(
                        K,
                        {
                          ref: p,
                          variant: "contained",
                          color: "secondary",
                          size: "small",
                          disabled: !o,
                          onMouseDown: F,
                          onMouseUp: I,
                          onMouseLeave: B,
                          onTouchStart: F,
                          onTouchEnd: I,
                          sx: {
                            textTransform: "none",
                            px: 1.5,
                            py: 0.5
                          },
                          children: a("RobotCard.DriveToHome.bt")
                        }
                      )
                    }
                  )
                ] })
              ]
            }
          ) })
        }
      );
    }
  )
), at = Array(6).fill(2 * Math.PI);
function ge({
  rapidlyChangingMotionState: e,
  dhParameters: r,
  onTranslationChanged: n,
  children: i
}) {
  const o = R([]), u = R([]), l = R(null), { invalidate: f } = ne();
  T(() => {
    const s = e.joint_position.filter(
      (a) => a !== void 0
    );
    return l.current = new se(s, {
      tension: 120,
      // Controls spring stiffness - higher values create faster, more responsive motion
      friction: 20,
      // Controls damping - higher values reduce oscillation and create smoother settling
      threshold: 1e-3
    }), () => {
      var a;
      (a = l.current) == null || a.destroy();
    };
  }, []), re((s, a) => {
    if (l.current) {
      const m = l.current.update(a);
      d(), m || f();
    }
  });
  function c(s) {
    s && (u.current = le(s), d(), f());
  }
  function d() {
    var a;
    const s = ((a = l.current) == null ? void 0 : a.getCurrentValues()) || [];
    if (n)
      n(u.current, s);
    else
      for (const [m, g] of u.current.entries()) {
        const j = r[m].reverse_rotation_direction ? -1 : 1;
        g.position.y = j * (s[m] || 0) / 1e3;
      }
  }
  const h = _(() => {
    const s = e.joint_position.filter(
      (a) => a !== void 0
    );
    requestAnimationFrame(() => {
      var a;
      o.current = s, (a = l.current) == null || a.setTarget(s);
    });
  }, [e]);
  return T(() => {
    h();
  }, [e, h]), ie(() => {
    h();
  }), /* @__PURE__ */ t.jsx("group", { ref: c, children: i });
}
function ye({
  rapidlyChangingMotionState: e,
  dhParameters: r,
  ...n
}) {
  const i = new w(), o = R(null), u = R(null);
  function l(d) {
    const h = new w();
    for (let g = 0; g < r.length; g++) {
      const p = r[g], j = d[g] ?? 0, y = new w().makeRotationY(p.theta).multiply(
        new w().makeTranslation(
          p.a / 1e3,
          (p.d + j * (p.reverse_rotation_direction ? -1 : 1)) / 1e3,
          0
        )
      ).multiply(new w().makeRotationX(p.alpha));
      h.multiply(y);
    }
    const s = new L(), a = new z(), m = new L();
    return h.decompose(s, a, m), s;
  }
  const f = l(e.joint_position);
  function c(d, h) {
    i.identity();
    let s = new L();
    for (let p = 0; p < r.length; p++) {
      const j = h[p] ?? 0, y = r[p], D = new w().makeRotationY(y.theta).multiply(
        new w().makeTranslation(
          y.a / 1e3,
          (y.d + j * (y.reverse_rotation_direction ? -1 : 1)) / 1e3,
          0
        )
      ).multiply(new w().makeRotationX(y.alpha));
      i.multiply(D);
    }
    const a = new L(), m = new z(), g = new L();
    if (i.decompose(a, m, g), s = a, o.current && o.current.position.set(s.x, s.y, s.z), u.current) {
      const p = u.current.geometry;
      p && p.setPositions && p.setPositions([0, 0, 0, s.x, s.y, s.z]);
    }
  }
  return /* @__PURE__ */ t.jsx(t.Fragment, { children: /* @__PURE__ */ t.jsx(
    ge,
    {
      rapidlyChangingMotionState: e,
      dhParameters: r,
      onTranslationChanged: c,
      children: /* @__PURE__ */ t.jsxs("group", { ...n, name: "Scene", children: [
        /* @__PURE__ */ t.jsxs("mesh", { name: "Base", position: [0, 0, 0], children: [
          /* @__PURE__ */ t.jsx("sphereGeometry", { args: [0.02, 32, 32] }),
          /* @__PURE__ */ t.jsx("meshStandardMaterial", { color: "green", depthTest: !0 })
        ] }),
        /* @__PURE__ */ t.jsx(
          U,
          {
            ref: u,
            points: [new L(0, 0, 0), f],
            color: "White",
            lineWidth: 5
          }
        ),
        /* @__PURE__ */ t.jsxs("mesh", { ref: o, name: "TCP", position: f, children: [
          /* @__PURE__ */ t.jsx("sphereGeometry", { args: [0.025, 32, 32] }),
          /* @__PURE__ */ t.jsx("meshStandardMaterial", { color: "red", depthTest: !0 })
        ] })
      ] })
    }
  ) });
}
const je = P(
  ({
    rapidlyChangingMotionState: e,
    modelFromController: r,
    dhParameters: n,
    getModel: i = C,
    flangeRef: o,
    postModelRender: u,
    transparentColor: l,
    instanceUrl: f,
    ...c
  }) => {
    const [d, h] = G(null), s = _((m) => {
      h(m);
    }, []);
    T(() => {
      d && (l ? he(d, l) : pe(d));
    }, [d, l]);
    const a = /* @__PURE__ */ t.jsx(
      ye,
      {
        rapidlyChangingMotionState: e,
        dhParameters: n,
        ...c
      }
    );
    return /* @__PURE__ */ t.jsxs(
      ae,
      {
        fallback: a,
        onError: (m) => {
          console.warn(m);
        },
        children: [
          /* @__PURE__ */ t.jsx(oe, { fallback: a, children: /* @__PURE__ */ t.jsx("group", { ref: s, children: /* @__PURE__ */ t.jsx(
            ge,
            {
              rapidlyChangingMotionState: e,
              dhParameters: n,
              children: /* @__PURE__ */ t.jsx(
                me,
                {
                  modelURL: (() => {
                    const m = i(r, f);
                    if (!m) {
                      const g = new Blob([], { type: "model/gltf-binary" }), p = new File([g], `${r}.glb`, { type: "model/gltf-binary" });
                      return Promise.resolve(URL.createObjectURL(p));
                    }
                    return m;
                  })(),
                  postModelRender: u,
                  flangeRef: o,
                  ...c
                }
              )
            }
          ) }) }),
          /* @__PURE__ */ t.jsx(de, {})
        ]
      }
    );
  }
);
function lt({
  connectedMotionGroup: e,
  getModel: r = C,
  flangeRef: n,
  transparentColor: i,
  postModelRender: o,
  ...u
}) {
  if (!e.dhParameters)
    return null;
  const l = e.modelFromController || "";
  return l && r(l) ? /* @__PURE__ */ t.jsx(
    je,
    {
      rapidlyChangingMotionState: e.rapidlyChangingMotionState,
      modelFromController: l,
      dhParameters: e.dhParameters,
      getModel: r,
      flangeRef: n,
      transparentColor: i,
      postModelRender: o,
      ...u
    }
  ) : /* @__PURE__ */ t.jsx(
    ye,
    {
      rapidlyChangingMotionState: e.rapidlyChangingMotionState,
      dhParameters: e.dhParameters,
      ...u
    }
  );
}
const ee = {
  [v.Abb]: [0, 0, 0, 0, Math.PI / 2, 0, 0],
  [v.Fanuc]: [0, 0, 0, 0, -Math.PI / 2, 0, 0],
  [v.Yaskawa]: [0, 0, 0, 0, -Math.PI / 2, 0, 0],
  [v.Kuka]: [
    0,
    -Math.PI / 2,
    Math.PI / 2,
    0,
    Math.PI / 2,
    0,
    0
  ],
  [v.Universalrobots]: [
    0,
    -Math.PI / 2,
    -Math.PI / 2,
    -Math.PI / 2,
    Math.PI / 2,
    -Math.PI / 2,
    0
  ]
};
function Je(e) {
  const [r] = e.split("_");
  switch (r) {
    case "ABB":
      return v.Abb;
    case "FANUC":
      return v.Fanuc;
    case "YASKAWA":
      return v.Yaskawa;
    case "KUKA":
      return v.Kuka;
    case "UniversalRobots":
      return v.Universalrobots;
    default:
      return null;
  }
}
function ct(e, r) {
  const n = Je(e);
  return n && n in ee ? ee[n] : r || null;
}
const ut = P((e) => {
  const {
    inverseSolver: r,
    jointType: n,
    ...i
  } = e, o = Ve(() => !r && n === "REVOLUTE_JOINT", [r, n]);
  return r || o ? /* @__PURE__ */ t.jsx(xe, { ...i }) : /* @__PURE__ */ t.jsx(je, { ...i });
});
export {
  rt as C,
  lt as L,
  ee as M,
  Q as P,
  We as R,
  ot as S,
  st as T,
  ut as a,
  it as b,
  je as c,
  xe as d,
  at as e,
  C as f,
  Je as g,
  ct as h
};
//# sourceMappingURL=MotionGroupVisualizer-DzSF9oW7.js.map
