import { createSelector } from '@reduxjs/toolkit' import { ActionAttribute } from '../actions/types' import { PipelineState } from '../pipeline/pipeline.slice' import { RootState } from '../store' import { VarDataType } from '../types' import { emptyAttribute, provideSystemProducedAttribute } from '../utils' const selectSelf = (state: RootState) => state /** * System produced attributes * @returns */ export const selectAllSystemProducedAttributes = (): ActionAttribute[] => { const execAttrs = [ { name: 'count', label: '$Entries Count', datatype: VarDataType.number, description: '$Execution total entries count', required: false, }, { name: 'index', label: '$Entries Index', datatype: VarDataType.number, description: '$Execution current entry index', required: false, }, ].map(attr => provideSystemProducedAttribute({ ...attr, relId: '$entries', }) ) const userAttrs = [ { id: '', name: 'index', label: '$User Content', datatype: VarDataType.number, description: '$Execution user-provided context', required: false, value: '', }, ].map(attr => provideSystemProducedAttribute({ ...attr, relId: '$user', }) ) return [execAttrs, userAttrs, emptyAttribute].flat() } /** * Select all produced by actions attributes */ export const selectAllActionRelationsProducedAttributes = () => createSelector(selectSelf, (state: RootState) => state.pipeline .reduce((acc: ActionAttribute[][], pipeline: PipelineState) => { const [relation] = state.relations.filter(rel => rel.id === pipeline.id) const [action] = state.actions.filter( action => action.name === relation.actName ) const attributes = Object.entries(pipeline.produce).flatMap( ([globalId, localId]) => { const attr = action.rAttributes.filter( attr => attr.id === localId )[0] return { ...attr, globalId } } ) return [...acc, attributes] }, []) .flat() ) /** * Select all produced by actions attributes before action * @param relId * @returns */ export const selectAllProducedAttributesBeforeActionRelation = ( relId: string ) => createSelector(selectSelf, (state: RootState) => { const { actions, relations, pipeline } = state const index = relations.findIndex(({ rel }) => rel.includes(relId)) return pipeline .slice(0, index + 1) .map(({ id, produce }) => { const [relation] = relations.filter(rel => rel.id === id) const [action] = actions.filter( action => action.name === relation.actName ) const attributes = Object.entries(produce).flatMap( ([globalId, localId]) => { const attr = action.rAttributes.filter( attr => attr.id === localId )[0] return { ...attr, globalId } } ) return attributes }) .flat() })