import { Behavior } from "../Behavior.js";
import { BehaviorStatus } from "../BehaviorStatus.js";

/**
 *
 * @author Alex Goldring
 * @copyright Company Named Limited (c) 2026
 */
export class PromiseBehavior extends Behavior {
    /**
     *
     * @param {()=>Promise} factory
     */
    constructor(factory) {
        super();

        /**
         *
         * @type {function(): Promise}
         * @private
         */
        this.__factory = factory;

        this.__promise = null;

        this.__promise_state = BehaviorStatus.Running;
    }

    initialize(context) {
        super.initialize(context);

        this.__promise = this.__factory();
        this.__promise_state = BehaviorStatus.Running;

        this.__promise.then(
            () => {
                this.__promise_state = BehaviorStatus.Succeeded;
            },
            () => {
                this.__promise_state = BehaviorStatus.Failed;
            }
        );
    }

    tick(timeDelta) {
        return this.__promise_state;
    }
}
