import { Injectable } from '@angular/core';

function* getUniqueNumber(): IterableIterator<number> {
  let timeStamp = Date.now();
  while (true) {
    yield timeStamp++;
  }
}

@Injectable({ providedIn: 'root' })
export class UidGeneratorService {
  private generator = getUniqueNumber();

  constructor() {}

  generate(options: { type: 'string' }): string;
  generate(options: { type: 'number' }): number;
  generate(options: { type: 'string' | 'number' } = { type: 'string' }): string | number {
    const id = this.generator.next();

    if (options.type === 'string') {
      return id.value.toString(32);
    }

    return id.value;
  }
}
