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";
import { Separator } from "./Separator";
import { pickVersionAtDate } from "../utils/versionAtDate";
const titlebarSelectorTriggerClassName = "border-0 shadow-none hover:border-0 focus-visible:border-0";
export function ItemLanguageVersion({ hideFavorites = false, hideActions = false, } = {}) {
    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 localizeCmd = editContext.configuration.commands.allItemCommands.find((c) => c.id === "localizeItem");
    const fullItemForTranslate = editContext.item;
    const canTranslate = !!localizeCmd && !!fullItemForTranslate;
    const languageSelector = item ? (_jsx(LanguageSelector, { showAllLanguagesSwitch: true, triggerClassName: titlebarSelectorTriggerClassName, selectedLanguage: item.language, onLanguageSelected: (language) => {
            if (!language.versions) {
                if (editContext) {
                    confirmCreateVersion(editContext, language);
                }
            }
            else
                editContext?.loadItem({
                    ...getItemDescriptor(item),
                    language: language.languageCode,
                    version: 0,
                });
        }, onTranslate: canTranslate
            ? () => editContext.executeCommand({
                command: localizeCmd,
                data: { items: [fullItemForTranslate] },
            })
            : undefined })) : 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 isPreviewDateActive = editContext.mode === "preview" && !!editContext.previewDate;
    const previewDateVersion = isPreviewDateActive
        ? pickVersionAtDate(editContext.itemVersions, editContext.previewDate)
            ?.version
        : undefined;
    const versionSelector = item ? (_jsx(VersionSelector, { itemDescriptor: item, versions: editContext.itemVersions, actualVersion: previewDateVersion ?? editContext.item?.version, isLoading: isVersionSelectorLoading, showVersionPreviews: hasLayout, previewDateActive: isPreviewDateActive, publishRestrictionsItem: editContext.item, triggerClassName: titlebarSelectorTriggerClassName, onVersionSelected: (version) => {
            editContext.setPreviewDate(undefined);
            editContext.loadItem({ ...item, version });
        } })) : null;
    const languageVersionSelectors = !isCompareMode && (languageSelector || versionSelector) ? (_jsxs("div", { className: "flex min-w-0 items-center gap-2", children: [languageSelector && (_jsx("div", { className: "flex min-w-0", children: languageSelector })), languageSelector && versionSelector && (_jsx(Separator, { size: "large", spacing: "none" })), versionSelector && _jsx("div", { className: "shrink-0", children: versionSelector })] })) : null;
    if (editContext.isMobile) {
        return (_jsxs("div", { className: "mr-1 flex w-full min-w-0 items-center gap-1", children: [_jsx(PageSelector, { itemDescriptor: item || null }), languageVersionSelectors && (_jsx(Separator, { size: "large", spacing: "none" })), languageVersionSelectors] }));
    }
    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 }), !hideFavorites && (_jsx("div", { className: "shrink-0", children: _jsx(FavoritesControls, { itemDescriptor: item || null, editContext: editContext }) }))] }), item && !hideActions && (_jsx("div", { className: "shrink-0", children: _jsx(ItemActionsMenu, { isMobile: false }) })), languageVersionSelectors && (_jsx(Separator, { size: "large", spacing: "none" })), languageVersionSelectors] }));
}
//# sourceMappingURL=ItemLanguageVersion.js.map