// @ts-nocheck
/**
 * @deprecated Promise.withResolvers
 */
export function withResolvers<T>(): {
  promise: Promise<T>;
  resolve: (value: T | PromiseLike<T>) => void;
  reject: (reason?: any) => void;
} {
  if ( typeof Promise.withResolvers === 'function' ) {
    withResolvers = Promise.withResolvers.bind(Promise);
    return Promise.withResolvers<T>();
  } else {
    const fn = function() {
      const out = {};
      out.promise = new Promise<T>((resolve_, reject_) => {
        out.resolve = resolve_;
        out.reject = reject_;
      });
      return out;
    };
    withResolvers = fn.bind(Promise);
    return fn();
  }
}

export function groupBy<T>(items: Iterable<T>, callbackFn: (element: T, index: number) => any): Record<string, T[]> {
  if ( typeof Object.groupBy === 'function' ) {
    groupBy = Object.groupBy.bind(Object);
    return Object.groupBy(items, callbackFn);
  } else {
    const fn = function(items, callbackFn) {
      const obj = Object.create(null);
      let k = 0;
      for ( const value of items ) {
        const key = callbackFn(value, k++);
        if ( key in obj ) { obj[ key ].push(value); } else { obj[ key ] = [ value ]; }
      }
    };
    groupBy = fn.bind(Object);
    return fn(items, callbackFn);
  }
}
