import Cookies from "universal-cookie";
import { get, post } from "./serviceHelper";
export async function getChildren(itemId, sessionId, templateIds = [], includeEmbeddedItems, language, include, database = "master") {
    let url = 
    // configuration.services.editorService.baseUrl +
    "/parhelia/children?itemId=" +
        itemId +
        "&sessionId=" +
        sessionId +
        "&includeEmbeddedItems=" +
        includeEmbeddedItems +
        "&language=" +
        language +
        "&include=" +
        include +
        "&database=" +
        database;
    url = templateIds.reduce((u, t) => u + "&templateIds=" + t, url);
    const response = await fetch(url, {
        method: "GET",
        credentials: "include",
        headers: {
            "Content-Type": "application/json",
            Cookie: new Cookies().getAll(),
        },
    });
    return await response.json();
}
// export async function getItem(
//   item: ItemDescriptor
// ): Promise<FullItem | undefined> {
//   let url =
//     "/parhelia/item?itemId=" +
//     item.id +
//     "&language=" +
//     item.language +
//     "&version=" +
//     item.version;
//   const response = await fetch(url, {
//     method: "GET",
//     credentials: "include",
//     headers: {
//       "Content-Type": "application/json",
//       Cookie: new Cookies().getAll(),
//     },
//   });
//   if (response.ok) {
//     const json = await response.json();
//     return json;
//   }
//   return undefined;
// }
export async function fetchItems(itemDescriptors, pageItem) {
    let url = "/parhelia/items";
    const request = {
        items: itemDescriptors.map((item) => {
            const { database, ...rest } = item;
            return database ? item : rest;
        }),
        ...(pageItem && {
            pageItem: pageItem.database
                ? pageItem
                : (({ database, ...rest }) => rest)(pageItem),
        }),
    };
    const response = await post(url, request);
    return response.data || [];
}
export async function fetchItemStubs(itemDescriptors, pageItem) {
    let url = "/parhelia/itemstubs";
    const request = {
        items: itemDescriptors.map((item) => {
            const { database, ...rest } = item;
            return database ? item : rest;
        }),
        ...(pageItem && {
            pageItem: pageItem.database
                ? pageItem
                : (({ database, ...rest }) => rest)(pageItem),
        }),
    };
    const response = await post(url, request);
    return response.data || [];
}
export async function getSites() {
    return await get("/parhelia/sites");
}
export async function resolvePageAndSite(itemId, language, version) {
    const result = await post("/parhelia/resolve", {
        itemId,
        language,
        version,
    });
    if (result.data) {
        return result.data;
    }
}
export async function getLanguagesAndVersions(item, options) {
    const request = {
        ...item,
        ...(options?.includePublishingRestrictions
            ? { includePublishingRestrictions: true }
            : {}),
        ...(options?.includePublishingTargets
            ? { includePublishingTargets: true }
            : {}),
    };
    return await post("/parhelia/getLanguagesAndVersions", request);
}
export async function getDictionary(siteName, language) {
    let response = await get("/parhelia/dictionary/index?siteName=" + siteName + "&language=" + language);
    return response.data;
}
export async function getWorkbox(items) {
    return await post("/parhelia/workbox", items);
}
export async function getWorkboxDescendants(rootItem) {
    return await post("/parhelia/workboxDescendants", rootItem);
}
export async function exportItems(request) {
    return await post("/parhelia/exportItems", request);
}
export async function importItems(request) {
    return await post("/parhelia/importItems", request);
}
export async function getRichTextProfile(itemPath, contextItemId) {
    return await post(`/parhelia/richTextProfile`, {
        profile: itemPath,
        contextItemId,
    });
}
export async function getLanguages() {
    return await get("/parhelia/languages");
}
//# sourceMappingURL=contentService.js.map