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

/**
 * Inverts result of the source behavior, success becomes failure, failure becomes success
 *
 * @author Alex Goldring
 * @copyright Company Named Limited (c) 2026
 */
export class InvertStatusBehavior extends AbstractDecoratorBehavior {
    /**
     *
     * @param {Behavior} source
     */
    constructor(source) {
        super();

        this.setSource(source);
    }

    /**
     *
     * @param {Behavior} source
     * @return {InvertStatusBehavior}
     */
    static from(source) {
        return new InvertStatusBehavior(source);
    }

    tick(timeDelta) {
        let r = this.__source.tick(timeDelta);

        if (r === BehaviorStatus.Succeeded) {
            r = BehaviorStatus.Failed;
        } else if (r === BehaviorStatus.Failed) {
            r = BehaviorStatus.Succeeded;
        }

        return r;
    }
}

InvertStatusBehavior.typeName = "InverterBehavior";

