import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { useEffect, useMemo, useState } from "react";
import { toast } from "sonner";
import { Button } from "../../components/ui/button";
import { Dialog, DialogContent, DialogFooter } from "../../components/ui/dialog";
import { StyledDialogTitle } from "../../components/ui/styled-dialog-title";
import { Input } from "../../components/ui/input";
import { Label } from "../../components/ui/label";
import { Select } from "../../components/ui/select";
import { ProjectExecutionUserPicker } from "./ProjectExecutionUserPicker";
import { Textarea } from "../../components/ui/textarea";
import { ChevronRight, Settings } from "lucide-react";
import { addProjectDependency, addProjectMember, getProjectMembers, getProjectDependencies, removeProjectDependency, removeProjectMember, updateProject, } from "../services/taskService";
export function ProjectSettingsDialog(props) {
    const DEFAULT_COST_LIMIT = "25";
    const { open, onOpenChange, projectId, selectedProject, projects, onChanged } = props;
    const [members, setMembers] = useState([]);
    const [projectDependencies, setProjectDependencies] = useState([]);
    const [loading, setLoading] = useState(false);
    const [loadingProjectDependencies, setLoadingProjectDependencies] = useState(false);
    const [savingProject, setSavingProject] = useState(false);
    const [savingProjectDependency, setSavingProjectDependency] = useState(false);
    const [removingProjectDependencyId, setRemovingProjectDependencyId] = useState(null);
    const [titleInput, setTitleInput] = useState("");
    const [descriptionInput, setDescriptionInput] = useState("");
    const [costLimitInput, setCostLimitInput] = useState("");
    const [statusInput, setStatusInput] = useState("Active");
    const [executionUserIdInput, setExecutionUserIdInput] = useState("");
    const [executionUserDisplayNameInput, setExecutionUserDisplayNameInput] = useState("");
    const [dependencyProjectId, setDependencyProjectId] = useState("");
    const [memberType, setMemberType] = useState("User");
    const [memberIdentifier, setMemberIdentifier] = useState("");
    const [permission, setPermission] = useState("Viewer");
    const [showProjectDependencies, setShowProjectDependencies] = useState(false);
    const [showMembers, setShowMembers] = useState(false);
    const canManage = selectedProject?.permission === "Owner";
    const title = useMemo(() => selectedProject?.project.title ?? "Project", [selectedProject]);
    useEffect(() => {
        if (!open || !selectedProject)
            return;
        const initialExecutionUserId = selectedProject.project.executionUserId ?? selectedProject.project.createdBy ?? "";
        const initialExecutionUserDisplayName = selectedProject.project.executionUserDisplayName ?? initialExecutionUserId;
        setTitleInput(selectedProject.project.title ?? "");
        setDescriptionInput(selectedProject.project.description ?? "");
        setCostLimitInput(selectedProject.project.costLimit == null ? DEFAULT_COST_LIMIT : String(selectedProject.project.costLimit));
        setStatusInput(selectedProject.project.status ?? "Active");
        setExecutionUserIdInput(initialExecutionUserId);
        setExecutionUserDisplayNameInput(initialExecutionUserDisplayName);
        setDependencyProjectId("");
        setShowProjectDependencies(false);
        setShowMembers(false);
    }, [open, selectedProject]);
    const projectTitlesById = useMemo(() => {
        const map = new Map();
        for (const project of projects) {
            map.set(project.project.projectId, project.project.title);
        }
        return map;
    }, [projects]);
    const siblingProjectOptions = useMemo(() => {
        const parentProjectId = selectedProject?.project.parentProjectId;
        if (!selectedProject || !parentProjectId)
            return [];
        const existingDependencyIds = new Set(projectDependencies.map((dependency) => dependency.dependsOnProjectId));
        return projects
            .filter((project) => project.project.projectId !== selectedProject.project.projectId &&
            project.project.parentProjectId === parentProjectId &&
            !existingDependencyIds.has(project.project.projectId))
            .sort((a, b) => a.project.title.localeCompare(b.project.title))
            .map((project) => ({
            value: project.project.projectId,
            label: project.project.title,
        }));
    }, [projectDependencies, projects, selectedProject]);
    const refreshMembers = async () => {
        if (!projectId)
            return;
        setLoading(true);
        try {
            const result = await getProjectMembers(projectId);
            if (result.type !== "success") {
                toast.error(result.summary || "Failed to load members");
                return;
            }
            setMembers(result.data || []);
        }
        finally {
            setLoading(false);
        }
    };
    const refreshProjectDependencies = async () => {
        if (!projectId)
            return;
        setLoadingProjectDependencies(true);
        try {
            const result = await getProjectDependencies(projectId);
            if (result.type !== "success") {
                toast.error(result.summary || "Failed to load project dependencies");
                return;
            }
            setProjectDependencies(result.data || []);
        }
        finally {
            setLoadingProjectDependencies(false);
        }
    };
    useEffect(() => {
        if (!open)
            return;
        void refreshMembers();
        void refreshProjectDependencies();
        // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [open, projectId]);
    const submitAdd = async () => {
        if (!projectId)
            return;
        if (!canManage) {
            toast.error("Only owners can manage members");
            return;
        }
        if (!memberIdentifier.trim()) {
            toast.error("Member identifier is required");
            return;
        }
        const result = await addProjectMember({
            projectId,
            memberType,
            memberIdentifier: memberIdentifier.trim(),
            permission,
        });
        if (result.type !== "success") {
            toast.error(result.summary || "Failed to add member");
            return;
        }
        toast.success("Member added");
        setMemberIdentifier("");
        await refreshMembers();
        await onChanged();
    };
    const submitProjectSettings = async () => {
        if (!projectId)
            return;
        if (!canManage) {
            toast.error("Only owners can edit project settings");
            return;
        }
        if (!titleInput.trim()) {
            toast.error("Project title is required");
            return;
        }
        const parsedCostLimit = Number(costLimitInput);
        if (Number.isNaN(parsedCostLimit) || parsedCostLimit <= 0) {
            toast.error("Cost limit must be greater than 0");
            return;
        }
        const normalizedExecutionUserId = executionUserIdInput.trim();
        const normalizedExecutionUserDisplayName = executionUserDisplayNameInput.trim() || normalizedExecutionUserId;
        if (!normalizedExecutionUserId) {
            toast.error("Agent execution user is required");
            return;
        }
        setSavingProject(true);
        try {
            const result = await updateProject({
                projectId,
                title: titleInput.trim(),
                description: descriptionInput.trim() ? descriptionInput.trim() : "",
                costLimit: parsedCostLimit,
                status: statusInput,
                executionUserId: normalizedExecutionUserId,
                executionUserDisplayName: normalizedExecutionUserDisplayName,
            });
            if (result.type !== "success") {
                toast.error(result.summary || "Failed to update project");
                return;
            }
            // toast.success("Project settings updated");
            await onChanged();
            onOpenChange(false);
        }
        finally {
            setSavingProject(false);
        }
    };
    const submitAddProjectDependency = async () => {
        if (!projectId)
            return;
        if (!canManage) {
            toast.error("Only owners can edit project settings");
            return;
        }
        if (!dependencyProjectId) {
            toast.error("Choose a sibling subproject");
            return;
        }
        setSavingProjectDependency(true);
        try {
            const result = await addProjectDependency({
                projectId,
                dependsOnProjectId: dependencyProjectId,
                dependencyType: "BlockedBy",
            });
            if (result.type !== "success") {
                toast.error(result.summary || "Failed to add project dependency");
                return;
            }
            toast.success("Subproject dependency added");
            setDependencyProjectId("");
            await refreshProjectDependencies();
            await onChanged();
        }
        finally {
            setSavingProjectDependency(false);
        }
    };
    const submitRemoveProjectDependency = async (projectDependencyId) => {
        if (!canManage)
            return;
        setRemovingProjectDependencyId(projectDependencyId);
        try {
            const result = await removeProjectDependency({ projectDependencyId });
            if (result.type !== "success") {
                toast.error(result.summary || "Failed to remove project dependency");
                return;
            }
            toast.success("Subproject dependency removed");
            await refreshProjectDependencies();
            await onChanged();
        }
        finally {
            setRemovingProjectDependencyId(null);
        }
    };
    const submitRemove = async (memberId) => {
        if (!canManage)
            return;
        const result = await removeProjectMember(memberId);
        if (result.type !== "success") {
            toast.error(result.summary || "Failed to remove member");
            return;
        }
        toast.success("Member removed");
        await refreshMembers();
        await onChanged();
    };
    return (_jsx(Dialog, { open: open, onOpenChange: onOpenChange, children: _jsxs(DialogContent, { className: "max-w-2xl", children: [_jsx(StyledDialogTitle, { icon: _jsx(Settings, { strokeWidth: 1 }), title: "Project Settings", subtitle: title }), _jsx("div", { className: "min-h-0 flex-1 overflow-y-auto", children: _jsxs("div", { className: "grid gap-4 p-4 sm:gap-6 sm:p-6", children: [_jsxs("div", { className: "grid gap-2", children: [_jsx(Label, { htmlFor: "projectSettingsTitle", children: "Title" }), _jsx(Input, { id: "projectSettingsTitle", value: titleInput, onChange: (e) => setTitleInput(e.target.value), disabled: !canManage || savingProject, placeholder: "Project title" })] }), _jsxs("div", { className: "grid gap-2", children: [_jsx(Label, { htmlFor: "projectSettingsDescription", children: "Description" }), _jsx(Textarea, { id: "projectSettingsDescription", className: "text-xs", value: descriptionInput, onChange: (e) => setDescriptionInput(e.target.value), disabled: !canManage || savingProject, placeholder: "Optional description", rows: 3 })] }), _jsxs("div", { className: "grid gap-2", children: [_jsx(Label, { htmlFor: "projectSettingsCostLimit", children: "Cost limit" }), _jsx(Input, { id: "projectSettingsCostLimit", value: costLimitInput, onChange: (e) => setCostLimitInput(e.target.value), disabled: !canManage || savingProject, placeholder: DEFAULT_COST_LIMIT })] }), _jsxs("div", { className: "grid gap-2", children: [_jsx(Label, { htmlFor: "projectSettingsStatus", children: "Status" }), _jsx(Select, { id: "projectSettingsStatus", value: statusInput, onValueChange: (v) => setStatusInput(v), disabled: !canManage || savingProject, options: [
                                            { value: "Planning", label: "Planning" },
                                            { value: "Active", label: "Active" },
                                            { value: "Finished", label: "Finished" },
                                            { value: "Archived", label: "Archived" },
                                        ] })] }), _jsxs("div", { className: "grid gap-2", children: [_jsx(Label, { htmlFor: "projectSettingsCreatedBy", children: "Created by" }), _jsx(Input, { id: "projectSettingsCreatedBy", value: selectedProject?.project.createdBy ?? "", disabled: true, "data-testid": "taskboard-project-settings-created-by" })] }), _jsxs("div", { className: "grid gap-2", children: [_jsxs("div", { className: "flex items-center justify-between gap-2", children: [_jsx(Label, { children: "Agents run as" }), _jsx(Button, { type: "button", variant: "ghost", size: "sm", onClick: () => {
                                                    const creatorUserName = selectedProject?.project.createdBy ?? "";
                                                    setExecutionUserIdInput(creatorUserName);
                                                    setExecutionUserDisplayNameInput(creatorUserName);
                                                }, disabled: !canManage || savingProject || !selectedProject?.project.createdBy, children: "Use creator" })] }), projectId ? (_jsx(ProjectExecutionUserPicker, { projectId: projectId, value: executionUserIdInput, displayValue: executionUserDisplayNameInput, disabled: !canManage || savingProject, buttonTestId: "taskboard-project-settings-execution-user", placeholder: "Choose project execution user", onChange: (next) => {
                                            setExecutionUserIdInput(next.userId);
                                            setExecutionUserDisplayNameInput(next.displayName);
                                        } })) : null, _jsx("div", { className: "text-xs text-muted-foreground", children: "Taskboard agents impersonate this Sitecore user when they run project work." })] }), _jsx("div", { className: "grid gap-2", children: _jsxs(Button, { type: "button", variant: "ghost", className: "h-auto justify-between px-0 py-0 text-left hover:bg-transparent", onClick: () => setShowProjectDependencies((value) => !value), children: [_jsxs("div", { className: "grid gap-1 text-left", children: [_jsx("div", { className: "text-sm font-semibold", children: "Subproject dependencies" }), _jsx("div", { className: "text-xs text-muted-foreground", children: "Execution in this subproject stays blocked until each selected sibling subproject is marked Finished." })] }), _jsx(ChevronRight, { className: `h-4 w-4 shrink-0 transition-transform ${showProjectDependencies ? "rotate-90" : ""}` })] }) }), showProjectDependencies ? (_jsxs(_Fragment, { children: [_jsx("div", { className: "max-h-48 overflow-auto rounded-md border", children: loadingProjectDependencies ? (_jsx("div", { className: "p-3 text-sm text-muted-foreground", children: "Loading..." })) : projectDependencies.length === 0 ? (_jsx("div", { className: "p-3 text-sm text-muted-foreground", children: "No subproject dependencies." })) : (_jsx("div", { className: "divide-y", children: projectDependencies.map((dependency) => {
                                                const dependencyTitle = projectTitlesById.get(dependency.dependsOnProjectId) ||
                                                    dependency.dependsOnProjectId;
                                                return (_jsxs("div", { className: "flex items-center justify-between gap-2 p-3", children: [_jsxs("div", { className: "min-w-0", children: [_jsx("div", { className: "truncate text-sm font-medium", children: dependencyTitle }), _jsx("div", { className: "truncate text-xs text-muted-foreground", children: "Blocks this subproject until status is Finished" })] }), _jsx(Button, { variant: "secondary", onClick: () => submitRemoveProjectDependency(dependency.projectDependencyId), disabled: !canManage ||
                                                                removingProjectDependencyId ===
                                                                    dependency.projectDependencyId, children: removingProjectDependencyId ===
                                                                dependency.projectDependencyId
                                                                ? "Removing..."
                                                                : "Remove" })] }, dependency.projectDependencyId));
                                            }) })) }), _jsxs("div", { className: "grid grid-cols-12 items-end gap-2", children: [_jsx("div", { className: "col-span-11", children: _jsxs("div", { className: "grid gap-1.5", children: [_jsx(Label, { className: "text-xs", children: "Blocked by sibling subproject" }), _jsx(Select, { value: dependencyProjectId, onValueChange: setDependencyProjectId, disabled: !canManage || savingProjectDependency, options: siblingProjectOptions, placeholder: selectedProject?.project.parentProjectId
                                                                ? "Select a sibling subproject"
                                                                : "Only subprojects can add dependencies" })] }) }), _jsx("div", { className: "col-span-1", children: _jsx(Button, { size: "icon", className: "h-[29px] w-[29px]", onClick: submitAddProjectDependency, disabled: !canManage ||
                                                        savingProjectDependency ||
                                                        !dependencyProjectId ||
                                                        !selectedProject?.project.parentProjectId, children: "+" }) })] })] })) : null, _jsx("div", { className: "grid gap-2", children: _jsxs(Button, { type: "button", variant: "ghost", className: "h-auto justify-between px-0 py-0 text-left hover:bg-transparent", onClick: () => setShowMembers((value) => !value), children: [_jsxs("div", { className: "grid gap-1 text-left", children: [_jsx("div", { className: "text-sm font-semibold", children: "Members" }), _jsx("div", { className: "text-xs text-muted-foreground", children: "Owners can add users or roles to this project." })] }), _jsx(ChevronRight, { className: `h-4 w-4 shrink-0 transition-transform ${showMembers ? "rotate-90" : ""}` })] }) }), showMembers ? (_jsxs(_Fragment, { children: [_jsx("div", { className: "max-h-72 overflow-auto rounded-md border", children: loading ? (_jsx("div", { className: "p-3 text-sm text-muted-foreground", children: "Loading\u2026" })) : members.length === 0 ? (_jsx("div", { className: "p-3 text-sm text-muted-foreground", children: "No members" })) : (_jsx("div", { className: "divide-y", children: members.map((m) => (_jsxs("div", { className: "flex items-center justify-between gap-2 p-3", children: [_jsxs("div", { className: "min-w-0", children: [_jsx("div", { className: "truncate text-sm font-medium", children: m.memberIdentifier }), _jsxs("div", { className: "truncate text-xs text-muted-foreground", children: [m.memberType, " \u2022 ", m.permission] })] }), _jsx(Button, { variant: "secondary", onClick: () => submitRemove(m.memberId), disabled: !canManage, children: "Remove" })] }, m.memberId))) })) }), _jsxs("div", { className: "grid grid-cols-12 items-end gap-2", children: [_jsx("div", { className: "col-span-3", children: _jsxs("div", { className: "grid gap-1.5", children: [_jsx(Label, { className: "text-xs", children: "Type" }), _jsx(Select, { value: memberType, onValueChange: (v) => setMemberType(v), options: [
                                                                { value: "User", label: "User" },
                                                                { value: "Role", label: "Role" },
                                                            ] })] }) }), _jsx("div", { className: "col-span-5", children: _jsxs("div", { className: "grid gap-1.5", children: [_jsx(Label, { className: "text-xs", children: "Identifier" }), _jsx(Input, { className: "h-[29px]", value: memberIdentifier, onChange: (e) => setMemberIdentifier(e.target.value), placeholder: memberType === "User" ? "sitecore\\john" : "sitecore\\Author" })] }) }), _jsx("div", { className: "col-span-3", children: _jsxs("div", { className: "grid gap-1.5", children: [_jsx(Label, { className: "text-xs", children: "Permission" }), _jsx(Select, { value: permission, onValueChange: (v) => setPermission(v), options: [
                                                                { value: "Viewer", label: "Viewer" },
                                                                { value: "Editor", label: "Editor" },
                                                                { value: "Owner", label: "Owner" },
                                                            ] })] }) }), _jsx("div", { className: "col-span-1", children: _jsx(Button, { size: "icon", className: "h-[29px] w-[29px]", onClick: submitAdd, disabled: !canManage, children: "+" }) })] })] })) : null] }) }), _jsxs(DialogFooter, { className: "flex flex-row items-center justify-end gap-2 p-4", children: [_jsx(Button, { variant: "outline", onClick: () => onOpenChange(false), children: "Close" }), _jsx(Button, { onClick: submitProjectSettings, disabled: !canManage || savingProject || !titleInput.trim(), children: savingProject ? "Saving..." : "Save Changes" })] })] }) }));
}
//# sourceMappingURL=ProjectSettingsDialog.js.map