import { createSelector } from '@reduxjs/toolkit' import { RootState } from '../store' import { parseAttributeGlobalId } from '../utils' const selectSelf = (state: RootState) => state /** * Select all action attributes consumed, produced and parameters * @param {string} relId * @returns {object} { consume: [], produce: [], parameters: [] } */ export const selectActionRelationAttributes = (relId: string) => createSelector( selectSelf, (state: RootState) => state.relations .filter(rel => rel.id === relId) .flatMap(rel => state.actions.filter(action => action.name === rel.actName) ) .map(action => { const pl = state.pipeline.filter(pl => pl.id === relId)[0] const pAttributes = Object.entries(pl.params).map( ([localId, value]) => { const { name } = parseAttributeGlobalId(localId) const attr = action.pAttributes.filter( attr => attr.name.toLowerCase() === name.toLowerCase() )[0] return { ...attr, value } } ) const lAttributes = Object.entries(pl.consume).map(([localId]) => { const attr = action.lAttributes.filter( attr => attr.id === localId )[0] return { ...attr } }) const rAttributes = Object.entries(pl.produce).map(([, localId]) => { const attr = action.rAttributes.filter( attr => attr.id === localId )[0] return { ...attr } }) return { lAttributes, pAttributes, rAttributes, } })[0] ) export const selectAction = (relId: string) => createSelector( selectSelf, (state: RootState) => state.relations .filter(rel => rel.id === relId) .flatMap(rel => state.actions.filter(action => action.name === rel.actName) )[0] )