import shallowEqual from '@tinkoff/utils/is/shallowEqual' ; import strictEqual from '@tinkoff/utils/is/strictEqual' ; import { connectAdvanced } from './connectAdvanced' ; import { mapContextToPropsFactories as defaultMapContextToPropsFactories } from './toProps/mapContextToProps' ; import { mapStateToPropsFactories as defaultMapStateToPropsFactories } from './toProps/mapStateToProps' ; import { mergePropsFactories as defaultMergePropsFactories } from './toProps/mergeProps' ; import { finalPropsSelectorFactory as defaultSelectorFactory } from './selectorFactory' ; import { ConnectContext } from './context' ; import { scheduling } from './scheduling' ; import type { ConsumerContext } from './types' ; import type { StoreClass } from '../typings' ; import type { MapContextToProps , MapStateToProps , MergeProps } from './toProps/types' ; function match < T > ( arg , factories , name ) { for ( let i = factories . length - 1 ; i >= 0 ; i -- ) { const result = factories [ i ] ( arg ) ; if ( result ) { return result ; } } return ( context , options ) => { throw new Error ( `Invalid value of type ${ typeof arg } for ${ name } argument when connecting component ${ options . wrappedComponentName }.` ) ; } ; } export function createConnect ( { connectHOC = connectAdvanced , mapStateToPropsFactories = defaultMapStateToPropsFactories , mapContextToPropsFactories = defaultMapContextToPropsFactories , mergePropsFactories = defaultMergePropsFactories , selectorFactory = defaultSelectorFactory , schedule = scheduling ( ) , } = { } ) { return ( stores , mapStateToProps , mapContextToProps ? , mergeProps ? , { pure = true , areStatesEqual = strictEqual , areOwnPropsEqual = shallowEqual , areStatePropsEqual = shallowEqual , areMergedPropsEqual = shallowEqual , ... extraOptions } = { } ) => { const initMapStateToProps = match ( mapStateToProps , mapStateToPropsFactories , 'mapStateToProps' ) ; const initMapContextToProps = match ( mapContextToProps , mapContextToPropsFactories , 'mapContextToProps' ) ; const initMergeProps = match ( mergeProps , mergePropsFactories , 'mergeProps' ) ; return connectHOC ( selectorFactory , { stores , initMapStateToProps , initMapContextToProps , initMergeProps , pure , areStatesEqual , areOwnPropsEqual , areStatePropsEqual , areMergedPropsEqual , schedule , methodName : 'connect' , getDisplayName : ( name ) => `Connect(${ name })` , shouldHandleStateChanges : stores && stores . length > 0 , ... extraOptions , } ) ; } ; } const connect = createConnect ( ) ; const { Consumer } = ConnectContext ; export default connect ; export { connect } ; export { Provider } from './Provider' ; export { Consumer } ; export { ConsumerContext } from './types' ; export * from './hooks' ;