import { Build } from "./utils";

declare global {
  interface Generator<T = unknown, TReturn = any, TNext = unknown> {
    /**
     * Concatenates two sequences.
     * @param item The sequence to concatenate to the first sequence.
     * @returns A Generator<T> that contains the concatenated elements of the two input sequences.
     */
    concat(
      item: Generator<T, TNext, TReturn> | T[]
    ): Generator<T, TNext, TReturn>;
  }

  interface AsyncGenerator<T = unknown, TReturn = any, TNext = unknown> {
    /**
     * Concatenates two sequences.
     * @param item The sequence to concatenate to the first sequence.
     * @returns An AsyncGenerator<T> that contains the concatenated elements of the two input sequences.
     */
    concat(
      item:
        | AsyncGenerator<T, TNext, TReturn>
        | Generator<T, TNext, TReturn>
        | T[]
    ): AsyncGenerator<T, TNext, TReturn>;
  }
}

Build(
  "concat",
  function* (extra) {
    for (const item of this) {
      yield item;
    }

    for (const item of extra) {
      yield item;
    }
  },
  async function* (extra) {
    for await (const item of this) {
      yield item;
    }

    for await (const item of extra) {
      yield item;
    }
  }
);
