import { assert } from "../../../../core/assert.js";
import { BehaviorStatus } from "../BehaviorStatus.js";
import { EntityBehavior } from "./EntityBehavior.js";

/**
 * Dispatch an event to an entity via {@link EntityComponentDataset#sendEvent}
 * @see WaitForEventBehavior
 * @author Alex Goldring
 * @copyright Company Named Limited (c) 2026
 */
export class SendEventBehavior extends EntityBehavior {

    /**
     * ID of the event
     * @type {string}
     */
    event = "";

    data = {};

    /**
     *
     * @type {number}
     */
    target = -1;

    /**
     *
     * @param {string} event
     * @param {Object} [data]
     * @param {number} [target]
     */
    fromJSON({ event, data = {}, target = -1 }) {
        assert.isString(event, 'event');
        assert.isInteger(target, 'target');

        this.event = event;
        this.data = data;
        this.target = target;
    }

    /**
     *
     * @param {{event:string, data?:Object, target?:number}} json
     * @return {SendEventBehavior}
     */
    static fromJSON(json) {
        const r = new SendEventBehavior();

        r.fromJSON(json);

        return r;
    }

    toString() {
        return `SendEventBehavior{ event: ${this.event}, data:${this.data}, target:${this.target} }`;
    }

    tick(timeDelta) {
        // console.log(`-> ${this}`);

        const target = this.target !== -1 ? this.target : this.entity;

        this.ecd.sendEvent(target, this.event, this.data);

        return BehaviorStatus.Succeeded;
    }
}
