using System;

namespace RebootKit.Engine.Foundation {
    public readonly struct Either<TLeft, TRight> {
        public TLeft Left { get; }
        public TRight Right { get; }
        public bool IsRight { get; }

        public bool IsLeft => !IsRight;

        Either(TLeft left, TRight right, bool isRight) {
            Left = left;
            Right = right;
            IsRight = isRight;
        }

        public void Match(Action<TLeft> leftAction, Action<TRight> rightAction) {
            if (IsRight) {
                rightAction(Right);
            } else {
                leftAction(Left);
            }
        }

        public static Either<TLeft, TRight> FromLeft(TLeft value) {
            return new Either<TLeft, TRight>(value, default, false);
        }

        public static Either<TLeft, TRight> FromRight(TRight value) {
            return new Either<TLeft, TRight>(default, value, true);
        }

        public override string ToString() {
            return IsRight ? $"Right({Right})" : $"Left({Left})";
        }
    }
}