import { useState, useCallback } from 'react' import * as api from '@/services/api' import type { ProposedDayApplyItem, PlanApplyResponse } from '@/types' interface UsePlanApplyResult { response: PlanApplyResponse | null isLoading: boolean error: string | null apply: (workspaceId: string, days: ProposedDayApplyItem[], replace: boolean) => Promise } /** * Applies a plan (persists proposed days as real planned days). * Returns the full response including collision info, rejections, and created counts. */ export function usePlanApply(): UsePlanApplyResult { const [response, setResponse] = useState(null) const [isLoading, setIsLoading] = useState(false) const [error, setError] = useState(null) const apply = useCallback( async (workspaceId: string, days: ProposedDayApplyItem[], replace: boolean) => { setIsLoading(true) setError(null) try { const result = await api.applyPlan(workspaceId, days, replace) setResponse(result) return result } catch (err: unknown) { const message = err instanceof Error ? err.message : 'Kunde inte använda plan' setError(message) setResponse(null) throw err } finally { setIsLoading(false) } }, [], ) return { response, isLoading, error, apply } }