'use client';
/**
 * useSyncAction
 *
 * Wraps POST /api/applications/:id/git/sync as a hook with local state
 * (idle / running / failed / done). Surfaces server errors verbatim
 * because the operation log drawer covers the full diagnostic — this
 * hook only needs enough state to render the SmartDeployButton's label.
 */
import { useCallback, useState } from 'react';
export function useSyncAction(applicationId) {
    const [state, setState] = useState({ kind: 'idle' });
    const sync = useCallback(async (message) => {
        setState({ kind: 'running' });
        try {
            const res = await fetch(`/api/applications/${applicationId}/git/sync`, {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify(message ? { message } : {}),
            });
            if (!res.ok) {
                const body = (await res.json().catch(() => ({})));
                setState({
                    kind: 'failed',
                    error: body.error ?? `Save failed (HTTP ${res.status})`,
                    code: body.code ?? null,
                });
                return;
            }
            const dto = (await res.json());
            setState({
                kind: 'done',
                committed: dto.committed,
                pushed: dto.pushed,
                headSha: dto.headSha,
            });
        }
        catch (err) {
            setState({
                kind: 'failed',
                error: err instanceof Error ? err.message : 'Network error contacting server',
                code: null,
            });
        }
    }, [applicationId]);
    const reset = useCallback(() => {
        setState({ kind: 'idle' });
    }, []);
    return { state, sync, reset };
}
