var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var animations_exports = {};
__export(animations_exports, {
  buildAnimations: () => buildAnimations
});
module.exports = __toCommonJS(animations_exports);
function createKeyframeTrack(THREE, channel, nodeLookup) {
  const object = nodeLookup.get(channel.node);
  if (!object) {
    console.warn("[boxel-three] animation channel references missing node", channel.node);
    return null;
  }
  const times = channel.keyframes.map((keyframe) => keyframe.time);
  const targetName = object.name && object.name.length > 0 ? object.name : object.uuid;
  switch (channel.property) {
    case "position": {
      const values = channel.keyframes.flatMap((keyframe) => keyframe.value);
      return new THREE.VectorKeyframeTrack(`${targetName}.position`, times, values);
    }
    case "rotation": {
      const values = [];
      channel.keyframes.forEach((keyframe) => {
        const radX = THREE.MathUtils.degToRad(keyframe.value[0]);
        const radY = THREE.MathUtils.degToRad(keyframe.value[1]);
        const radZ = THREE.MathUtils.degToRad(keyframe.value[2]);
        const quaternion = new THREE.Quaternion().setFromEuler(new THREE.Euler(radX, radY, radZ));
        values.push(quaternion.x, quaternion.y, quaternion.z, quaternion.w);
      });
      return new THREE.QuaternionKeyframeTrack(`${targetName}.quaternion`, times, values);
    }
    case "scale": {
      const values = channel.keyframes.flatMap((keyframe) => keyframe.value);
      return new THREE.VectorKeyframeTrack(`${targetName}.scale`, times, values);
    }
    default:
      return null;
  }
}
function buildAnimation(THREE, animation, nodeLookup) {
  const tracks = [];
  animation.channels.forEach((channel) => {
    const track = createKeyframeTrack(THREE, channel, nodeLookup);
    if (!track)
      return;
    tracks.push({
      nodeId: channel.node,
      property: channel.property,
      track
    });
  });
  if (tracks.length === 0) {
    return null;
  }
  const threeTracks = tracks.map((channel) => channel.track);
  const inferredDuration = threeTracks.reduce((max, track) => {
    const times = track.times;
    if (!times || times.length === 0)
      return max;
    return Math.max(max, times[times.length - 1]);
  }, 0);
  const duration = typeof animation.duration === "number" && animation.duration > 0 ? animation.duration : inferredDuration;
  const clip = new THREE.AnimationClip(animation.id, duration, threeTracks);
  return {
    id: animation.id,
    duration,
    channels: tracks,
    clip
  };
}
function buildAnimations(THREE, entity, nodeLookup) {
  return (entity.animations ?? []).map((animation) => buildAnimation(THREE, animation, nodeLookup)).filter((value) => Boolean(value));
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  buildAnimations
});
