import { customAlphabet } from 'nanoid' import { createStore, RootState, selectAction, selectActionRelationAttributes, selectActionsCost, selectActionsTime, selectAllActionRelationsAsExecution, selectAllActionRelationsProducedAttributes, selectAllMissedActionRelations, selectAllProducedAttributesBeforeActionRelation, selectAllSystemProducedAttributes, selectAllViewsAsOutput, selectNormalizedActionRelationsSequence, selectNormalizedState, } from '..' import { ActionState, updateAction } from '../actions/actions.slice' import { MetadataState, setMetadata, updateMetadata, } from '../metadata/metadata.slice' import { LabelsState, setLabels, updateLabels } from '../pipeline/labels.slice' import { addView, removeView, updateView, ViewState, } from '../views/views.slice' import { attach } from './attach' import { autoAssignLeftAttributes, canConnect, connect } from './connect' import { disconnect } from './disconnect' import { remove } from './remove' import { update } from './update' export const createRelId = (len = 7) => customAlphabet( '0123456789' + 'abcdefghijklmnopqrstuvwxyz' + 'abcdefghijklmnopqrstuvwxyz'.toUpperCase(), len )(len) export const create = (snapshot?: RootState) => { const store = createStore(snapshot) return { // common getState: () => store.getState(), subscribe: (listener: (state: RootState) => void) => store.subscribe(() => listener(store.getState())), // mutators setMetadata: (metadata: MetadataState) => store.dispatch(setMetadata(metadata)), updateMetadata: (metadata: Partial) => store.dispatch(updateMetadata(metadata)), // labels setLabels: (labels: LabelsState) => store.dispatch(setLabels(labels)), updateLabels: (labels: LabelsState) => store.dispatch(updateLabels(labels)), // views addView: (view: ViewState) => store.dispatch(addView(view)), updateView: (view: ViewState) => store.dispatch(updateView(view)), removeView: (view: ViewState) => store.dispatch(removeView(view)), // actions & relations updateAction: (payload: Partial) => { store.dispatch(updateAction(payload)) }, // Relations only attach: attach(store), disconnect: disconnect(store), update: update(store), connect: connect(store), canConnect: canConnect(store), remove: remove(store), autoAssignLeftAttributes: autoAssignLeftAttributes(store), // selectors selectActionsCost: () => selectActionsCost()(store.getState()), selectActionsTime: () => selectActionsTime()(store.getState()), selectAction: (id: string) => selectAction(id)(store.getState()), selectActionAttributes: (id: string) => selectActionRelationAttributes(id)(store.getState()), selectAllActionProducedAttributes: () => selectAllActionRelationsProducedAttributes()(store.getState()), selectAllProducedAttributesBeforeAction: (id: string) => selectAllProducedAttributesBeforeActionRelation(id)(store.getState()), selectAllSystemProducedAttributes: () => selectAllSystemProducedAttributes(), selectNormalizedRelationsSequence: () => selectNormalizedActionRelationsSequence()(store.getState()), selectNormalizedState: () => selectNormalizedState()(store.getState()), selectAllMissedActionRelations: () => selectAllMissedActionRelations()(store.getState()), selectAllActionsAsExecution: () => selectAllActionRelationsAsExecution()(store.getState()), selectAllViewsAsOutput: () => selectAllViewsAsOutput()(store.getState()), } }