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

/**
 *
 * @author Alex Goldring
 * @copyright Company Named Limited (c) 2026
 */
export class RepeatUntilSuccessBehavior extends AbstractDecoratorBehavior {

    /**
     *
     * @param {Behavior} source
     * @param {number} [count=Infinity]
     */
    constructor(source, count = Infinity) {
        super();

        /**
         *
         * @type {number}
         * @private
         */
        this.__limit = count;

        /**
         *
         * @type {Behavior}
         * @private
         */
        this.__source = source;

        /**
         *
         * @type {number}
         * @private
         */
        this.__iterator = 0;
    }

    /**
     *
     * @param {Behavior} source
     * @param {number} [limit]
     * @return {RepeatUntilSuccessBehavior}
     */
    static from(source, limit = Infinity) {
        const r = new RepeatUntilSuccessBehavior(
            source,
            limit
        );

        return r;
    }

    tick(timeDelta) {
        const s = this.__source.tick(timeDelta);

        if (s === BehaviorStatus.Succeeded) {
            return s;

        } else if (s === BehaviorStatus.Running) {
            return s;
        }

        this.__iterator++;

        if (this.__iterator >= this.__limit) {
            return BehaviorStatus.Failed;
        } else {
            //re-initialize the source behavior
            this.__source.initialize(this.context);

            return BehaviorStatus.Running;
        }
    }
}
