import { FindPaths } from "./FindPaths"; import { Path } from "./types"; import { Next } from "./utils"; import { Type, A, B, C, Checked, apply, Last, partialRight } from "free-types-core"; import { Replace } from "./Replace"; import { Over } from "./Over"; export { FindReplace, $Callback as $ReplaceCallback }; type $Callback = Type<[T, Path?]>; type FindReplace, From extends Path = [], Limit extends number = number> = FindPaths extends infer Queries ? [Queries] extends [never] ? T : Queries extends Path[] ? Fold : V extends Type<[Needle]> ? $WithUnaryCallback : V extends Type ? $WithBinaryCallback : never> : never : never; interface $WithValue extends Type<[unknown, Path, number]> { type: unknown extends this[1] ? unknown : Replace, A, LastAvailable>, any>; } interface $WithUnaryCallback> extends Type<[unknown, Path]> { type: unknown extends this[1] ? unknown : Over, A, V, any>; } interface $WithBinaryCallback extends Type<[unknown, Path]> { type: unknown extends this[1] ? unknown : Over, A, partialRight], $Callback>, any>; } type Fold, Acc extends unknown = Init, I extends number = 0> = I extends T['length'] ? Acc : Fold, Next>; type LastAvailable = IsIndexOf extends true ? V[I] : Last; type IsIndexOf = `${I}` extends Extract ? true : false;