import { create } from 'zustand' import type { VesselPosition } from '../../vessel-map' interface PositionState { /** MMSI → 위치 맵 */ positions: Map /** 선택된 MMSI (팝업 표시용) */ selectedMmsi: string | null /** 선종별 가시성 필터 */ kindVisibility: Record /** 위치 데이터 전체 교체 */ setPositions: (list: VesselPosition[]) => void /** 선박 선택/해제 */ selectVessel: (mmsi: string | null) => void /** 특정 선종 토글 */ toggleKindVisibility: (kindCode: string) => void /** 전체 선종 표시/숨김 */ setAllKindVisibility: (visible: boolean) => void /** 가시성 필터 적용된 선박 목록 */ getVisiblePositions: () => VesselPosition[] } export const usePositionStore = create((set, get) => ({ positions: new Map(), selectedMmsi: null, kindVisibility: {}, setPositions: (list) => { const map = new Map() for (const p of list) { map.set(p.mmsi, p) } set({ positions: map }) }, selectVessel: (mmsi) => set({ selectedMmsi: mmsi }), toggleKindVisibility: (kindCode) => set((state) => ({ kindVisibility: { ...state.kindVisibility, [kindCode]: !(state.kindVisibility[kindCode] ?? true), }, })), setAllKindVisibility: (visible) => set((state) => { const next: Record = {} for (const code of Object.keys(state.kindVisibility)) { next[code] = visible } return { kindVisibility: next } }), getVisiblePositions: () => { const { positions, kindVisibility } = get() const result: VesselPosition[] = [] for (const p of positions.values()) { const code = p.shipKindCode || '' if (kindVisibility[code] === false) continue result.push(p) } return result }, }))