import { TreeNode , RawNode , InputMergedKeys , Key , GetNonLeafKeysOptions , GetChildren } from './interface' ; export function toArray < T > ( arg ) { if ( Array . isArray ( arg ) ) return arg as any ; return [ arg ] as any ; } export const TRAVERSE_COMMAND = { STOP : 'STOP' } ; export function traverseWithCb < R , G , I > ( treeNode , callback ) { const command = callback ( treeNode ) ; if ( treeNode . children !== undefined && command !== TRAVERSE_COMMAND . STOP ) { treeNode . children . forEach ( ( childNode ) => traverseWithCb ( childNode , callback ) ) ; } } export function getNonLeafKeys < R , G , I > ( treeNodes , options = { } ) { const { preserveGroup = false } = options ; const keys = [ ] ; const cb = preserveGroup ? ( node ) => { if ( ! node . isLeaf ) { keys . push ( node . key ) ; traverse ( node . children as Array < TreeNode < R , G , I > > ) ; } } : ( node ) => { if ( ! node . isLeaf ) { if ( ! node . isGroup ) keys . push ( node . key ) ; traverse ( node . children as Array < TreeNode < R , G , I > > ) ; } } ; function traverse ( nodes ) { nodes . forEach ( cb ) ; } traverse ( treeNodes ) ; return keys ; } export function isLeaf < R = RawNode , G = R , I = R > ( rawNode , getChildren ) { const { isLeaf } = rawNode as any ; if ( isLeaf !== undefined ) return isLeaf ; else if ( ! getChildren ( rawNode ) ) return true ; return false ; } export function defaultGetChildren < R , G , I > ( node ) { return ( node as any ) . children ; } export function defaultGetKey ( node ) { return ( node as any ) . key ; } export function isIgnored ( ) { return false ; } export function isShallowLoaded < R = RawNode , G = R , I = R > ( rawNode , getChildren ) { const { isLeaf } = rawNode as any ; if ( isLeaf === false && ! Array . isArray ( getChildren ( rawNode ) ) ) return false ; return true ; } export function isDisabled ( rawNode ) { return rawNode . disabled === true ; } export function isExpilicitlyNotLoaded < R = RawNode , G = R , I = R > ( rawNode , getChildren ) { return ( ( rawNode as any ) . isLeaf === false && ! Array . isArray ( getChildren ( rawNode ) ) ) ; } export function isNodeInvalid < R = RawNode , G = R , I = R > ( rawNode , getChildren ) { if ( ( rawNode as any ) . isLeaf === true ) { const children = getChildren ( rawNode ) ; if ( Array . isArray ( children ) && children . length > 0 ) return true ; } return false ; } export function unwrapCheckedKeys ( result ? ) { if ( result === undefined || result === null ) return [ ] ; if ( Array . isArray ( result ) ) return result ; return result . checkedKeys ?? [ ] ; } export function unwrapIndeterminateKeys ( result ? ) { if ( result === undefined || result === null || Array . isArray ( result ) ) { return [ ] ; } return result . indeterminateKeys ?? [ ] ; } export function merge ( originalKeys , keysToAdd ) { const set = new Set ( originalKeys ) ; keysToAdd . forEach ( ( key ) => { if ( ! set . has ( key ) ) { set . add ( key ) ; } } ) ; return Array . from ( set ) ; } export function minus ( originalKeys , keysToRemove ) { const set = new Set ( originalKeys ) ; keysToRemove . forEach ( ( key ) => { if ( set . has ( key ) ) { set . delete ( key ) ; } } ) ; return Array . from ( set ) ; } export function isGroup ( rawNode ) { return rawNode ?. type === 'group' ; } export type IndexGetter = ( key : Key ) => number | null ; export function createIndexGetter < R , G , I > ( treeNodes ) { const map = new Map < Key , number > ( ) ; treeNodes . forEach ( ( treeNode , i ) => { map . set ( treeNode . key , i ) ; } ) ; return ( key ) => map . get ( key ) ?? null ; }