import { Schema, schema } from '@data-client/endpoint'; export default function mapCollection< M extends (collection: C) => any, S extends Schema | undefined, >( s: S, mapper: M, ): S extends schema.Collection ? ReturnType> : S extends schema.Object ? { [K in keyof T]: T[K] extends Schema ? typeof mapCollection : T[K]; } : S extends { [K: string]: any } ? { [K in keyof S]: S[K] extends Schema ? typeof mapCollection : S[K]; } : S { if (typeof s !== 'object' || s === undefined) return s as any; if (s instanceof schema.Collection) { return mapper(s as any); } const objCopy: Record = { ...(s instanceof schema.Object ? (s as any).schema : s), }; for (const k in objCopy) { if (!objCopy[k]) continue; objCopy[k] = mapCollection(objCopy[k], mapper) as any; } return objCopy as any; } type MapCollection< M extends (collection: C) => any, S extends Schema | undefined, > = S extends schema.Collection ? ReturnType : S extends schema.Object ? MapCollection : S extends { [K: string]: any } ? MapObject : never; export type MapObject< M extends (collection: schema.Collection) => any, S extends Record, > = { [K in keyof S]: S[K] extends Schema ? MapCollection : S[K]; };