import "./array";
import { Build, Promised } from "./utils";

declare global {
  interface Generator<T = unknown, TReturn = any, TNext = unknown> {
    /**
     * Correlates the elements of two sequences based on matching keys.
     * The default equality comparer is used to compare keys.
     * @param inner The sequence to join to the first sequence.
     * @param outer_key_selector A function to extract the join key from each element of this sequence.
     * @param inner_key_selector A function to extract the join key from each element of the inner sequence.
     * @param result_selector A function to create a result element from two matching elements.
     * @returns A sequence that has elements of type TResult that are obtained by performing an inner join on two sequences.
     */
    join<TKey extends string | number | boolean, TInner, TResult>(
      inner: Generator<TInner> | TInner[],
      outer_key_selector: (item: T) => TKey,
      inner_key_selector: (item: TInner) => TKey,
      result_selector: (out: T, inn: TInner) => TResult
    ): Generator<TResult>;
    /**
     * Correlates the elements of two sequences based on matching keys.
     * A specified comparitor is used to compare keys.
     * @param inner The sequence to join to the first sequence.
     * @param outer_key_selector A function to extract the join key from each element of this sequence.
     * @param inner_key_selector A function to extract the join key from each element of the inner sequence.
     * @param result_selector A function to create a result element from two matching elements.
     * @param comparitor A comparitor to hash and compare keys.
     * @returns A sequence that has elements of type TResult that are obtained by performing an inner join on two sequences.
     */
    join<TKey, TInner, TResult>(
      inner: Generator<TInner> | TInner[],
      outer_key_selector: (item: T) => TKey,
      inner_key_selector: (item: TInner) => TKey,
      result_selector: (out: T, inn: TInner) => TResult,
      comparitor: (a: TKey, b: TKey) => boolean
    ): Generator<TResult>;
  }

  interface AsyncGenerator<T = unknown, TReturn = any, TNext = unknown> {
    /**
     * Correlates the elements of two sequences based on matching keys.
     * The default equality comparer is used to compare keys.
     * @param inner The sequence to join to the first sequence.
     * @param outer_key_selector A function to extract the join key from each element of this sequence.
     * @param inner_key_selector A function to extract the join key from each element of the inner sequence.
     * @param result_selector A function to create a result element from two matching elements.
     * @returns A sequence that has elements of type TResult that are obtained by performing an inner join on two sequences.
     */
    join<TKey extends string | number | boolean, TInner, TResult>(
      inner: AsyncGenerator<TInner> | Generator<TInner> | TInner[],
      outer_key_selector: (item: T) => Promised<TKey>,
      inner_key_selector: (item: TInner) => Promised<TKey>,
      result_selector: (out: T, inn: TInner) => Promised<TResult>
    ): AsyncGenerator<TResult>;
    /**
     * Correlates the elements of two sequences based on matching keys.
     * A specified comparitor is used to compare keys.
     * @param inner The sequence to join to the first sequence.
     * @param outer_key_selector A function to extract the join key from each element of this sequence.
     * @param inner_key_selector A function to extract the join key from each element of the inner sequence.
     * @param result_selector A function to create a result element from two matching elements.
     * @param comparitor A comparitor to hash and compare keys.
     * @returns A sequence that has elements of type TResult that are obtained by performing an inner join on two sequences.
     */
    join<TKey, TInner, TResult>(
      inner: AsyncGenerator<TInner> | Generator<TInner> | TInner[],
      outer_key_selector: (item: T) => Promised<TKey>,
      inner_key_selector: (item: TInner) => Promised<TKey>,
      result_selector: (out: T, inn: TInner) => Promised<TResult>,
      comparitor: (a: TKey, b: TKey) => boolean
    ): AsyncGenerator<TResult>;
  }
}

Build(
  "join",
  function* (
    inner,
    outer_key_selector,
    inner_key_selector,
    result_selector,
    comparitor = (a, b) => a === b
  ) {
    const inner_array = Array.isArray(inner) ? inner : inner.array();
    for (const item of this) {
      const key = outer_key_selector(item);
      for (let i = 0; i < inner_array.length; i++) {
        const inn = inner_array[i];
        const bkey = inner_key_selector(inn);
        if (comparitor(key, bkey)) yield result_selector(item, inn);
      }
    }
  },
  async function* (
    inner,
    outer_key_selector,
    inner_key_selector,
    result_selector,
    comparitor = (a, b) => a === b
  ) {
    const inner_array = Array.isArray(inner) ? inner : await inner.array();
    for await (const item of this) {
      const key = await Promise.resolve(outer_key_selector(item));
      for (let i = 0; i < inner_array.length; i++) {
        const inn = inner_array[i];
        const bkey = await Promise.resolve(inner_key_selector(inn));
        if (comparitor(key, bkey))
          yield await Promise.resolve(result_selector(item, inn));
      }
    }
  }
);
