import { create } from 'zustand'; export const useTourStore = create((set, get) => ({ currentTour: null, legs: [], publicTours: [], userRole: null, activeLegId: null, mapCenter: [10.7769, 106.7009], setTour: (tour) => set({ currentTour: tour }), updateLegs: (legs) => set({ legs }), setActiveLegId: (id) => set({ activeLegId: id }), setMapCenter: (pos) => set({ mapCenter: pos }), fetchTour: async (id) => { const API_BASE = `http://${window.location.hostname}:3001`; const response = await fetch(`${API_BASE}/api/v1/tours/${id}`); const data = await response.json(); const role = data.participants?.[0]?.role || 'VIEWER_ONLY'; const legs = data.legs || []; set({ currentTour: data, legs: legs, userRole: role, activeLegId: legs.length > 0 ? legs[0].id : null }); }, fetchPublicTours: async () => { const API_BASE = `http://${window.location.hostname}:3001`; const response = await fetch(`${API_BASE}/api/v1/tours/explore`); const data = await response.json(); set({ publicTours: data }); }, createTour: async (tourData) => { const API_BASE = `http://${window.location.hostname}:3001`; const response = await fetch(`${API_BASE}/api/v1/tours`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${localStorage.getItem('token')}` }, body: JSON.stringify(tourData), }); return await response.json(); }, updateTour: async (id, data) => { const API_BASE = `http://${window.location.hostname}:3001`; const response = await fetch(`${API_BASE}/api/v1/tours/${id}`, { method: 'PATCH', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${localStorage.getItem('token')}` }, body: JSON.stringify(data), }); if (!response.ok) throw new Error('Lỗi khi cập nhật Tour'); get().fetchPublicTours(); }, deleteTour: async (id) => { const API_BASE = `http://${window.location.hostname}:3001`; const response = await fetch(`${API_BASE}/api/v1/tours/${id}`, { method: 'DELETE', headers: { 'Authorization': `Bearer ${localStorage.getItem('token')}` }, }); if (!response.ok) { const data = await response.json(); throw new Error(data.message || 'Lỗi khi xóa Tour'); } if (get().currentTour?.id === id) set({ currentTour: null }); get().fetchPublicTours(); }, addLeg: async (tourId, data) => { const API_BASE = `http://${window.location.hostname}:3001`; const response = await fetch(`${API_BASE}/api/v1/tours/${tourId}/legs`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${localStorage.getItem('token')}` }, body: JSON.stringify(data), }); if (!response.ok) throw new Error('Lỗi khi thêm chặng'); get().fetchTour(tourId); }, updateLeg: async (legId, data) => { const API_BASE = `http://${window.location.hostname}:3001`; const response = await fetch(`${API_BASE}/api/v1/legs/${legId}`, { method: 'PATCH', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${localStorage.getItem('token')}` }, body: JSON.stringify(data), }); if (!response.ok) throw new Error('Lỗi khi cập nhật chặng'); const { currentTour } = get(); if (currentTour) get().fetchTour(currentTour.id); }, deleteLeg: async (legId) => { const API_BASE = `http://${window.location.hostname}:3001`; const response = await fetch(`${API_BASE}/api/v1/legs/${legId}`, { method: 'DELETE', headers: { 'Authorization': `Bearer ${localStorage.getItem('token')}` }, }); const result = await response.json(); if (!response.ok) { throw new Error(result.message || 'Lỗi khi xóa chặng'); } const { currentTour } = get(); if (currentTour) get().fetchTour(currentTour.id); }, addLocation: async (tourId, locationData) => { const API_BASE = `http://${window.location.hostname}:3001`; const response = await fetch(`${API_BASE}/api/v1/tours/${tourId}/locations`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${localStorage.getItem('token')}` }, body: JSON.stringify(locationData), }); if (!response.ok) throw new Error('Lỗi khi thêm địa điểm'); get().fetchTour(tourId); }, optimizeRouting: async (legId) => { const API_BASE = `http://${window.location.hostname}:3001`; const response = await fetch(`${API_BASE}/api/v1/routing/optimize/${legId}`, { method: 'POST' }); const { locations, totalDistance } = await response.json(); const { currentTour } = get(); if (currentTour) { const updatedLegs = currentTour.legs.map((l) => l.id === legId ? { ...l, locations: locations, totalDistance: totalDistance } : l); set({ currentTour: { ...currentTour, legs: updatedLegs }, legs: updatedLegs }); } }, })); //# sourceMappingURL=useTourStore.js.map