import { isString } from "@thi.ng/checks/is-string" ; import { illegalArgs } from "@thi.ng/errors/illegal-arguments" ; import { illegalArity } from "@thi.ng/errors/illegal-arity" ; import type { Reducer } from "@thi.ng/transducers" ; import { reduce , reducer } from "@thi.ng/transducers/reduce" ; import type { PatchArrayOp } from "./api.js" ; /**
 * Reducer for  Patch based array edits. Only numeric indices are
 * supported (i.e. NO nested edits, use  patchObj for that
 * purpose).
 *
 *  remarks example export function patchArray < T > ( immutable ? ) ; export function patchArray < T > ( immutable , init , patches ) ; export function patchArray < T > ( ... args ) { let immutable ; let init ; let patches ; switch ( args . length ) { case 0 : immutable = true ; break ; case 1 : immutable = args [ 0 ] ; break ; case 3 : immutable = args [ 0 ] ; init = args [ 1 ] ; patches = args [ 2 ] ; break ; default : illegalArity ( args . length ) ; } const edit = ( acc , x ) => { switch ( x [ 0 ] ) { case "set" : acc [ x [ 1 ] ] = x [ 2 ] ; break ; case "update" : acc [ x [ 1 ] ] = x [ 2 ] ( acc [ x [ 1 ] ] , ... x . slice ( 3 ) ) ; break ; case "insert" : acc . splice ( x [ 1 ] , 0 , ... x [ 2 ] ) ; break ; case "delete" : acc . splice ( x [ 1 ] , 1 ) ; break ; default : illegalArgs ( `patch op: ${ x }` ) ; } return acc ; } ; return patches ? reduce ( patchArray < T > ( immutable ) , init ! , patches ) : reducer < T [ ] , PatchArrayOp < T > | PatchArrayOp < T > [ ] > ( ( ) => [ ] , ( acc , x ) => { immutable && ( acc = acc . slice ( ) ) ; if ( isString ( x [ 0 ] ) ) { acc = edit ( acc , x ) ; } else { for ( let e of < PatchArrayOp < T > [ ] > x ) { acc = edit ( acc , e ) ; } } return acc ; } ) ; }