import {
    TransitionPropertyVectorXBehavior
} from "../../../../../model/game/story/behaviors/generic/TransitionPropertyVectorXBehavior.js";
import { assert } from "../../../core/assert.js";
import { SerializationMetadata } from "../../ecs/components/SerializationMetadata.js";
import Entity from "../../ecs/Entity.js";
import { SequenceBehavior } from "../../intelligence/behavior/composite/SequenceBehavior.js";
import { BehaviorComponent } from "../../intelligence/behavior/ecs/BehaviorComponent.js";
import { DieBehavior } from "../../intelligence/behavior/ecs/DieBehavior.js";
import { SendEventBehavior } from "../../intelligence/behavior/ecs/SendEventBehavior.js";

/**
 *
 * @param {EntityComponentDataset} ecd
 * @param {number} entity
 * @param {string} component
 * @param {string} property
 * @param {number[]} target
 * @param {number} time
 * @returns {Promise<void>}
 */
export async function animateProperty(ecd, entity, component, property, target, time) {

    assert.ok(ecd.entityExists(entity), `entity ${entity} doesn't exist`);

    const builder = new Entity()
        .add(SerializationMetadata.Transient)
        .add(BehaviorComponent.from(SequenceBehavior.from([
            TransitionPropertyVectorXBehavior.from({
                component,
                path: property,
                entity,
                target,
                duration: time
            }),
            SendEventBehavior.fromJSON({ event: 'done' }),
            DieBehavior.create()
        ])));

    const promise = builder.promiseEvent('done');

    builder.build(ecd);

    await promise;
}
