import { ApKind , ofAp } from './ap' ; import { IsUnion } from './do' ; import { ReplaceFirst , Type } from './hkt' ; export const sequenceS = < Of extends ofAp < any > > ( of ) => < Ap extends ApKind < any , any > = ReturnType < Of > , NER extends Record < string , Ap > = any , A extends any [ ] = ( NER extends Record < string , infer A > ? A : never ) [ '_A' ] , Rest = A extends [ infer _A , ... infer Rest ] ? Rest : never > ( r ) : IsUnion < Rest > extends true ? [ 'ERROR: All secondary generics must be of the same type. Different generics found: ' , Rest ] : Type < Ap [ '_URI' ] , ReplaceFirst < A , { [ K in keyof NER ] : NER [ K ] extends ApKind < any , infer A > ? A [ 0 ] : never ; } > > => { const entries = Object . entries ( r ) as [ string , Ap ] [ ] ; const newObj = of ( { } ) ; const add = ( key ) => of ( ( obj ) => ( value ) => ( ( obj [ key ] = value ) , obj ) ) ; const res = entries . reduce ( ( prev , [ key , value ] ) => { const val1 = prev . ap ( add ( key ) ) ; const val2 = val1 . ap ( value . map ( ( val ) => ( f ) => f ( val ) ) ) ; return val2 ; } , newObj ) ; return res as any ; } ; export const sequenceSFlex = < Of extends ofAp < any > > ( of ) => < Ap extends ApKind < any , any > = ReturnType < Of > , NER extends Record < string , Ap > = any , A extends any [ ] = ( NER extends Record < string , infer A > ? A : never ) [ '_A' ] > ( r ) : Type < Ap [ '_URI' ] , ReplaceFirst < A , { [ K in keyof NER ] : NER [ K ] extends ApKind < any , infer A > ? A [ 0 ] : never ; } > > => { return sequenceS ( of ) ( r as any ) ; } ;