import { getItemDescriptor } from "../utils";
import { post } from "./serviceHelper";
export async function createNewComponent(name, contextItem) {
    let url = "/alpaca/headless/component/create";
    const response = await post(url, {
        name,
        contextItem: getItemDescriptor(contextItem),
    });
    if (!response.response.ok) {
        return new Error("Error creating new component");
    }
    return await response.response.json();
}
export async function createRendering(name, context, componentTemplateId) {
    let url = "/alpaca/headless/component/createRendering";
    const response = await post(url, {
        contextItem: getItemDescriptor(context),
        componentTemplateId,
        name,
    });
    if (!response.response.ok) {
        return new Error("Error creating new rendering");
    }
    return await response.response.json();
}
export async function getAllComponents(contextItemId) {
    let url = "/alpaca/headless/component/all?contextItemId=" + contextItemId;
    const response = await fetch(url, {
        method: "GET",
        credentials: "include",
        headers: {
            "Content-Type": "application/json",
        },
    });
    return await response.json();
}
export async function getFieldTypes() {
    let url = "/alpaca/headless/component/fieldtypes";
    const response = await fetch(url, {
        method: "GET",
        credentials: "include",
        headers: {
            "Content-Type": "application/json",
        },
    });
    return await response.json();
}
export async function loadComponentDetails(id, contextItemId) {
    let url = "/alpaca/headless/component?id=" + id + "&contextItemId=" + contextItemId;
    const response = await fetch(url, {
        method: "GET",
        credentials: "include",
        headers: {
            "Content-Type": "application/json",
        },
    });
    return await response.json();
}
export async function saveComponentDetails(component, contextItem) {
    let url = "/alpaca/headless/component/save";
    const response = await post(url, {
        component,
        contextItem: getItemDescriptor(contextItem),
    });
    if (response.response.ok) {
        return true;
    }
    else {
        return false;
    }
}
//# sourceMappingURL=componentDesignerService.js.map