'use client';
import * as facelessUIImport from '@faceless-ui/modal';
import { getTranslation } from '@payloadcms/translations';
import { useRouter } from 'next/navigation.js';
import React, { useCallback, useState } from 'react';
import { toast } from 'react-toastify';
import { useForm } from '../../forms/Form/context.js';
import { useConfig } from '../../providers/Config/index.js';
import { useDocumentInfo } from '../../providers/DocumentInfo/index.js';
import { useTranslation } from '../../providers/Translation/index.js';
import { MinimalTemplate } from '../../templates/Minimal/index.js';
import { requests } from '../../utilities/api.js';
import { Button } from '../Button/index.js';
import * as PopupList from '../Popup/PopupButtonList/index.js';
import { Translation } from '../Translation/index.js';
const baseClass = 'delete-document';
const DeleteDocument = (props)=>{
    const { id, buttonId, collectionSlug, singularLabel, title: titleFromProps } = props;
    const { Modal, useModal } = facelessUIImport;
    const { routes: { admin, api }, serverURL } = useConfig();
    const { setModified } = useForm();
    const [deleting, setDeleting] = useState(false);
    const { toggleModal } = useModal();
    const router = useRouter();
    const { i18n, t } = useTranslation();
    const { title } = useDocumentInfo();
    const titleToRender = titleFromProps || title || id;
    const modalSlug = `delete-${id}`;
    const addDefaultError = useCallback(()=>{
        setDeleting(false);
        toast.error(t('error:deletingTitle', {
            title
        }));
    }, [
        t,
        title
    ]);
    const handleDelete = useCallback(async ()=>{
        setDeleting(true);
        setModified(false);
        try {
            await requests.delete(`${serverURL}${api}/${collectionSlug}/${id}`, {
                headers: {
                    'Accept-Language': i18n.language,
                    'Content-Type': 'application/json'
                }
            }).then(async (res)=>{
                try {
                    const json = await res.json();
                    if (res.status < 400) {
                        setDeleting(false);
                        toggleModal(modalSlug);
                        toast.success(json.message || t('general:titleDeleted', {
                            label: getTranslation(singularLabel, i18n),
                            title
                        }));
                        return router.push(`${admin}/collections/${collectionSlug}`);
                    }
                    toggleModal(modalSlug);
                    if (json.errors) {
                        json.errors.forEach((error)=>toast.error(error.message));
                    } else {
                        addDefaultError();
                    }
                    return false;
                } catch (e) {
                    return addDefaultError();
                }
            });
        } catch (e) {
            addDefaultError();
        }
    }, [
        setModified,
        serverURL,
        api,
        collectionSlug,
        id,
        toggleModal,
        modalSlug,
        t,
        singularLabel,
        i18n,
        title,
        router,
        admin,
        addDefaultError
    ]);
    if (id) {
        return /*#__PURE__*/ React.createElement(React.Fragment, null, /*#__PURE__*/ React.createElement(PopupList.Button, {
            id: buttonId,
            onClick: ()=>{
                setDeleting(false);
                toggleModal(modalSlug);
            }
        }, t('general:delete')), /*#__PURE__*/ React.createElement(Modal, {
            className: baseClass,
            slug: modalSlug
        }, /*#__PURE__*/ React.createElement(MinimalTemplate, {
            className: `${baseClass}__template`
        }, /*#__PURE__*/ React.createElement("h1", null, t('general:confirmDeletion')), /*#__PURE__*/ React.createElement("p", null, /*#__PURE__*/ React.createElement(Translation, {
            elements: {
                '1': ({ children })=>/*#__PURE__*/ React.createElement("strong", null, children)
            },
            i18nKey: "general:aboutToDelete",
            t: t,
            variables: {
                label: getTranslation(singularLabel, i18n),
                title: titleToRender
            }
        })), /*#__PURE__*/ React.createElement("div", {
            className: `${baseClass}__actions`
        }, /*#__PURE__*/ React.createElement(Button, {
            buttonStyle: "secondary",
            id: "confirm-cancel",
            onClick: deleting ? undefined : ()=>toggleModal(modalSlug),
            type: "button"
        }, t('general:cancel')), /*#__PURE__*/ React.createElement(Button, {
            id: "confirm-delete",
            onClick: deleting ? undefined : handleDelete
        }, deleting ? t('general:deleting') : t('general:confirm'))))));
    }
    return null;
};
export default DeleteDocument;

//# sourceMappingURL=index.js.map