import { SerializationMetadata } from "../ecs/components/SerializationMetadata.js";
import Entity from "../ecs/Entity.js";
import { BehaviorComponent } from "../intelligence/behavior/ecs/BehaviorComponent.js";
import { ClockChannelType } from "../intelligence/behavior/ecs/ClockChannelType.js";
import { AnimationBehavior } from "./keyed2/behavior/AnimationBehavior.js";

/**
 *
 * @param {AnimationTrackPlayback} track
 * @param {EntityComponentDataset} ecd
 *
 * @returns {Entity}
 */
export function playTrackRealTime(track, ecd) {

    const entity = new Entity();

    const behaviorComponent = BehaviorComponent.from(new AnimationBehavior(track));

    behaviorComponent.clock = ClockChannelType.System;

    entity.add(behaviorComponent);
    entity.add(SerializationMetadata.Transient);

    // tick animation to make sure the state is synchronized
    track.update();

    entity.build(ecd);

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

    return entity;
}
