import { Data } from "../interfaces" ; import { getType } from "../../misc/index" ; import pathLayer from "../../path/index" ; var log = require ( "loglevel" ) . getLogger ( "phases-log" ) ; const getValueFromRange = ( values , numOfKeyPaths , index ) => { let min = Math . min ( ... values ) ; let max = Math . max ( ... values ) ; return ( ( max - min ) / numOfKeyPaths ) * index + min ; } ; const generateDValues = ( data ) : Data => { log . info ( "start generate d values" ) ; const { numOfKeyPaths , loop } = data . parameters ; let inputKeyPathsParameters = data . keyPathsParameters ; let paths = [ ] ; let dValuesFrames = [ ] ; for ( let i = 0 ; i < numOfKeyPaths ; i ++ ) { log . info ( `generate key path number ${ i }` ) ; var pathParameters = { } ; for ( let key in inputKeyPathsParameters ) { if ( key === "groups" ) { if ( getType ( inputKeyPathsParameters [ key ] [ 0 ] ) === "object" ) pathParameters [ key ] = inputKeyPathsParameters [ key ] ; else pathParameters [ key ] = inputKeyPathsParameters [ key ] [ i ] ; log . debug ( "group param" , pathParameters [ key ] ) ; } else if ( typeof inputKeyPathsParameters [ key ] !== "object" ) { pathParameters [ key ] = inputKeyPathsParameters [ key ] ; } else { if ( inputKeyPathsParameters [ key ] . length === numOfKeyPaths ) pathParameters [ key ] = inputKeyPathsParameters [ key ] [ i ] ; else if ( inputKeyPathsParameters [ key ] . length === 2 ) pathParameters [ key ] = getValueFromRange ( inputKeyPathsParameters [ key ] , numOfKeyPaths , i ) ; else throw `Wrong '${ key }' parameter array at ${ i } key path` ; } } let path = pathLayer ( pathParameters ) ; paths [ i ] = path ; dValuesFrames [ i ] = path . d ; if ( loop ) { if ( loop === "linear" && i !== numOfKeyPaths - 1 ) { dValuesFrames [ ( numOfKeyPaths - 1 ) * 2 - i ] = path . d ; } else if ( loop === "circle" && i === numOfKeyPaths - 1 ) { dValuesFrames [ numOfKeyPaths ] = dValuesFrames [ 0 ] ; } } } const dValues = dValuesFrames . join ( ";" ) ; return { ... data , dValues , dValuesFrames } ; } ; export default generateDValues ;