/*!
 * Copyright (c) 2017-2018 by The Funfix Project Developers.
 * Some rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* @flow */

import type { HK, HK2 } from "funland"
import { Setoid, Monad } from "funland"
import type { Throwable } from "./errors"

declare export class Either<+L, +R> {
  +value: L | R;

  get(): R;
  getOrElse<RR>(fallback: RR): R | RR;
  getOrElseL<RR>(thunk: () => RR): R | RR;

  isLeft(): boolean;
  isRight(): boolean;
  contains(elem: R): boolean;
  exists(p: (r: R) => boolean): boolean;
  filterOrElse<LL>(p: (r: R) => boolean, zero: () => LL): Either<L | LL, R>;
  flatMap<S>(f: (r: R) => Either<L, S>): Either<L, S>;
  fold<S>(left: (l: L) => S, right: (r: R) => S): S;
  forAll(p: (r: R) => boolean): boolean;
  map<C>(f: (r: R) => C): Either<L, C>;
  forEach(cb: (r: R) => void): void;
  swap(): Either<R, L>;
  toOption(): Option<R>;
  equals(that: Either<L, R>): boolean;
  hashCode(): number;

  // Implements Constructor<T>
  static +_Class: Either<any, any>;

  static left<L, R>(value: L): Either<L, R>;
  static right<L, R>(value: R): Either<L, R>;

  static map2<A1,A2,L,R>(
    fa1: Either<L,A1>, fa2: Either<L,A2>,
    f: (A1, A2) => R): Either<L, R>;
  static map3<A1,A2,A3,L,R>(
    fa1: Either<L,A1>, fa2: Either<L,A2>, fa3: Either<L,A3>,
    f: (A1, A2, A3) => R): Either<L, R>;
  static map4<A1,A2,A3,A4,L,R>(
    fa1: Either<L,A1>, fa2: Either<L,A2>, fa3: Either<L,A3>, fa4: Either<L,A4>,
    f: (A1, A2, A3, A4) => R): Either<L, R>;
  static map5<A1,A2,A3,A4,A5,L,R>(
    fa1: Either<L,A1>, fa2: Either<L,A2>, fa3: Either<L,A3>, fa4: Either<L,A4>, fa5: Either<L,A5>,
    f: (A1, A2, A3, A4, A5) => R): Either<L, R>;
  static map6<A1,A2,A3,A4,A5,A6,L,R>(
    fa1: Either<L,A1>, fa2: Either<L,A2>, fa3: Either<L,A3>, fa4: Either<L,A4>, fa5: Either<L,A5>, fa6: Either<L,A6>,
    f: (A1, A2, A3, A4, A5, A6) => R): Either<L, R>;

  static tailRecM<L, A, B>(a: A, f: (a: A) => Either<L, Either<A, B>>): Either<L, B>;
}

declare export class TLeft<+L> extends Either<L, empty> {
  +value: L;
}

declare export class TRight<+R> extends Either<empty, R> {
  +value: R;
}

declare export function Left<L>(value: L): TLeft<L>;
declare export function Right<R>(value: R): TRight<R>;

export type EitherURI = <U, L, A>(x: [U, L, A]) => Either<L, A>;
export type EitherK<L, R> = HK2<EitherURI, L, R>;

export type EitherTypes = Setoid<Either<any, any>> & Monad<EitherURI>;
declare export var EitherModule: EitherTypes;

declare export class Option<+A> {
  +value: void | A;

  get(): A;
  getOrElse<AA>(fallback: AA): A | AA;
  getOrElseL<AA>(thunk: () => AA): A | AA;
  orElse<AA>(fallback: Option<AA>): Option<A | AA>;
  orElseL<AA>(thunk: () => Option<AA>): Option<A | AA>;
  orNull(): A | null;
  orUndefined(): A | void;

  chain<B>(f: (a: A) => Option<B>): Option<B>;
  contains(elem: A): boolean;
  equals(that: Option<A>): boolean;
  exists(p: (a: A) => boolean): boolean;
  filter(p: (a: A) => boolean): Option<A>;
  flatMap<B>(f: (a: A) => Option<B>): Option<B>;
  fold<B>(fallback: () => B, f: (a: A) => B): B;
  forAll(p: (a: A) => boolean): boolean;
  forEach(cb: (a: A) => void): void;
  hashCode(): number;
  isEmpty(): boolean;
  map<B>(f: (a: A) => B): Option<B>;
  nonEmpty(): boolean;

  // Implements Constructor<T>
  static +_Class: Option<any>;

  static empty<A>(): Option<A>;
  static none<A>(): Option<A>;
  static of<A>(value: ?A): Option<A>;
  static pure<A>(value: A): Option<A>;
  static some<A>(value: A): Option<A>;

  static map2<A1,A2,R>(
    fa1: Option<A1>, fa2: Option<A2>,
    f: (A1, A2) => R): Option<R>;
  static map3<A1,A2,A3,R>(
    fa1: Option<A1>, fa2: Option<A2>, fa3: Option<A3>,
    f: (A1, A2, A3) => R): Option<R>;
  static map4<A1,A2,A3,A4,R>(
    fa1: Option<A1>, fa2: Option<A2>, fa3: Option<A3>, fa4: Option<A4>,
    f: (A1, A2, A3, A4) => R): Option<R>;
  static map5<A1,A2,A3,A4,A5,R>(
    fa1: Option<A1>, fa2: Option<A2>, fa3: Option<A3>, fa4: Option<A4>, fa5: Option<A5>,
    f: (A1, A2, A3, A4, A5) => R): Option<R>;
  static map6<A1,A2,A3,A4,A5,A6,R>(
    fa1: Option<A1>, fa2: Option<A2>, fa3: Option<A3>, fa4: Option<A4>, fa5: Option<A5>, fa6: Option<A6>,
    f: (A1, A2, A3, A4, A5, A6) => R): Option<R>;

  static tailRecM<A, B>(a: A, f: (a: A) => Option<Either<A, B>>): Option<B>;
}

declare export class TSome<A> extends Option<A> {
  +value: A;
}

declare export class TNone extends Option<empty> {
  +value: void;
}

declare export function Some<A>(value: A): TSome<A>;
declare export var None: TNone;

export type OptionURI = <U, L, A>(x: [U, L, A]) => Option<A>;
export type OptionK<A> = HK<OptionURI, A>;

export type OptionTypes = Setoid<Option<any>> & Monad<OptionURI>;
declare export var OptionModule: OptionTypes;

declare export class Try<+A> {
  +value: Throwable | A;

  get(): A;
  getOrElse<AA>(fallback: AA): A | AA;
  getOrElseL<AA>(thunk: () => AA): A | AA;
  orElse<AA>(fallback: Try<AA>): Try<A | AA>;
  orElseL<AA>(thunk: () => Try<AA>): Try<A | AA>;
  orNull(): A | null;
  orUndefined(): A | void;

  isSuccess(): boolean;
  isFailure(): boolean;
  failed(): Try<Throwable>;
  fold<R>(failure: (error: Throwable) => R, success: (a: A) => R): R;
  filter(p: (a: A) => boolean): Try<A>;
  flatMap<B>(f: (a: A) => Try<B>): Try<B>;
  chain<B>(f: (a: A) => Try<B>): Try<B>;
  map<B>(f: (a: A) => B): Try<B>;
  forEach(cb: (a: A) => void): void;
  recover<AA>(f: (error: Throwable) => AA): Try<A | AA>;
  recoverWith<AA>(f: (error: Throwable) => Try<AA>): Try<A | AA>;
  toOption(): Option<A>;
  toEither(): Either<Throwable, A>;
  equals(that: Try<A>): boolean;
  hashCode(): number;

  // Implements Constructor<T>
  static +_Class: Try<any>;

  static of<A>(thunk: () => A): Try<A>;
  static pure<A>(value: A): Try<A>;
  static unit(): Try<void>;
  static success<A>(value: A): Try<A>;
  static failure<A>(e: Throwable): Try<A>;
  static raise<A>(e: Throwable): Try<A>;

  static map2<A1,A2,R>(
    fa1: Try<A1>, fa2: Try<A2>,
    f: (A1, A2) => R): Try<R>;
  static map3<A1,A2,A3,R>(
    fa1: Try<A1>, fa2: Try<A2>, fa3: Try<A3>,
    f: (A1, A2, A3) => R): Try<R>;
  static map4<A1,A2,A3,A4,R>(
    fa1: Try<A1>, fa2: Try<A2>, fa3: Try<A3>, fa4: Try<A4>,
    f: (A1, A2, A3, A4) => R): Try<R>;
  static map5<A1,A2,A3,A4,A5,R>(
    fa1: Try<A1>, fa2: Try<A2>, fa3: Try<A3>, fa4: Try<A4>, fa5: Try<A5>,
    f: (A1, A2, A3, A4, A5) => R): Try<R>;
  static map6<A1,A2,A3,A4,A5,A6,R>(
    fa1: Try<A1>, fa2: Try<A2>, fa3: Try<A3>, fa4: Try<A4>, fa5: Try<A5>, fa6: Try<A6>,
    f: (A1, A2, A3, A4, A5, A6) => R): Try<R>;

  static tailRecM<A, B>(a: A, f: (a: A) => Try<Either<A, B>>): Try<B>;
}

declare export class TSuccess<A> extends Try<A> {
  +value: A;
}

declare export class TFailure extends Try<empty> {
  +value: void;
}

declare export function Success<A>(value: A): TSuccess<A>;
declare export function Failure(e: Throwable): TFailure;

export type TryURI = <U, L, A>(x: [U, L, A]) => Try<A>;
export type TryK<A> = HK<TryURI, A>;

export type TryTypes = Setoid<Try<any>> & Monad<TryURI>;
declare export var TryModule: TryTypes;
