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

/**
 * Causes another entity to be destroyed
 * Will produce a {@link BehaviorStatus.Failed} if entity could not be destroyed, either because it doesn't exist or the reference is stale
 * @author Alex Goldring
 * @copyright Company Named Limited (c) 2026
 */
export class KillBehavior extends EntityBehavior {

    /**
     * @type {EntityReference}
     */
    #ref;

    /**
     *
     * @param {EntityReference} ref
     * @returns {KillBehavior}
     */
    static create(ref) {
        assert.defined(ref, 'ref');
        assert.isObject(ref, 'ref');
        assert.equal(ref.isEntityReference, true, 'ref.isEntityReference !== true');

        const r = new KillBehavior();

        r.#ref = ref;

        return r;
    }

    tick(timeDelta) {

        const ecd = this.ecd;

        if (this.#ref.destroy(ecd)) {

            return BehaviorStatus.Succeeded;

        } else {
            // couldn't find
            return BehaviorStatus.Failed;

        }
    }
}