import { create } from 'zustand'; import { immer } from 'zustand/middleware/immer'; interface ArmBotStore { armBots: ArmBotStatus[]; addArmBot: (productId: string, event: RoboticArmEventSchema) => void; removeArmBot: (modelUuid: string) => void; updateArmBot: ( modelUuid: string, updates: Partial> ) => void; clearArmBots: () => void; addCurrentAction: (modelUuid: string, actionUuid: string, materialType: string, materialId: string) => void; removeCurrentAction: (modelUuid: string) => void; addAction: (modelUuid: string, action: RoboticArmPointSchema['actions'][number]) => void; removeAction: (modelUuid: string, actionUuid: string) => void; updateStartPoint: (modelUuid: string, actionUuid: string, startPoint: [number, number, number] | null) => void; updateEndPoint: (modelUuid: string, actionUuid: string, endPoint: [number, number, number] | null) => void; setArmBotActive: (modelUuid: string, isActive: boolean) => void; setArmBotState: (modelUuid: string, newState: ArmBotStatus['state']) => void; incrementActiveTime: (modelUuid: string, incrementBy: number) => void; incrementIdleTime: (modelUuid: string, incrementBy: number) => void; getArmBotById: (modelUuid: string) => ArmBotStatus | undefined; getArmBotsByProduct: (productId: string) => ArmBotStatus[]; getArmBotsByState: (state: string) => ArmBotStatus[]; getActiveArmBots: () => ArmBotStatus[]; getIdleArmBots: () => ArmBotStatus[]; getArmBotsByCurrentAction: (actionUuid: string) => ArmBotStatus[]; } export const useArmBotStore = create()( immer((set, get) => ({ armBots: [], addArmBot: (productId, event) => { set((state) => { const exists = state.armBots.some(a => a.modelUuid === event.modelUuid); if (!exists) { state.armBots.push({ ...event, productId, isActive: false, idleTime: 0, activeTime: 0, state: 'idle', }); } }); }, removeArmBot: (modelUuid) => { set((state) => { state.armBots = state.armBots.filter(a => a.modelUuid !== modelUuid); }); }, updateArmBot: (modelUuid, updates) => { set((state) => { const armBot = state.armBots.find(a => a.modelUuid === modelUuid); if (armBot) { Object.assign(armBot, updates); } }); }, clearArmBots: () => { set((state) => { state.armBots = []; }); }, addCurrentAction: (modelUuid, actionUuid, materialType, materialId) => { set((state) => { const armBot = state.armBots.find(a => a.modelUuid === modelUuid); if (armBot) { const action = armBot.point.actions.find(a => a.actionUuid === actionUuid); if (action) { armBot.currentAction = { actionUuid: action.actionUuid, actionName: action.actionName, materialType: materialType, materialId: materialId }; } } }); }, removeCurrentAction: (modelUuid) => { set((state) => { const armBot = state.armBots.find(a => a.modelUuid === modelUuid); if (armBot) { armBot.currentAction = undefined; } }); }, addAction: (modelUuid, action) => { set((state) => { const armBot = state.armBots.find(a => a.modelUuid === modelUuid); if (armBot) { armBot.point.actions.push(action); } }); }, removeAction: (modelUuid, actionUuid) => { set((state) => { const armBot = state.armBots.find(a => a.modelUuid === modelUuid); if (armBot) { armBot.point.actions = armBot.point.actions.filter(a => a.actionUuid !== actionUuid); } }); }, updateStartPoint: (modelUuid, actionUuid, startPoint) => { set((state) => { const armBot = state.armBots.find(a => a.modelUuid === modelUuid); if (armBot) { const action = armBot.point.actions.find(a => a.actionUuid === actionUuid); if (action) { action.process.startPoint = startPoint; } } }); }, updateEndPoint: (modelUuid, actionUuid, endPoint) => { set((state) => { const armBot = state.armBots.find(a => a.modelUuid === modelUuid); if (armBot) { const action = armBot.point.actions.find(a => a.actionUuid === actionUuid); if (action) { action.process.endPoint = endPoint; } } }); }, setArmBotActive: (modelUuid, isActive) => { set((state) => { const armBot = state.armBots.find(a => a.modelUuid === modelUuid); if (armBot) { armBot.isActive = isActive; } }); }, setArmBotState: (modelUuid, newState) => { set((state) => { const armBot = state.armBots.find(a => a.modelUuid === modelUuid); if (armBot) { armBot.state = newState; } }); }, incrementActiveTime: (modelUuid, incrementBy) => { set((state) => { const armBot = state.armBots.find(a => a.modelUuid === modelUuid); if (armBot) { armBot.activeTime += incrementBy; } }); }, incrementIdleTime: (modelUuid, incrementBy) => { set((state) => { const armBot = state.armBots.find(a => a.modelUuid === modelUuid); if (armBot) { armBot.idleTime += incrementBy; } }); }, getArmBotById: (modelUuid) => { return get().armBots.find(a => a.modelUuid === modelUuid); }, getArmBotsByProduct: (productId) => { return get().armBots.filter(a => a.productId === productId); }, getArmBotsByState: (state) => { return get().armBots.filter(a => a.state === state); }, getActiveArmBots: () => { return get().armBots.filter(a => a.isActive); }, getIdleArmBots: () => { return get().armBots.filter(a => !a.isActive && a.state === 'idle'); }, getArmBotsByCurrentAction: (actionUuid) => { return get().armBots.filter(a => a.currentAction?.actionUuid === actionUuid); } })) );