import type { ObjRec, PH } from './utils/types.ts';import { isArrayLike, isInteger, isString, isUndefinedOrNull,} from './utils/is.ts';import { nth } from './nth.ts';import curryN from './utils/curry_n.ts';import { trim } from './trim.ts';
export type Path = string | Array<string | number>;
type Paths_2 = <T, R>(obj: ObjRec<T> | null) => R[];
type Paths_1<T, R> = (pathsArr: Path[]) => R[];
type Paths = & ((pathsArr: Path[]) => Paths_2) & (<T, R>(pathsArr: PH, obj: ObjRec<T> | null) => Paths_1<T, R>) & (<T, R>(pathsArr: Path[], obj: ObjRec<T> | null) => R[]);
export function getPath(path: Path): Array<string | number> { if (isString(path)) { if (path.includes('/')) return trim(path, '/').split('/'); if (path.includes('.')) return trim(path, '.').split('.'); return path ? [path] : []; }
return path;}
function _paths<T, R>(pathsArr: Path[], obj: ObjRec<T> | null): R[] { return pathsArr.map((p) => { const path = getPath(p); let val: any = obj; for (let i = 0; i < path.length; i++) { if (isUndefinedOrNull(val)) return undefined; const p = path[i]; const pInt = parseInt(p as string, 10); val = isInteger(pInt) && isArrayLike(val) ? nth(pInt, val) : val[p]; } return val; });}
export const paths: Paths = curryN(2, _paths);