import { create } from "zustand"; interface SimulationUsageRecord { activeTime: number; isActive: boolean; idleTime: number; type: "roboticArm" | "vehicle" | "transfer" | "storageUnit" | "crane" | "human" | "machine"; assetId: string; } // Product → holds multiple usage records interface ProductSimulation { productId: string; data: SimulationUsageRecord[]; } // Version → holds multiple products interface VersionSimulation { versionId: string; products: ProductSimulation[]; } // Project → holds multiple versions interface ProjectSimulation { projectId: string | undefined; versions: VersionSimulation[]; } // or same file interface SimulationManagerStore { simulationRecords: ProjectSimulation[]; setSimulationRecords: (data: ProjectSimulation[]) => void; addSimulationRecord: (projectId: string | undefined, versionId: string, productId: string, record: SimulationUsageRecord) => void; resetProductRecords: (projectId: string, versionId: string, productId: string) => void; getProjectById: (projectId: string | undefined) => ProjectSimulation | undefined; getVersionById: (projectId: string | undefined, versionId: string) => VersionSimulation | undefined; getProductById: (projectId: string | undefined, versionId: string, productId: string) => ProductSimulation | undefined; } export const useSimulationManager = create((set, get) => ({ simulationRecords: [], addSimulationRecord: (projectId, versionId, productId, record) => set((state) => { const projects = state.simulationRecords.map((project) => { if (project.projectId !== projectId) return project; return { ...project, versions: project.versions.map((version) => { if (version.versionId !== versionId) return version; return { ...version, products: version.products.map((product) => (product.productId === productId ? { ...product, data: [...product.data, record] } : product)), }; }), }; }); // If project doesn't exist, create it if (!state.simulationRecords.find((p) => p.projectId === projectId)) { projects.push({ projectId, versions: [ { versionId, products: [{ productId, data: [record] }], }, ], }); } else { const project = projects.find((p) => p.projectId === projectId)!; if (!project.versions.find((v) => v.versionId === versionId)) { project.versions.push({ versionId, products: [{ productId, data: [record] }], }); } else { const version = project.versions.find((v) => v.versionId === versionId)!; if (!version.products.find((p) => p.productId === productId)) { version.products.push({ productId, data: [record] }); } } } return { simulationRecords: projects }; }), resetProductRecords: (projectId, versionId, productId) => set((state) => { const projects = state.simulationRecords.map((project) => { if (project.projectId !== projectId) return project; return { ...project, versions: project.versions.map((version) => { if (version.versionId !== versionId) return version; return { ...version, products: version.products.map((product) => (product.productId === productId ? { ...product, data: [] } : product)), }; }), }; }); return { simulationRecords: projects }; }), setSimulationRecords: (data) => { set({ simulationRecords: data }); }, getProjectById: (projectId: string | undefined) => { return get().simulationRecords.find((p: ProjectSimulation) => p.projectId === projectId); }, getVersionById: (projectId: string | undefined, versionId: string) => { const project = get().simulationRecords.find((p: ProjectSimulation) => p.projectId === projectId); return project?.versions.find((v: VersionSimulation) => v.versionId === versionId); }, getProductById: (projectId: string | undefined, versionId: string, productId: string) => { const project = get().simulationRecords.find((p: ProjectSimulation) => p.projectId === projectId); const version = project?.versions.find((v: VersionSimulation) => v.versionId === versionId); return version?.products.find((p: ProductSimulation) => p.productId === productId); }, })); ///////////////////////////