'use client';
import { jsxDEV as _jsxDEV, Fragment as _Fragment } from "react/jsx-dev-runtime";
import { getTranslation } from '@payloadcms/translations';
import { formatAdminURL, hasAutosaveEnabled, hasDraftsEnabled } from 'payload/shared';
import React, { Fragment, useEffect } from 'react';
import { useFormInitializing, useFormProcessing } from '../../forms/Form/context.js';
import { useConfig } from '../../providers/Config/index.js';
import { useEditDepth } from '../../providers/EditDepth/index.js';
import { useLivePreviewContext } from '../../providers/LivePreview/context.js';
import { useTranslation } from '../../providers/Translation/index.js';
import { formatDate } from '../../utilities/formatDocTitle/formatDateTitle.js';
import { Autosave } from '../Autosave/index.js';
import { Button } from '../Button/index.js';
import { CopyLocaleData } from '../CopyLocaleData/index.js';
import { DeleteDocument } from '../DeleteDocument/index.js';
import { DuplicateDocument } from '../DuplicateDocument/index.js';
import { MoveDocToFolder } from '../FolderView/MoveDocToFolder/index.js';
import { Gutter } from '../Gutter/index.js';
import { LivePreviewToggler } from '../LivePreview/Toggler/index.js';
import { Locked } from '../Locked/index.js';
import { PermanentlyDeleteButton } from '../PermanentlyDeleteButton/index.js';
import { Popup, PopupList } from '../Popup/index.js';
import { PreviewButton } from '../PreviewButton/index.js';
import { PublishButton } from '../PublishButton/index.js';
import { RenderCustomComponent } from '../RenderCustomComponent/index.js';
import { RestoreButton } from '../RestoreButton/index.js';
import { SaveButton } from '../SaveButton/index.js';
import './index.scss';
import { SaveDraftButton } from '../SaveDraftButton/index.js';
import { Status } from '../Status/index.js';
const baseClass = 'doc-controls';
export const DocumentControls = /* Only available if `redirectAfterDuplicate` is `false` */ (props)=>{
    const { id, slug, BeforeDocumentControls, customComponents: { PreviewButton: CustomPreviewButton, PublishButton: CustomPublishButton, SaveButton: CustomSaveButton, SaveDraftButton: CustomSaveDraftButton } = {}, data, disableActions, disableCreate, EditMenuItems, hasSavePermission, isAccountView, isEditing, isInDrawer, isTrashed, onDelete, onDrawerCreateNew, onDuplicate, onRestore, onTakeOver, permissions, readOnlyForIncomingUser, redirectAfterDelete, redirectAfterDuplicate, redirectAfterRestore, user } = props;
    const { i18n, t } = useTranslation();
    const editDepth = useEditDepth();
    const { config, getEntityConfig } = useConfig();
    const collectionConfig = getEntityConfig({
        collectionSlug: slug
    });
    const globalConfig = getEntityConfig({
        globalSlug: slug
    });
    const { isLivePreviewEnabled } = useLivePreviewContext();
    const { admin: { dateFormat }, localization, routes: { admin: adminRoute }, serverURL } = config;
    // Settings these in state to avoid hydration issues if there is a mismatch between the server and client
    const [updatedAt, setUpdatedAt] = React.useState('');
    const [createdAt, setCreatedAt] = React.useState('');
    const processing = useFormProcessing();
    const initializing = useFormInitializing();
    useEffect(()=>{
        if (data?.updatedAt) {
            setUpdatedAt(formatDate({
                date: data.updatedAt,
                i18n,
                pattern: dateFormat
            }));
        }
        if (data?.createdAt) {
            setCreatedAt(formatDate({
                date: data.createdAt,
                i18n,
                pattern: dateFormat
            }));
        }
    }, [
        data,
        i18n,
        dateFormat
    ]);
    const hasCreatePermission = permissions && 'create' in permissions && permissions.create;
    const hasDeletePermission = permissions && 'delete' in permissions && permissions.delete;
    const showDotMenu = Boolean(collectionConfig && id && !disableActions && (hasCreatePermission || hasDeletePermission));
    const unsavedDraftWithValidations = !id && collectionConfig?.versions?.drafts && collectionConfig.versions?.drafts.validate;
    const globalHasDraftsEnabled = hasDraftsEnabled(globalConfig);
    const collectionHasDraftsEnabled = hasDraftsEnabled(collectionConfig);
    const collectionAutosaveEnabled = hasAutosaveEnabled(collectionConfig);
    const globalAutosaveEnabled = hasAutosaveEnabled(globalConfig);
    const autosaveEnabled = collectionAutosaveEnabled || globalAutosaveEnabled;
    const showSaveDraftButton = collectionAutosaveEnabled && collectionConfig.versions.drafts.autosave !== false && collectionConfig.versions.drafts.autosave.showSaveDraftButton === true || globalAutosaveEnabled && globalConfig.versions.drafts.autosave !== false && globalConfig.versions.drafts.autosave.showSaveDraftButton === true;
    const showCopyToLocale = localization && !collectionConfig?.admin?.disableCopyToLocale;
    const showFolderMetaIcon = collectionConfig && collectionConfig.folders;
    const showLockedMetaIcon = user && readOnlyForIncomingUser;
    return /*#__PURE__*/ _jsxDEV(Gutter, {
        className: baseClass,
        children: [
            /*#__PURE__*/ _jsxDEV("div", {
                className: `${baseClass}__wrapper`,
                children: [
                    /*#__PURE__*/ _jsxDEV("div", {
                        className: `${baseClass}__content`,
                        children: [
                            showLockedMetaIcon || showFolderMetaIcon ? /*#__PURE__*/ _jsxDEV("div", {
                                className: `${baseClass}__meta-icons`,
                                children: [
                                    showLockedMetaIcon && /*#__PURE__*/ _jsxDEV(Locked, {
                                        className: `${baseClass}__locked-controls`,
                                        user: user
                                    }, void 0, false, {
                                        fileName: "src/elements/DocumentControls/index.tsx",
                                        lineNumber: 182,
                                        columnNumber: 17
                                    }, this),
                                    showFolderMetaIcon && config.folders && !isTrashed && /*#__PURE__*/ _jsxDEV(MoveDocToFolder, {
                                        folderCollectionSlug: config.folders.slug,
                                        folderFieldName: config.folders.fieldName
                                    }, void 0, false, {
                                        fileName: "src/elements/DocumentControls/index.tsx",
                                        lineNumber: 185,
                                        columnNumber: 17
                                    }, this)
                                ]
                            }, void 0, true, {
                                fileName: "src/elements/DocumentControls/index.tsx",
                                lineNumber: 180,
                                columnNumber: 13
                            }, this) : null,
                            /*#__PURE__*/ _jsxDEV("ul", {
                                className: `${baseClass}__meta`,
                                children: [
                                    collectionConfig && !isEditing && !isAccountView && /*#__PURE__*/ _jsxDEV("li", {
                                        className: `${baseClass}__list-item`,
                                        children: /*#__PURE__*/ _jsxDEV("p", {
                                            className: `${baseClass}__value`,
                                            children: i18n.t('general:creatingNewLabel', {
                                                label: getTranslation(collectionConfig?.labels?.singular ?? i18n.t('general:document'), i18n)
                                            })
                                        }, void 0, false, {
                                            fileName: "src/elements/DocumentControls/index.tsx",
                                            lineNumber: 195,
                                            columnNumber: 17
                                        }, this)
                                    }, void 0, false, {
                                        fileName: "src/elements/DocumentControls/index.tsx",
                                        lineNumber: 194,
                                        columnNumber: 15
                                    }, this),
                                    (collectionHasDraftsEnabled || globalHasDraftsEnabled) && /*#__PURE__*/ _jsxDEV(Fragment, {
                                        children: [
                                            (globalConfig || collectionConfig && isEditing) && /*#__PURE__*/ _jsxDEV("li", {
                                                className: [
                                                    `${baseClass}__status`,
                                                    `${baseClass}__list-item`
                                                ].filter(Boolean).join(' '),
                                                children: /*#__PURE__*/ _jsxDEV(Status, {}, void 0, false, {
                                                    fileName: "src/elements/DocumentControls/index.tsx",
                                                    lineNumber: 213,
                                                    columnNumber: 21
                                                }, this)
                                            }, void 0, false, {
                                                fileName: "src/elements/DocumentControls/index.tsx",
                                                lineNumber: 208,
                                                columnNumber: 19
                                            }, this),
                                            hasSavePermission && autosaveEnabled && !unsavedDraftWithValidations && !isTrashed && /*#__PURE__*/ _jsxDEV("li", {
                                                className: `${baseClass}__list-item`,
                                                children: /*#__PURE__*/ _jsxDEV(Autosave, {
                                                    collection: collectionConfig,
                                                    global: globalConfig,
                                                    id: id,
                                                    publishedDocUpdatedAt: data?.createdAt
                                                }, void 0, false, {
                                                    fileName: "src/elements/DocumentControls/index.tsx",
                                                    lineNumber: 221,
                                                    columnNumber: 23
                                                }, this)
                                            }, void 0, false, {
                                                fileName: "src/elements/DocumentControls/index.tsx",
                                                lineNumber: 220,
                                                columnNumber: 21
                                            }, this)
                                        ]
                                    }, void 0, true, {
                                        fileName: "src/elements/DocumentControls/index.tsx",
                                        lineNumber: 206,
                                        columnNumber: 15
                                    }, this),
                                    collectionConfig?.timestamps && (isEditing || isAccountView) && /*#__PURE__*/ _jsxDEV(Fragment, {
                                        children: [
                                            /*#__PURE__*/ _jsxDEV("li", {
                                                className: [
                                                    `${baseClass}__list-item`,
                                                    `${baseClass}__value-wrap`
                                                ].filter(Boolean).join(' '),
                                                title: data?.updatedAt ? updatedAt : '',
                                                children: [
                                                    /*#__PURE__*/ _jsxDEV("p", {
                                                        className: `${baseClass}__label`,
                                                        children: [
                                                            i18n.t(isTrashed ? 'general:deleted' : 'general:lastModified'),
                                                            ": "
                                                        ]
                                                    }, void 0, true, {
                                                        fileName: "src/elements/DocumentControls/index.tsx",
                                                        lineNumber: 239,
                                                        columnNumber: 19
                                                    }, this),
                                                    data?.updatedAt && /*#__PURE__*/ _jsxDEV("p", {
                                                        className: `${baseClass}__value`,
                                                        children: updatedAt
                                                    }, void 0, false, {
                                                        fileName: "src/elements/DocumentControls/index.tsx",
                                                        lineNumber: 243,
                                                        columnNumber: 39
                                                    }, this)
                                                ]
                                            }, void 0, true, {
                                                fileName: "src/elements/DocumentControls/index.tsx",
                                                lineNumber: 233,
                                                columnNumber: 17
                                            }, this),
                                            /*#__PURE__*/ _jsxDEV("li", {
                                                className: [
                                                    `${baseClass}__list-item`,
                                                    `${baseClass}__value-wrap`
                                                ].filter(Boolean).join(' '),
                                                title: data?.createdAt ? createdAt : '',
                                                children: [
                                                    /*#__PURE__*/ _jsxDEV("p", {
                                                        className: `${baseClass}__label`,
                                                        children: [
                                                            i18n.t('general:created'),
                                                            ": "
                                                        ]
                                                    }, void 0, true, {
                                                        fileName: "src/elements/DocumentControls/index.tsx",
                                                        lineNumber: 251,
                                                        columnNumber: 19
                                                    }, this),
                                                    data?.createdAt && /*#__PURE__*/ _jsxDEV("p", {
                                                        className: `${baseClass}__value`,
                                                        children: createdAt
                                                    }, void 0, false, {
                                                        fileName: "src/elements/DocumentControls/index.tsx",
                                                        lineNumber: 252,
                                                        columnNumber: 39
                                                    }, this)
                                                ]
                                            }, void 0, true, {
                                                fileName: "src/elements/DocumentControls/index.tsx",
                                                lineNumber: 245,
                                                columnNumber: 17
                                            }, this)
                                        ]
                                    }, void 0, true, {
                                        fileName: "src/elements/DocumentControls/index.tsx",
                                        lineNumber: 232,
                                        columnNumber: 15
                                    }, this)
                                ]
                            }, void 0, true, {
                                fileName: "src/elements/DocumentControls/index.tsx",
                                lineNumber: 192,
                                columnNumber: 11
                            }, this)
                        ]
                    }, void 0, true, {
                        fileName: "src/elements/DocumentControls/index.tsx",
                        lineNumber: 178,
                        columnNumber: 9
                    }, this),
                    /*#__PURE__*/ _jsxDEV("div", {
                        className: `${baseClass}__controls-wrapper`,
                        children: [
                            /*#__PURE__*/ _jsxDEV("div", {
                                className: `${baseClass}__controls`,
                                children: [
                                    BeforeDocumentControls,
                                    isLivePreviewEnabled && !isInDrawer && /*#__PURE__*/ _jsxDEV(LivePreviewToggler, {}, void 0, false, {
                                        fileName: "src/elements/DocumentControls/index.tsx",
                                        lineNumber: 261,
                                        columnNumber: 53
                                    }, this),
                                    (collectionConfig?.admin.preview || globalConfig?.admin.preview) && /*#__PURE__*/ _jsxDEV(RenderCustomComponent, {
                                        CustomComponent: CustomPreviewButton,
                                        Fallback: /*#__PURE__*/ _jsxDEV(PreviewButton, {}, void 0, false, {
                                            fileName: "src/elements/DocumentControls/index.tsx",
                                            lineNumber: 265,
                                            columnNumber: 27
                                        }, void 0)
                                    }, void 0, false, {
                                        fileName: "src/elements/DocumentControls/index.tsx",
                                        lineNumber: 263,
                                        columnNumber: 15
                                    }, this),
                                    hasSavePermission && !isTrashed && /*#__PURE__*/ _jsxDEV(Fragment, {
                                        children: collectionHasDraftsEnabled || globalHasDraftsEnabled ? /*#__PURE__*/ _jsxDEV(Fragment, {
                                            children: [
                                                (unsavedDraftWithValidations || !autosaveEnabled || autosaveEnabled && showSaveDraftButton) && /*#__PURE__*/ _jsxDEV(RenderCustomComponent, {
                                                    CustomComponent: CustomSaveDraftButton,
                                                    Fallback: /*#__PURE__*/ _jsxDEV(SaveDraftButton, {}, void 0, false, {
                                                        fileName: "src/elements/DocumentControls/index.tsx",
                                                        lineNumber: 277,
                                                        columnNumber: 35
                                                    }, void 0)
                                                }, void 0, false, {
                                                    fileName: "src/elements/DocumentControls/index.tsx",
                                                    lineNumber: 275,
                                                    columnNumber: 23
                                                }, this),
                                                /*#__PURE__*/ _jsxDEV(RenderCustomComponent, {
                                                    CustomComponent: CustomPublishButton,
                                                    Fallback: /*#__PURE__*/ _jsxDEV(PublishButton, {}, void 0, false, {
                                                        fileName: "src/elements/DocumentControls/index.tsx",
                                                        lineNumber: 282,
                                                        columnNumber: 33
                                                    }, void 0)
                                                }, void 0, false, {
                                                    fileName: "src/elements/DocumentControls/index.tsx",
                                                    lineNumber: 280,
                                                    columnNumber: 21
                                                }, this)
                                            ]
                                        }, void 0, true, {
                                            fileName: "src/elements/DocumentControls/index.tsx",
                                            lineNumber: 271,
                                            columnNumber: 19
                                        }, this) : /*#__PURE__*/ _jsxDEV(RenderCustomComponent, {
                                            CustomComponent: CustomSaveButton,
                                            Fallback: /*#__PURE__*/ _jsxDEV(SaveButton, {}, void 0, false, {
                                                fileName: "src/elements/DocumentControls/index.tsx",
                                                lineNumber: 288,
                                                columnNumber: 31
                                            }, void 0)
                                        }, void 0, false, {
                                            fileName: "src/elements/DocumentControls/index.tsx",
                                            lineNumber: 286,
                                            columnNumber: 19
                                        }, this)
                                    }, void 0, false, {
                                        fileName: "src/elements/DocumentControls/index.tsx",
                                        lineNumber: 269,
                                        columnNumber: 15
                                    }, this),
                                    hasDeletePermission && isTrashed && /*#__PURE__*/ _jsxDEV(PermanentlyDeleteButton, {
                                        buttonId: "action-permanently-delete",
                                        collectionSlug: collectionConfig?.slug,
                                        id: id.toString(),
                                        onDelete: onDelete,
                                        redirectAfterDelete: redirectAfterDelete,
                                        singularLabel: collectionConfig?.labels?.singular
                                    }, void 0, false, {
                                        fileName: "src/elements/DocumentControls/index.tsx",
                                        lineNumber: 294,
                                        columnNumber: 15
                                    }, this),
                                    hasSavePermission && isTrashed && /*#__PURE__*/ _jsxDEV(RestoreButton, {
                                        buttonId: "action-restore",
                                        collectionSlug: collectionConfig?.slug,
                                        id: id.toString(),
                                        onRestore: onRestore,
                                        redirectAfterRestore: redirectAfterRestore,
                                        singularLabel: collectionConfig?.labels?.singular
                                    }, void 0, false, {
                                        fileName: "src/elements/DocumentControls/index.tsx",
                                        lineNumber: 304,
                                        columnNumber: 15
                                    }, this),
                                    user && readOnlyForIncomingUser && /*#__PURE__*/ _jsxDEV(Button, {
                                        buttonStyle: "secondary",
                                        id: "take-over",
                                        onClick: onTakeOver,
                                        size: "medium",
                                        type: "button",
                                        children: t('general:takeOver')
                                    }, void 0, false, {
                                        fileName: "src/elements/DocumentControls/index.tsx",
                                        lineNumber: 314,
                                        columnNumber: 15
                                    }, this)
                                ]
                            }, void 0, true, {
                                fileName: "src/elements/DocumentControls/index.tsx",
                                lineNumber: 259,
                                columnNumber: 11
                            }, this),
                            showDotMenu && !readOnlyForIncomingUser && /*#__PURE__*/ _jsxDEV(Popup, {
                                button: /*#__PURE__*/ _jsxDEV("div", {
                                    className: `${baseClass}__dots`,
                                    children: [
                                        /*#__PURE__*/ _jsxDEV("div", {}, void 0, false, {
                                            fileName: "src/elements/DocumentControls/index.tsx",
                                            lineNumber: 329,
                                            columnNumber: 19
                                        }, void 0),
                                        /*#__PURE__*/ _jsxDEV("div", {}, void 0, false, {
                                            fileName: "src/elements/DocumentControls/index.tsx",
                                            lineNumber: 330,
                                            columnNumber: 19
                                        }, void 0),
                                        /*#__PURE__*/ _jsxDEV("div", {}, void 0, false, {
                                            fileName: "src/elements/DocumentControls/index.tsx",
                                            lineNumber: 331,
                                            columnNumber: 19
                                        }, void 0)
                                    ]
                                }, void 0, true, {
                                    fileName: "src/elements/DocumentControls/index.tsx",
                                    lineNumber: 328,
                                    columnNumber: 17
                                }, void 0),
                                className: `${baseClass}__popup`,
                                disabled: initializing || processing,
                                horizontalAlign: "right",
                                size: "large",
                                verticalAlign: "bottom",
                                children: /*#__PURE__*/ _jsxDEV(PopupList.ButtonGroup, {
                                    children: [
                                        showCopyToLocale && /*#__PURE__*/ _jsxDEV(CopyLocaleData, {}, void 0, false, {
                                            fileName: "src/elements/DocumentControls/index.tsx",
                                            lineNumber: 341,
                                            columnNumber: 38
                                        }, this),
                                        hasCreatePermission && /*#__PURE__*/ _jsxDEV(React.Fragment, {
                                            children: [
                                                !disableCreate && /*#__PURE__*/ _jsxDEV(Fragment, {
                                                    children: editDepth > 1 ? /*#__PURE__*/ _jsxDEV(PopupList.Button, {
                                                        id: "action-create",
                                                        onClick: onDrawerCreateNew,
                                                        children: i18n.t('general:createNew')
                                                    }, void 0, false, {
                                                        fileName: "src/elements/DocumentControls/index.tsx",
                                                        lineNumber: 347,
                                                        columnNumber: 27
                                                    }, this) : /*#__PURE__*/ _jsxDEV(PopupList.Button, {
                                                        href: formatAdminURL({
                                                            adminRoute,
                                                            path: `/collections/${collectionConfig?.slug}/create`
                                                        }),
                                                        id: "action-create",
                                                        children: i18n.t('general:createNew')
                                                    }, void 0, false, {
                                                        fileName: "src/elements/DocumentControls/index.tsx",
                                                        lineNumber: 351,
                                                        columnNumber: 27
                                                    }, this)
                                                }, void 0, false, {
                                                    fileName: "src/elements/DocumentControls/index.tsx",
                                                    lineNumber: 345,
                                                    columnNumber: 23
                                                }, this),
                                                collectionConfig.disableDuplicate !== true && isEditing && /*#__PURE__*/ _jsxDEV(_Fragment, {
                                                    children: [
                                                        /*#__PURE__*/ _jsxDEV(DuplicateDocument, {
                                                            id: id,
                                                            onDuplicate: onDuplicate,
                                                            redirectAfterDuplicate: redirectAfterDuplicate,
                                                            singularLabel: collectionConfig?.labels?.singular,
                                                            slug: collectionConfig?.slug
                                                        }, void 0, false, {
                                                            fileName: "src/elements/DocumentControls/index.tsx",
                                                            lineNumber: 365,
                                                            columnNumber: 25
                                                        }, this),
                                                        localization && /*#__PURE__*/ _jsxDEV(DuplicateDocument, {
                                                            id: id,
                                                            onDuplicate: onDuplicate,
                                                            redirectAfterDuplicate: redirectAfterDuplicate,
                                                            selectLocales: true,
                                                            singularLabel: collectionConfig?.labels?.singular,
                                                            slug: collectionConfig?.slug
                                                        }, void 0, false, {
                                                            fileName: "src/elements/DocumentControls/index.tsx",
                                                            lineNumber: 373,
                                                            columnNumber: 27
                                                        }, this)
                                                    ]
                                                }, void 0, true)
                                            ]
                                        }, void 0, true, {
                                            fileName: "src/elements/DocumentControls/index.tsx",
                                            lineNumber: 343,
                                            columnNumber: 19
                                        }, this),
                                        hasDeletePermission && /*#__PURE__*/ _jsxDEV(DeleteDocument, {
                                            buttonId: "action-delete",
                                            collectionSlug: collectionConfig?.slug,
                                            id: id.toString(),
                                            onDelete: onDelete,
                                            redirectAfterDelete: redirectAfterDelete,
                                            singularLabel: collectionConfig?.labels?.singular,
                                            useAsTitle: collectionConfig?.admin?.useAsTitle
                                        }, void 0, false, {
                                            fileName: "src/elements/DocumentControls/index.tsx",
                                            lineNumber: 387,
                                            columnNumber: 19
                                        }, this),
                                        EditMenuItems
                                    ]
                                }, void 0, true, {
                                    fileName: "src/elements/DocumentControls/index.tsx",
                                    lineNumber: 340,
                                    columnNumber: 15
                                }, this)
                            }, void 0, false, {
                                fileName: "src/elements/DocumentControls/index.tsx",
                                lineNumber: 326,
                                columnNumber: 13
                            }, this)
                        ]
                    }, void 0, true, {
                        fileName: "src/elements/DocumentControls/index.tsx",
                        lineNumber: 258,
                        columnNumber: 9
                    }, this)
                ]
            }, void 0, true, {
                fileName: "src/elements/DocumentControls/index.tsx",
                lineNumber: 177,
                columnNumber: 7
            }, this),
            /*#__PURE__*/ _jsxDEV("div", {
                className: `${baseClass}__divider`
            }, void 0, false, {
                fileName: "src/elements/DocumentControls/index.tsx",
                lineNumber: 403,
                columnNumber: 7
            }, this)
        ]
    }, void 0, true, {
        fileName: "src/elements/DocumentControls/index.tsx",
        lineNumber: 176,
        columnNumber: 5
    }, this);
};

//# sourceMappingURL=index.js.map