'use client';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useState, useTransition } from 'react';
import { toast } from 'sonner';
import { enableBedrock as defaultEnableBedrock, } from '../app/actions/enable-bedrock.action.js';
import { Label } from './ui/label.js';
import { Switch } from './ui/switch.js';
export function BedrockMemoryToggle({ applicationId, initialEnabled, enableActionOverride, }) {
    const [enabled, setEnabled] = useState(initialEnabled);
    const [failure, setFailure] = useState(null);
    const [isPending, startTransition] = useTransition();
    const action = enableActionOverride ?? defaultEnableBedrock;
    function handleToggle(next) {
        // The M ships enable-only — disabling is not yet exposed because the
        // underlying use case does not implement teardown. Ignore off-clicks
        // so the switch acts as a one-way commit.
        if (!next)
            return;
        if (enabled)
            return;
        setEnabled(true);
        setFailure(null);
        startTransition(async () => {
            const result = await action(applicationId);
            if (!result.ok) {
                setEnabled(false);
                setFailure({ code: result.code, remediation: result.remediation });
                toast.error(result.remediation);
                return;
            }
            setEnabled(result.bedrockEnabled);
        });
    }
    return (_jsxs("div", { className: "flex flex-col gap-1", "data-testid": "bedrock-memory-toggle-container", "data-pending": isPending ? 'true' : 'false', children: [_jsxs("div", { className: "flex items-center justify-between", children: [_jsxs("div", { className: "flex flex-col", children: [_jsx(Label, { htmlFor: "bedrock-memory-toggle", children: "Bedrock memory" }), _jsx("span", { className: "text-muted-foreground text-xs", children: "Persist markdown project memory for AI coding agents." })] }), _jsx(Switch, { id: "bedrock-memory-toggle", "data-testid": "bedrock-memory-toggle", checked: enabled, disabled: isPending || initialEnabled, onCheckedChange: handleToggle })] }), failure ? (_jsx("p", { className: "text-destructive text-xs", "data-testid": "bedrock-toggle-remediation", role: "alert", children: failure.remediation })) : null] }));
}
