import Cookies from "universal-cookie";
import { get, post } from "./serviceHelper";
import { toItemDescriptor, } from "../pageModel";
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(toItemDescriptor),
        ...(pageItem && { pageItem: toItemDescriptor(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(toItemDescriptor),
        ...(pageItem && { pageItem: toItemDescriptor(pageItem) }),
    };
    const response = await post(url, request);
    return response.data || [];
}
export async function getPageComponents(pageItem) {
    const response = await post("/parhelia/pagecomponents", {
        pageItem,
    });
    return response.data || [];
}
export async function getSites() {
    return await get("/parhelia/sites");
}
// Coalesce concurrent identical resolves. Several page-view contexts (the shell fallback,
// each slot's primary context, compare contexts, offscreen capture) can ask to resolve the
// same item version at the same time; without this each fires its own /parhelia/resolve.
// The entry is removed as soon as the request settles, so a later resolve (after
// navigation/publish) still hits the server fresh — this only shares in-flight requests,
// it does NOT cache results.
const inFlightResolves = new Map();
export function resolvePageAndSite(itemId, language, version) {
    const key = `${itemId}|${language}|${version}`;
    const existing = inFlightResolves.get(key);
    if (existing)
        return existing;
    const pending = post("/parhelia/resolve", {
        itemId,
        language,
        version,
    })
        .then((result) => result.data ?? undefined)
        .finally(() => {
        inFlightResolves.delete(key);
    });
    inFlightResolves.set(key, pending);
    return pending;
}
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(request) {
    return await post("/parhelia/workbox", request);
}
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