/** * WordPress dependencies */ import { createSelector } from '@safe-wordpress/data'; /** * External dependencies */ import { keys } from 'lodash'; import { EMPTY_ARRAY } from '@nelio-content/constants'; import { isAutoSelectedUuid, isEmpty, createIndexedSelector, } from '@nelio-content/utils'; import type { AutoSelectedUuid, SocialNetworkName, Uuid, } from '@nelio-content/types'; /** * Internal dependencies */ import type { State } from '../../types'; export const getSelectedSocialProfiles = createSelector( ( state: State ): ReadonlyArray< Uuid > | readonly [ AutoSelectedUuid ] => { const result = state.attributes.profileIds.all || EMPTY_ARRAY; if ( result.length ) { return result; } const { fallback } = state.attributes.profileIds; return fallback ? [ fallback ] : result; }, ( state: State ) => [ state.attributes.profileIds ] ); export const getSelectedSocialProfilesInNetwork = createIndexedSelector( ( state: State, network: SocialNetworkName ): ReadonlyArray< Uuid > | readonly [ AutoSelectedUuid ] => { const fallback = getSelectedSocialProfiles( state )[ 0 ]; if ( isAutoSelectedUuid( fallback ) ) { return [ fallback ]; } return state.attributes.profileIds.byNetwork[ network ] || EMPTY_ARRAY; }, ( state: State, network: SocialNetworkName ) => ( { key: network, dependants: [ getSelectedSocialProfiles( state )[ 0 ], state.attributes.profileIds.byNetwork[ network ], ], } ) ); export const getSelectedSocialNetworks = createSelector( ( state: State ): ReadonlyArray< SocialNetworkName > => { const networks = keys( state.attributes.profileIds.byNetwork ) as ReadonlyArray< SocialNetworkName >; return networks.filter( ( network ) => ! isEmpty( state.attributes.profileIds.byNetwork[ network ] ) ); }, ( state: State ) => [ state.attributes.profileIds.byNetwork ] );