import { get, post } from "../../editor/services/serviceHelper";
export function getProjects(options) {
    const includeAll = options?.includeAll === true;
    const query = includeAll ? "?includeAll=true" : "";
    return get(`/parhelia/tasks/projects${query}`);
}
export function createProject(data) {
    return post("/parhelia/tasks/createProject", data);
}
export function deleteProject(projectId) {
    const body = { projectId };
    return post("/parhelia/tasks/deleteProject", body);
}
export function updateProject(data) {
    return post("/parhelia/tasks/updateProject", data);
}
export function getProject(projectId) {
    return get(`/parhelia/tasks/project?projectId=${encodeURIComponent(projectId)}`);
}
export function getProjectTemplates(options) {
    const includeDisabled = options?.includeDisabled === true;
    const query = includeDisabled ? "?includeDisabled=true" : "";
    return get(`/parhelia/tasks/projectTemplates${query}`);
}
export function getProjectTemplate(projectTemplateId) {
    return get(`/parhelia/tasks/projectTemplate?projectTemplateId=${encodeURIComponent(projectTemplateId)}`);
}
export function upsertProjectTemplate(data) {
    return post("/parhelia/tasks/upsertProjectTemplate", data);
}
export function deleteProjectTemplate(projectTemplateId) {
    const body = { projectTemplateId };
    return post("/parhelia/tasks/deleteProjectTemplate", body);
}
export function getProjectTemplateAgent(projectTemplateId) {
    return get(`/parhelia/tasks/projectTemplateAgent?projectTemplateId=${encodeURIComponent(projectTemplateId)}`);
}
export function resetProjectTemplateAgent(projectTemplateId) {
    return post("/parhelia/tasks/resetProjectTemplateAgent", { projectTemplateId });
}
export function getProjectProperties(projectId) {
    return get(`/parhelia/tasks/projectProperties?projectId=${encodeURIComponent(projectId)}`);
}
export function upsertProjectProperties(data) {
    return post("/parhelia/tasks/upsertProjectProperties", data);
}
export function deleteProjectProperty(data) {
    return post("/parhelia/tasks/deleteProjectProperty", data);
}
export function getItemCollectionSummary(collectionId) {
    return get(`/parhelia/tasks/itemCollectionSummary?collectionId=${encodeURIComponent(collectionId)}`);
}
export function getItemCollectionDetails(collectionId) {
    return get(`/parhelia/tasks/itemCollectionDetails?collectionId=${encodeURIComponent(collectionId)}`);
}
export function updateItemCollection(data) {
    return post("/parhelia/tasks/updateItemCollection", data);
}
export function createItemCollection(data) {
    return post("/parhelia/tasks/createItemCollection", data);
}
export function getProjectMembers(projectId) {
    return get(`/parhelia/tasks/members?projectId=${encodeURIComponent(projectId)}`);
}
export function addProjectMember(data) {
    return post("/parhelia/tasks/addMember", data);
}
export function searchTaskAssignableUsers(projectId, query, limit = 20) {
    const params = new URLSearchParams({
        projectId,
        query,
        limit: String(limit),
    });
    return get(`/parhelia/tasks/users?${params.toString()}`);
}
export function searchTemplateAssignableUsers(projectTemplateId, query, limit = 20) {
    const params = new URLSearchParams({
        projectTemplateId,
        query,
        limit: String(limit),
    });
    return get(`/parhelia/tasks/templateUsers?${params.toString()}`);
}
export function removeProjectMember(memberId) {
    const body = { memberId };
    return post("/parhelia/tasks/removeMember", body);
}
export function getTasks(projectId) {
    return get(`/parhelia/tasks/tasks?projectId=${encodeURIComponent(projectId)}`);
}
export function getExecutionState(data) {
    return post("/parhelia/tasks/getExecutionState", data);
}
export function getProjectPersistentLogs(projectId, options) {
    const params = new URLSearchParams({
        projectId,
        limit: String(options?.limit ?? 100),
    });
    if (options?.taskId) {
        params.set("taskId", options.taskId);
    }
    return get(`/parhelia/tasks/projectPersistentLogs?${params.toString()}`);
}
export function getTaskPersistentLogs(taskId, options) {
    const params = new URLSearchParams({
        taskId,
        limit: String(options?.limit ?? 100),
    });
    return get(`/parhelia/tasks/taskPersistentLogs?${params.toString()}`);
}
export function getPersistentLogsAdmin(filters) {
    const params = new URLSearchParams();
    const limit = filters?.limit ?? 200;
    params.set("limit", String(limit));
    if (filters?.agentId)
        params.set("agentId", filters.agentId);
    if (filters?.projectId)
        params.set("projectId", filters.projectId);
    if (filters?.taskId)
        params.set("taskId", filters.taskId);
    if (filters?.runId)
        params.set("runId", filters.runId);
    if (filters?.eventType)
        params.set("eventType", filters.eventType);
    if (filters?.stage)
        params.set("stage", filters.stage);
    if (filters?.status)
        params.set("status", filters.status);
    if (filters?.source)
        params.set("source", filters.source);
    if (filters?.searchTerm)
        params.set("searchTerm", filters.searchTerm);
    if (typeof filters?.success === "boolean") {
        params.set("success", String(filters.success));
    }
    return get(`/parhelia/tasks/persistentLogsAdmin?${params.toString()}`);
}
export function purgePersistentLogs() {
    return post("/parhelia/tasks/purgePersistentLogs", {});
}
export function createTask(data) {
    return post("/parhelia/tasks/createTask", data);
}
export function updateTask(data) {
    return post("/parhelia/tasks/updateTask", data);
}
export function applyDecision(data) {
    return post("/parhelia/tasks/applyDecision", data);
}
export function deleteTask(taskId) {
    const body = { taskId };
    return post("/parhelia/tasks/deleteTask", body);
}
export function addComment(data) {
    return post("/parhelia/tasks/addComment", data);
}
export function deleteComment(data) {
    return post("/parhelia/tasks/deleteComment", data);
}
export function getDependencies(projectId) {
    return get(`/parhelia/tasks/dependencies?projectId=${encodeURIComponent(projectId)}`);
}
export function getGraphLayout(projectId) {
    return get(`/parhelia/tasks/graphLayout?projectId=${encodeURIComponent(projectId)}`);
}
export function saveGraphLayout(data) {
    return post("/parhelia/tasks/saveGraphLayout", data);
}
export function addDependency(data) {
    return post("/parhelia/tasks/addDependency", data);
}
export function removeDependency(data) {
    return post("/parhelia/tasks/removeDependency", data);
}
export function getProjectDependencies(projectId) {
    return get(`/parhelia/tasks/projectDependencies?projectId=${encodeURIComponent(projectId)}`);
}
export function addProjectDependency(data) {
    return post("/parhelia/tasks/addProjectDependency", data);
}
export function removeProjectDependency(data) {
    return post("/parhelia/tasks/removeProjectDependency", data);
}
export function uploadAttachment(data) {
    return post("/parhelia/tasks/uploadAttachment", data);
}
export function getAttachments(taskId) {
    return get(`/parhelia/tasks/attachments?taskId=${encodeURIComponent(taskId)}`);
}
export async function uploadAttachmentFile(taskId, file) {
    const formData = new FormData();
    formData.append("file", file);
    const response = await fetch(`/parhelia/tasks/uploadAttachmentFile?taskId=${encodeURIComponent(taskId)}`, {
        method: "POST",
        credentials: "include",
        body: formData,
    });
    if (response.redirected || response.status === 401) {
        if (typeof window !== "undefined") {
            window.dispatchEvent(new CustomEvent("parhelia:session-expired", {
                detail: {
                    reason: response.redirected ? "login-redirect" : "unauthorized",
                },
            }));
        }
        return {
            type: "unauthorized",
            summary: "Session expired",
            details: "Please login again to continue.",
            response,
        };
    }
    if (!response.ok) {
        const contentType = response.headers.get("content-type") || "";
        if (contentType.includes("application/json")) {
            const data = await response.json();
            return {
                type: "error",
                summary: data.summary || "Upload failed",
                details: data.details || data.error,
                response,
            };
        }
        return {
            type: "error",
            summary: "Upload failed",
            details: await response.text(),
            response,
        };
    }
    const data = (await response.json());
    return { type: "success", response, data };
}
export function getAttachmentDownloadUrl(attachmentId) {
    return `/parhelia/tasks/downloadAttachment?attachmentId=${encodeURIComponent(attachmentId)}`;
}
export function deleteAttachment(attachmentId) {
    const body = { attachmentId };
    return post("/parhelia/tasks/deleteAttachment", body);
}
export function getOrchestratorStatus(projectId) {
    return get(`/parhelia/tasks/orchestrator/status?projectId=${encodeURIComponent(projectId)}`);
}
export function runOrchestrator(projectId) {
    return post("/parhelia/tasks/orchestrator/run", { projectId });
}
export function triggerPlanning(projectId) {
    return post("/parhelia/tasks/triggerPlanning", { projectId });
}
export function getWizardGuidance(projectId, selectedTaskId) {
    const params = new URLSearchParams({ projectId });
    if (selectedTaskId) {
        params.set("selectedTaskId", selectedTaskId);
    }
    return get(`/parhelia/tasks/wizardGuidance?${params.toString()}`);
}
//# sourceMappingURL=taskService.js.map