import Script from "../ecs/components/Script.js";
import { SerializationMetadata } from "../ecs/components/SerializationMetadata.js";
import Entity from "../ecs/Entity.js";

/**
 *
 * @param {AnimationTrackPlayback} track
 * @param {EntityComponentDataset} ecd
 * @returns {Entity}
 */
export function playAnimationTrack(track, ecd) {
    const script = new Script();

    script.scripts.push(function (timeDelta) {
        track.advance(timeDelta);
    });

    const entity = new Entity();
    entity.add(script);
    entity.add(SerializationMetadata.Transient);

    entity.build(ecd);

    track.on.ended.add(function () {
        entity.destroy();
    });

    return entity;
}
