import { get, post } from "./serviceHelper";
export async function createOrUpdateSuggestedEdit(suggestedEdit, options = {}) {
    const params = new URLSearchParams();
    if (options.expectedUpdated) {
        params.set("expectedUpdated", options.expectedUpdated);
    }
    if (options.expectedFieldHash) {
        params.set("expectedFieldHash", options.expectedFieldHash);
    }
    const query = params.toString();
    const result = await post(`/parhelia/suggested-edits/add${query ? `?${query}` : ""}`, suggestedEdit);
    if (result.response?.status !== 409)
        return result;
    try {
        const payload = JSON.parse(result.rawDetails || "{}");
        return { type: "conflict", response: result.response, ...payload };
    }
    catch {
        return {
            type: "conflict",
            response: result.response,
            summary: result.summary || "Suggestion changed while saving.",
        };
    }
}
/**
 * Retrieves all suggested edits for a given main item based on its ID, language, and version.
 */
export function getSuggestedEdits(mainItemId, mainItemLanguage, mainItemVersion, reviewId) {
    const params = new URLSearchParams({
        mainItemId,
        mainItemLanguage,
        mainItemVersion: mainItemVersion.toString(),
    });
    if (reviewId) {
        params.set("reviewId", reviewId);
    }
    return get(`/parhelia/suggested-edits/index?${params.toString()}`);
}
// Latest suggested edits across the system, already filtered by backend for user access.
export function getLatestSuggestedEdits(take, language) {
    const params = new URLSearchParams();
    if (take && take > 0)
        params.set("take", take.toString());
    if (language)
        params.set("language", language);
    const query = params.toString();
    return get(`/parhelia/suggested-edits/latest${query ? `?${query}` : ""}`);
}
/**
 * Retrieves a single suggested edit by its unique identifier.
 */
export function getSuggestedEditById(id) {
    return get(`/parhelia/suggested-edits/get?id=${id}`);
}
/**
 * Deletes the given suggested edit.
 */
export function deleteSuggestedEdit(suggestedEdit) {
    return post(`/parhelia/suggested-edits/delete?id=${suggestedEdit.id}`, {});
}
//# sourceMappingURL=suggestedEditsService.js.map