'use client';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
/**
 * CreateSupervisorDialog
 *
 * Lets the user create a brand-new supervisor policy without first
 * navigating into an application/repository page. Picks the scope, then
 * delegates to the existing {@link SupervisorConfigForm} (so add and edit
 * flows stay in lock-step).
 *
 * Fetches the candidate apps/repos/features as props from the parent
 * server component — keeps this component stateless w.r.t. data
 * loading.
 */
import { useState } from 'react';
import { Plus } from 'lucide-react';
import { Button } from '../ui/button.js';
import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from '../ui/dialog.js';
import { Label } from '../ui/label.js';
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '../ui/select.js';
import { SupervisorConfigForm } from './supervisor-config-form.js';
export function CreateSupervisorDialog({ applications, repositories, features, initialOpen, }) {
    const [open, setOpen] = useState(Boolean(initialOpen));
    const [scope, setScope] = useState('global');
    const [appId, setAppId] = useState('');
    const [repoId, setRepoId] = useState('');
    const [featureId, setFeatureId] = useState('');
    function reset() {
        setScope('global');
        setAppId('');
        setRepoId('');
        setFeatureId('');
    }
    const ready = scope === 'global' ||
        (scope === 'app' && appId.length > 0) ||
        (scope === 'repo' && repoId.length > 0) ||
        (scope === 'feature' && featureId.length > 0);
    const formScopeType = scope === 'feature' ? deriveFeatureParentScope(featureId, features) : scope;
    const formScopeId = scope === 'app'
        ? appId
        : scope === 'repo'
            ? repoId
            : scope === 'feature'
                ? deriveFeatureParentId(featureId, features)
                : undefined;
    const formFeatureId = scope === 'feature' ? featureId : undefined;
    return (_jsxs(Dialog, { open: open, onOpenChange: (next) => {
            setOpen(next);
            if (!next)
                reset();
        }, children: [_jsx(DialogTrigger, { asChild: true, children: _jsxs(Button, { "data-testid": "create-supervisor-trigger", children: [_jsx(Plus, { className: "size-4" }), "Create supervisor"] }) }), _jsxs(DialogContent, { className: "max-w-xl", "data-testid": "create-supervisor-dialog", children: [_jsxs(DialogHeader, { children: [_jsx(DialogTitle, { children: "Create supervisor policy" }), _jsx(DialogDescription, { children: "Pick the scope this supervisor watches, then configure how aggressive it should be. More-specific scopes override less-specific ones at run time." })] }), _jsxs("div", { className: "flex flex-col gap-3", children: [_jsxs("div", { className: "flex flex-col gap-2", children: [_jsx(Label, { htmlFor: "create-supervisor-scope", children: "Scope" }), _jsxs(Select, { value: scope, onValueChange: (v) => setScope(v), children: [_jsx(SelectTrigger, { id: "create-supervisor-scope", "data-testid": "scope-trigger", children: _jsx(SelectValue, {}) }), _jsxs(SelectContent, { children: [_jsx(SelectItem, { value: "global", children: "Global default" }), _jsxs(SelectItem, { value: "app", disabled: applications.length === 0, children: ["Application", applications.length === 0 ? ' (none yet)' : ''] }), _jsxs(SelectItem, { value: "repo", disabled: repositories.length === 0, children: ["Repository", repositories.length === 0 ? ' (none yet)' : ''] }), _jsxs(SelectItem, { value: "feature", disabled: features.length === 0, children: ["Per-feature override", features.length === 0 ? ' (none yet)' : ''] })] })] })] }), scope === 'app' ? (_jsx(ScopeOptionPicker, { id: "create-supervisor-app", label: "Application", value: appId, onChange: setAppId, options: applications, testid: "app-trigger" })) : null, scope === 'repo' ? (_jsx(ScopeOptionPicker, { id: "create-supervisor-repo", label: "Repository", value: repoId, onChange: setRepoId, options: repositories, testid: "repo-trigger" })) : null, scope === 'feature' ? (_jsx(ScopeOptionPicker, { id: "create-supervisor-feature", label: "Feature", value: featureId, onChange: setFeatureId, options: features.map((f) => ({ id: f.id, name: f.name })), testid: "feature-trigger" })) : null] }), ready ? (_jsx(SupervisorConfigForm, { scopeType: formScopeType, ...(formScopeId !== undefined && { scopeId: formScopeId }), ...(formFeatureId !== undefined && { featureId: formFeatureId }), initialPolicy: null })) : (_jsx(DialogFooter, { children: _jsx("p", { className: "text-muted-foreground text-xs", children: "Pick a scope above to configure the supervisor." }) }))] })] }));
}
function ScopeOptionPicker({ id, label, value, onChange, options, testid, }) {
    return (_jsxs("div", { className: "flex flex-col gap-2", children: [_jsx(Label, { htmlFor: id, children: label }), _jsxs(Select, { value: value, onValueChange: onChange, disabled: options.length === 0, children: [_jsx(SelectTrigger, { id: id, "data-testid": testid, children: _jsx(SelectValue, { placeholder: `Pick a ${label.toLowerCase()}` }) }), _jsx(SelectContent, { children: options.map((option) => (_jsx(SelectItem, { value: option.id, children: option.name }, option.id))) })] })] }));
}
function deriveFeatureParentScope(featureId, features) {
    if (!featureId)
        return 'global';
    const feat = features.find((f) => f.id === featureId);
    if (feat?.applicationId)
        return 'app';
    if (feat?.repositoryId)
        return 'repo';
    return 'global';
}
function deriveFeatureParentId(featureId, features) {
    if (!featureId)
        return undefined;
    const feat = features.find((f) => f.id === featureId);
    return feat?.applicationId ?? feat?.repositoryId;
}
