import { defineStore } from 'pinia'; import { computed, ref } from 'vue'; import { CurrentMcadState, InitialMcadState } from '@3cr/types-ts'; import { inflateInitialMcadObjectState } from '@/functions/modelHelper'; import { parseDataOverlay } from '@/functions/parsers/parseDataOverlay'; import { parseMcadEvent } from '@/functions/parsers/parseMcadEvent'; import { DataOverlayMcad, DataOverlayMcadRaw, McadObjectInteraction, McadObjectInteractionRaw, } from '@3cr/viewer-types-ts'; function toMcad(data: DataOverlayMcadRaw): DataOverlayMcad { return parseDataOverlay(data) as DataOverlayMcad; } function toMcadEvent(data: McadObjectInteractionRaw): McadObjectInteraction { return parseMcadEvent(data); } export const useMcadStore = defineStore('mcad', () => { const state = ref(inflateInitialMcadObjectState()); const event = ref(null); const sortBy = ref<{ key: string; order: 'asc' | 'desc' }[]>([ { key: 'title', order: 'asc' }, ]); const mcads = computed(() => { return state.value.Models.Models.map(toMcad); }); const childKeys = computed(() => { return mcads.value.map((x) => x.Children).flat(); }); const mcadEvent = computed(() => { const value = event.value; return value ? toMcadEvent(value) : null; }); function setInitialMcadState(message: string): void { state.value = JSON.parse(message) as InitialMcadState; } function setMcadState(message: string): void { state.value = { Version: '1.1.0', Models: JSON.parse(message) as CurrentMcadState, }; } function setMcadEvent(message: string): void { event.value = JSON.parse(message) as McadObjectInteractionRaw; } return { state, event, mcads, mcadEvent, setInitialMcadState, setMcadState, setMcadEvent, childKeys, sortBy, }; });