import { AnimationClip } from "../animation/browser-index.js";
import { Renderable, Scene } from "../scene/index.js";
export class SceneLoader {
    type = "scene";
    canLoad(request) {
        return /\.scene\.json(?:\?.*)?$/i.test(request.url);
    }
    async load(request, context) {
        context.throwIfAborted(request.url);
        if (typeof fetch !== "function") {
            throw new Error("SceneLoader requires fetch");
        }
        const response = await fetch(request.url, { signal: request.signal });
        if (!response.ok) {
            throw new Error(`Scene request failed with ${response.status}`);
        }
        const descriptor = (await response.json());
        const scene = new Scene();
        for (const node of descriptor.nodes ?? []) {
            scene.root.addChild(createNode(scene, node));
        }
        const animations = (descriptor.animations ?? []).map((animation) => AnimationClip.fromJSON(animation));
        return { url: request.url, scene, animations };
    }
}
function createNode(scene, descriptor) {
    const node = scene.createNode(descriptor.name);
    if (descriptor.translation)
        node.transform.setPosition(descriptor.translation[0], descriptor.translation[1], descriptor.translation[2]);
    if (descriptor.rotation)
        node.transform.setRotation(descriptor.rotation[0], descriptor.rotation[1], descriptor.rotation[2], descriptor.rotation[3]);
    if (descriptor.scale)
        node.transform.setScale(descriptor.scale[0], descriptor.scale[1], descriptor.scale[2]);
    if (descriptor.renderable)
        scene.addRenderable(node, new Renderable(descriptor.renderable));
    for (const child of descriptor.children ?? [])
        node.addChild(createNode(scene, child));
    return node;
}
//# sourceMappingURL=SceneLoader.js.map