'use strict';

/** Makes specified properties required in a type */
export type WithRequired<T, K extends keyof T> = T & {[P in K]-?: T[P]};

/** Makes specified properties optional in a type */
export type WithOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;

/** Signifies that the value can be undefined */
export type Optional<T> = T | undefined;

/**
 * Native `Omit` has some known bugs which are not going to be fixed, as it can break backward compatibility, so here
 * is a modern fixed version (https://stackoverflow.com/questions/76616163/omit-seems-broken-on-type-extending-record)
 */
export type Omit<T, K extends PropertyKey> = {[P in keyof T as Exclude<P, K>]: T[P]};

/**
 * Creates a type as one of const array elements.
 * 
 * @example
 * const TIME_UNITS = ['hours', 'minutes', 'seconds'] as const;
 * type TimeUnit = OneOf<typeof TIME_UNITS>;
 */
export type OneOf<T extends readonly unknown[]> = T extends readonly (infer K)[] ? K : never;

/** Deeply replaces `Date` and `Buffer` fields to their JSON representation in a type */
export type Serialized<T> = {
  [P in keyof T]: T[P] extends Date ? string :
    T[P] extends Buffer ? {type: 'Buffer', data: number[]} :
      Serialized<T[P]>
};

/** Signifies that a type can be wrapped in a promise */
export type PromiseOrNot<T> = Promise<T> | T;

/** Constructor function */
export type Constructor<T, Args extends any[] = any[]> = {new(...args: Args): T};

/** Extracts method names of a class */
export type MethodNames<T> = {[K in keyof T]: T[K] extends (...args: any[]) => any ? K : never}[keyof T];

/**
 * Replaces a key type
 * 
 * @example
 * type ExampleType = {a: number, b: string};
 * type ReplacedType = Replace<ExampleType, {b: Date}>;
 */
export type Replace<T, Replacements extends {[Key in keyof T]?: Replacements[Key]}> = {
  [P in keyof T]: P extends keyof Replacements ? Replacements[P] : T[P]
};

/** Like `Partial` but deep */
export type DeepPartial<T> = T extends object ?
  T extends Date ? T :
  {[P in keyof T]?: DeepPartial<T[P]>} : T;

/** Like `Required` but deep */
export type RequiredDeep<T> = T extends object ?
  T extends Date ? T :
  {[P in keyof T]-?: RequiredDeep<T[P]>} : T;

/** Selects only public members of a class */
export type Public<T> = {[K in keyof T]: T[K]};

/** Shortened `Record` where keys are always strings */
export type Dict<T> = Record<string, T>;

// Helper to split a tuple into two parts at a given index
type TupleSplit<T extends readonly any[], N extends number, Acc extends readonly any[] = []> =
  Acc['length'] extends N
  ? [Acc, T]
  : T extends readonly [infer Head, ...infer Tail]
    ? TupleSplit<Tail, N, [...Acc, Head]>
    : [Acc, []];

/** Skips the first N elements of a tuple */
export type SkipFirst<T extends readonly any[], N extends number> = TupleSplit<T, N>[1];

/**
 * Calls `bind` on function saving original function type
 * @param func function to bind
 * @param thisArg `this` argument
 * @returns bound function
 */
// eslint-disable-next-line @typescript-eslint/ban-types
export function typedBind<T extends Function>(func: T, thisArg: any): T {
  return func.bind(thisArg);
}
