import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useEditContext } from "../client/editContext";
import { LanguageSelector } from "../../components/ui/LanguageSelector";
import { PageSelector } from "./PageSelector";
import { VersionSelector } from "./VersionSelector";
import { getItemDescriptor } from "../utils";
import { confirmCreateVersion } from "../utils/itemutils";
import { ItemActionsMenu } from "./ItemActionsMenu";
import { FavoritesControls } from "./FavoritesControls";
export function ItemLanguageVersion() {
    const editContext = useEditContext();
    if (!editContext)
        return;
    // Hide language/version selectors in compare mode since each pane has its own
    const activeSlotContext = editContext.getActiveSlotContext();
    const isCompareMode = activeSlotContext?.compareMode ?? false;
    const item = editContext.currentItemDescriptor;
    const languageSelector = item ? (_jsx(LanguageSelector, { showAllLanguagesSwitch: true, selectedLanguage: item.language, onLanguageSelected: (language) => {
            if (!language.versions) {
                if (editContext) {
                    confirmCreateVersion(editContext, language);
                }
            }
            else
                editContext?.loadItem({
                    ...getItemDescriptor(item),
                    language: language.languageCode,
                    version: 0,
                });
        } })) : null;
    const contentEditorItem = editContext.item;
    const pageItem = editContext.page?.item;
    const contentEditorItemMatchesBaseItem = item &&
        contentEditorItem?.id === item.id &&
        contentEditorItem?.language === item.language;
    const isContentEditorPendingOrWrongVersion = !item ||
        !contentEditorItemMatchesBaseItem ||
        (item.version !== 0 && contentEditorItem?.version !== item.version);
    const hasLayout = contentEditorItem?.hasLayout === true;
    const pageItemMatchesBaseItem = item && pageItem?.id === item.id && pageItem?.language === item.language;
    const expectedPageVersion = item && item.version !== 0 ? item.version : contentEditorItem?.version;
    // Don't wait for page if item has no versions (page can't load without a version)
    const hasNoVersions = contentEditorItem?.versions === 0;
    const isPagePendingOrWrongVersion = hasLayout &&
        !hasNoVersions &&
        (!pageItemMatchesBaseItem ||
            (expectedPageVersion != null &&
                pageItem?.version !== expectedPageVersion));
    const isVersionSelectorLoading = isContentEditorPendingOrWrongVersion || isPagePendingOrWrongVersion;
    const versionSelector = item ? (_jsx(VersionSelector, { itemDescriptor: item, versions: editContext.itemVersions, actualVersion: editContext.item?.version, isLoading: isVersionSelectorLoading, onVersionSelected: (version) => {
            editContext.loadItem({ ...item, version });
        } })) : null;
    if (editContext.isMobile) {
        return (_jsxs("div", { className: "mr-1 flex w-full min-w-0 items-center gap-1 overflow-y-clip md:overflow-scroll", children: [_jsx(PageSelector, { itemDescriptor: item || null }), !isCompareMode && languageSelector, !isCompareMode && versionSelector] }));
    }
    return (_jsxs("div", { className: "mr-3 flex min-w-0 items-center gap-3", "data-testid": "item-language-version-bar", children: [_jsxs("div", { className: "flex min-w-0 items-center gap-1", children: [_jsx(PageSelector, { itemDescriptor: item || null }), _jsx("div", { className: "shrink-0", children: _jsx(FavoritesControls, { itemDescriptor: item || null, editContext: editContext }) })] }), item && (_jsx("div", { className: "shrink-0", children: _jsx(ItemActionsMenu, { isMobile: false }) })), !isCompareMode && languageSelector && (_jsx("div", { className: "shrink-0", children: languageSelector })), !isCompareMode && versionSelector && (_jsx("div", { className: "shrink-0", children: versionSelector }))] }));
}
//# sourceMappingURL=ItemLanguageVersion.js.map