'use client';
import { jsxDEV as _jsxDEV } from "react/jsx-dev-runtime";
import { useCallback } from 'react';
import { toast } from 'sonner';
import { MoreIcon } from '../../icons/More/index.js';
import { useTranslation } from '../../providers/Translation/index.js';
import { Popup, PopupList } from '../Popup/index.js';
import { ClipboardActionLabel } from './ClipboardActionLabel.js';
import { clipboardCopy, clipboardPaste } from './clipboardUtilities.js';
const baseClass = 'clipboard-action';
/**
 * Menu actions for copying and pasting fields. Currently, this is only used in Arrays and Blocks.
 * @note This component doesn't use the Clipboard API, but localStorage. See rationale in #11513
 */ export const ClipboardAction = ({ allowCopy, allowPaste, className, copyClassName, disabled, isRow, onPaste, pasteClassName, path, ...rest })=>{
    const { t } = useTranslation();
    const classes = [
        `${baseClass}__popup`,
        className
    ].filter(Boolean).join(' ');
    const handleCopy = useCallback(()=>{
        const clipboardResult = clipboardCopy({
            path,
            t,
            ...rest
        });
        if (typeof clipboardResult === 'string') {
            toast.error(clipboardResult);
        } else {
            toast.success(t('general:copied'));
        }
    }, [
        t,
        rest,
        path
    ]);
    const handlePaste = useCallback(()=>{
        const clipboardResult = clipboardPaste(rest.type === 'array' ? {
            onPaste,
            path,
            schemaFields: rest.fields,
            t
        } : {
            onPaste,
            path,
            schemaBlocks: rest.blocks,
            t
        });
        if (typeof clipboardResult === 'string') {
            toast.error(clipboardResult);
        }
    }, [
        onPaste,
        rest,
        path,
        t
    ]);
    if (!allowPaste && !allowCopy) {
        return null;
    }
    return /*#__PURE__*/ _jsxDEV(Popup, {
        button: /*#__PURE__*/ _jsxDEV(MoreIcon, {}, void 0, false, {
            fileName: "src/elements/ClipboardAction/index.tsx",
            lineNumber: 92,
            columnNumber: 15
        }, void 0),
        className: classes,
        disabled: disabled,
        horizontalAlign: "center",
        render: ({ close })=>/*#__PURE__*/ _jsxDEV(PopupList.ButtonGroup, {
                children: [
                    /*#__PURE__*/ _jsxDEV(PopupList.Button, {
                        className: copyClassName,
                        disabled: !allowCopy,
                        onClick: ()=>{
                            void handleCopy();
                            close();
                        },
                        children: /*#__PURE__*/ _jsxDEV(ClipboardActionLabel, {
                            isRow: isRow
                        }, void 0, false, {
                            fileName: "src/elements/ClipboardAction/index.tsx",
                            lineNumber: 106,
                            columnNumber: 13
                        }, void 0)
                    }, void 0, false, {
                        fileName: "src/elements/ClipboardAction/index.tsx",
                        lineNumber: 98,
                        columnNumber: 11
                    }, void 0),
                    /*#__PURE__*/ _jsxDEV(PopupList.Button, {
                        className: pasteClassName,
                        disabled: !allowPaste,
                        onClick: ()=>{
                            void handlePaste();
                            close();
                        },
                        children: /*#__PURE__*/ _jsxDEV(ClipboardActionLabel, {
                            isPaste: true,
                            isRow: isRow
                        }, void 0, false, {
                            fileName: "src/elements/ClipboardAction/index.tsx",
                            lineNumber: 116,
                            columnNumber: 13
                        }, void 0)
                    }, void 0, false, {
                        fileName: "src/elements/ClipboardAction/index.tsx",
                        lineNumber: 108,
                        columnNumber: 11
                    }, void 0)
                ]
            }, void 0, true, {
                fileName: "src/elements/ClipboardAction/index.tsx",
                lineNumber: 97,
                columnNumber: 9
            }, void 0),
        size: "large",
        verticalAlign: "bottom"
    }, void 0, false, {
        fileName: "src/elements/ClipboardAction/index.tsx",
        lineNumber: 91,
        columnNumber: 5
    }, this);
};

//# sourceMappingURL=index.js.map