import DataType from "../../../../parser/simple/DataType.js";
import { ReactiveBinaryExpression } from "../ReactiveBinaryExpression.js";

export class ReactiveOr extends ReactiveBinaryExpression {
    /**
     *
     * @param {boolean} left
     * @param {boolean} right
     * @returns {boolean}
     */
    transform(left, right) {
        return left || right;
    }

    /**
     *
     * @return {ReactiveOr}
     */
    clone() {
        const r = new ReactiveOr();

        r.copy(this);

        return r;
    }

    evaluate(scope) {
        const left = this.left.evaluate(scope);

        if (left) {
            return true;
        }

        return this.right.evaluate(scope);
    }

    equals(other) {
        return (other.isReactiveOr === true) && super.equals(other);
    }

    toCode() {
        return `( ${this.left.toCode()} || ${this.right.toCode()} )`
    }

    /**
     *
     * @param {ReactiveExpression} left
     * @param {ReactiveExpression} right
     * @returns {ReactiveOr}
     */
    static from(left, right) {
        const r = new ReactiveOr();

        r.connect(left, right);

        return r;
    }

}

/**
 * @readonly
 * @type {DataType|string}
 */
ReactiveOr.prototype.dataType = DataType.Boolean;

/**
 * @readonly
 * @type {boolean}
 */
ReactiveOr.prototype.isLogicExpression = true;

/**
 * @readonly
 * @type {boolean}
 */
ReactiveOr.prototype.isReactiveOr = true;

/**
 * @readonly
 * @type {boolean}
 */
ReactiveOr.prototype.isCommutative = true;
