'use client';
import { useCallback, useEffect, useState } from 'react';
import { toast } from 'sonner';
import { cmsApi } from '../../lib/api.js';
const REPO_PATTERN = /^[a-zA-Z0-9._-]+\/[a-zA-Z0-9._-]+$/;
/** Admin/Owner may manage updates (matches the server's `requireAdminScope`). */
export function canManageUpdates(role) {
    const r = (role ?? '').toUpperCase();
    return r === 'ADMIN' || r === 'OWNER';
}
/**
 * State + actions for the Settings → Updates tab. Owns the update-status check,
 * the GitHub-integration config form (with dirty tracking for the shared save
 * bar), and the "Create Update PR" action. All writes are gated on `canManage`,
 * which the server confirms in the check response.
 */
export function useUpdates({ role }) {
    const [info, setInfo] = useState(null);
    const [checking, setChecking] = useState(false);
    const [hasChecked, setHasChecked] = useState(false);
    const [error, setError] = useState('');
    const [applying, setApplying] = useState(false);
    const [prResult, setPrResult] = useState(null);
    const [form, setForm] = useState({ githubRepo: '', githubToken: '' });
    const [baseline, setBaseline] = useState({ githubRepo: '', githubToken: '' });
    const [saveState, setSaveState] = useState('idle');
    // Optimistic until the check response confirms the server-side permission.
    const canManage = info?.canManage ?? canManageUpdates(role);
    const check = useCallback(async () => {
        setChecking(true);
        setError('');
        setPrResult(null);
        const res = await cmsApi('/updates/check');
        if (res.error) {
            setError(res.error);
        }
        else if (res.data) {
            setInfo(res.data);
            const repo = res.data.githubRepo ?? '';
            setForm({ githubRepo: repo, githubToken: '' });
            setBaseline({ githubRepo: repo, githubToken: '' });
        }
        setChecking(false);
        setHasChecked(true);
    }, []);
    useEffect(() => {
        check();
    }, [check]);
    const repoError = form.githubRepo.trim() && !REPO_PATTERN.test(form.githubRepo.trim())
        ? 'Use the owner/repo format (e.g. actuate-media/my-site).'
        : '';
    const hasErrors = Boolean(repoError);
    const dirty = form.githubRepo !== baseline.githubRepo || form.githubToken.trim() !== '';
    const setField = useCallback((key, value) => {
        setForm((f) => ({ ...f, [key]: value }));
        setSaveState('idle');
    }, []);
    const save = useCallback(async () => {
        if (hasErrors || !dirty)
            return;
        setSaveState('saving');
        const body = {};
        if (form.githubToken.trim())
            body.githubToken = form.githubToken.trim();
        if (form.githubRepo.trim())
            body.githubRepo = form.githubRepo.trim();
        const res = await cmsApi('/updates/config', { method: 'PUT', body: JSON.stringify(body) });
        if (res.error) {
            setSaveState('error');
            toast.error(res.error);
            return;
        }
        setSaveState('success');
        toast.success('GitHub configuration saved and encrypted.');
        const savedRepo = form.githubRepo.trim();
        setBaseline({ githubRepo: savedRepo, githubToken: '' });
        setForm({ githubRepo: savedRepo, githubToken: '' });
        setInfo((prev) => prev
            ? {
                ...prev,
                githubRepo: savedRepo,
                hasGithubToken: prev.hasGithubToken || Boolean(body.githubToken),
            }
            : prev);
    }, [dirty, form.githubRepo, form.githubToken, hasErrors]);
    const reset = useCallback(() => {
        setForm({ ...baseline });
        setSaveState('idle');
    }, [baseline]);
    const apply = useCallback(async () => {
        if (!info?.latest || !canManage)
            return;
        setApplying(true);
        const res = await cmsApi('/updates/apply', {
            method: 'POST',
            body: JSON.stringify({ targetVersion: info.latest }),
        });
        if (res.error) {
            toast.error(res.error);
        }
        else if (res.data) {
            setPrResult(res.data);
            toast.success('Update PR created.');
        }
        setApplying(false);
    }, [info, canManage]);
    return {
        info,
        checking,
        hasChecked,
        error,
        applying,
        prResult,
        canManage,
        form,
        setField,
        dirty,
        repoError,
        hasErrors,
        saveState,
        save,
        reset,
        check,
        apply,
    };
}
//# sourceMappingURL=useUpdates.js.map