import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import { Button } from "../../components/ui/button";
import { EditorWarnings } from "../EditorWarnings";
import { FieldList } from "../FieldList";
import { ItemInfo } from "../ItemInfo";
import { useEditContext } from "../client/editContext";
import { LanguageSelector } from "../../components/ui/LanguageSelector";
import { useSlotContext } from "./editorSlotContext";
import { TemplateBuilderDialog } from "../template-wizard/TemplateBuilderDialog";
const TEMPLATE_TEMPLATE_ID = "AB86861A-6030-46C5-B394-E8F99E8B87DB";
const normalizeGuid = (guid) => (guid || "").replace(/[{}]/g, "").toUpperCase();
export function ItemEditor({ item, compareToItem, itemVersions, itemLanguages, onCreateVersion, onLanguageSelected, }) {
    const editContext = useEditContext();
    const slotContext = useSlotContext();
    if (!editContext)
        return;
    if (!item)
        item = slotContext?.item ?? editContext.item;
    if (!item)
        return;
    const versions = itemVersions ?? editContext.itemVersions;
    const languages = itemLanguages ?? editContext.itemLanguages;
    const pageValidators = editContext.validationResult?.find((x) => x.item.id === item?.id &&
        x.item.language === item?.language &&
        x.item.version === item?.version)?.results || [];
    const isTemplateItem = normalizeGuid(item.templateId) === normalizeGuid(TEMPLATE_TEMPLATE_ID);
    return (_jsxs("div", { className: "absolute inset-0 overflow-y-auto bg-white", children: [_jsx(EditorWarnings, { item: item }), _jsx(ItemInfo, { item: item }), versions.length > 0 && (_jsx(_Fragment, { children: _jsx(FieldList, { fields: [{ fields: Object.values(item.fields) }], validators: pageValidators.filter((x) => x.itemId === item.id), showStandardFieldsEnabled: true, compareToFields: compareToItem?.fields }) })), versions.length === 0 && !editContext.isLoadingVersions && (_jsxs("div", { className: "flex flex-col items-center gap-4 p-4", children: [_jsxs(Button, { "data-testid": "create-first-version-button", onClick: async () => {
                            if (onCreateVersion) {
                                await onCreateVersion(item.descriptor);
                                return;
                            }
                            await editContext.operations.createVersion(item.descriptor);
                        }, children: ["Create First Version (", item.language, ")"] }), languages.some((lang) => lang.versions > 0) && (_jsxs("div", { className: "flex flex-col items-center gap-2", children: [_jsx("div", { className: "text-neutral-grey-50 text-sm", children: "Or select a different language:" }), _jsx(LanguageSelector
                            // If the current language has no versions yet, don't show it as the selected
                            // language in the selector (the selector is meant for switching to existing
                            // language versions in this state).
                            , { 
                                // If the current language has no versions yet, don't show it as the selected
                                // language in the selector (the selector is meant for switching to existing
                                // language versions in this state).
                                selectedLanguage: languages.find((lang) => lang.languageCode === item.language && lang.versions > 0)
                                    ? item.language
                                    : undefined, onLanguageSelected: async (language) => {
                                    const descriptor = {
                                        id: item.descriptor.id,
                                        language: language.languageCode,
                                        version: 0,
                                    };
                                    if (onLanguageSelected) {
                                        await onLanguageSelected(descriptor);
                                        return;
                                    }
                                    await editContext.loadItem(descriptor);
                                }, showAllLanguagesSwitch: false })] }))] })), isTemplateItem && versions.length > 0 && (_jsx(TemplateBuilderDialog, { item: item }))] }));
}
//# sourceMappingURL=ItemEditor.js.map