'use client';
import { jsxDEV as _jsxDEV } from "react/jsx-dev-runtime";
import { useModal } from '@faceless-ui/modal';
import { getTranslation } from '@payloadcms/translations';
import { useRouter } from 'next/navigation.js';
import { formatAdminURL } from 'payload/shared';
import React, { useCallback } from 'react';
import { toast } from 'sonner';
import { CheckboxField } from '../../fields/Checkbox/index.js';
import { SelectInput } from '../../fields/Select/index.js';
import { useFormModified } from '../../forms/Form/context.js';
import { useConfig } from '../../providers/Config/index.js';
import { useDocumentInfo } from '../../providers/DocumentInfo/index.js';
import { useLocale } from '../../providers/Locale/index.js';
import { useRouteTransition } from '../../providers/RouteTransition/index.js';
import { useServerFunctions } from '../../providers/ServerFunctions/index.js';
import { useTranslation } from '../../providers/Translation/index.js';
import { DrawerHeader } from '../BulkUpload/Header/index.js';
import { Button } from '../Button/index.js';
import { Drawer } from '../Drawer/index.js';
import { PopupList } from '../Popup/index.js';
import './index.scss';
const baseClass = 'copy-locale-data';
const drawerSlug = 'copy-locale';
export const CopyLocaleData = ()=>{
    const { config: { localization, routes: { admin } } } = useConfig();
    const { code } = useLocale();
    const { id, collectionSlug, globalSlug } = useDocumentInfo();
    const { i18n, t } = useTranslation();
    const modified = useFormModified();
    const { toggleModal } = useModal();
    const { copyDataFromLocale } = useServerFunctions();
    const router = useRouter();
    const { startRouteTransition } = useRouteTransition();
    const localeOptions = localization && localization.locales.map((locale)=>({
            label: locale.label,
            value: locale.code
        })) || [];
    const localeOptionsWithoutCurrent = localeOptions.filter((locale)=>locale.value !== code);
    const getLocaleLabel = (code)=>{
        const locale = localization && localization.locales.find((l)=>l.code === code);
        return locale && locale.label ? getTranslation(locale.label, i18n) : code;
    };
    const [copying, setCopying] = React.useState(false);
    const [toLocale, setToLocale] = React.useState(null);
    const [fromLocale, setFromLocale] = React.useState(code);
    const [overwriteExisting, setOverwriteExisting] = React.useState(false);
    React.useEffect(()=>{
        if (fromLocale !== code) {
            setFromLocale(code);
        }
    }, [
        code,
        fromLocale
    ]);
    const copyLocaleData = useCallback(async ({ from, to })=>{
        setCopying(true);
        try {
            await copyDataFromLocale({
                collectionSlug,
                docID: id,
                fromLocale: from,
                globalSlug,
                overrideData: overwriteExisting,
                toLocale: to
            });
            setCopying(false);
            startRouteTransition(()=>router.push(formatAdminURL({
                    adminRoute: admin,
                    path: `/${collectionSlug ? `collections/${collectionSlug}/${id}` : `globals/${globalSlug}`}`
                }) + `?locale=${to}`));
            toggleModal(drawerSlug);
        } catch (error) {
            setCopying(false);
            const errorMessage = error.message || 'error:unknown';
            const translatedMessage = errorMessage.startsWith('error:') ? t(errorMessage) : errorMessage;
            toast.error(translatedMessage);
        }
    }, [
        copyDataFromLocale,
        collectionSlug,
        id,
        globalSlug,
        overwriteExisting,
        toggleModal,
        router,
        admin,
        startRouteTransition,
        t
    ]);
    if (!id && !globalSlug) {
        return null;
    }
    return /*#__PURE__*/ _jsxDEV(React.Fragment, {
        children: [
            /*#__PURE__*/ _jsxDEV(PopupList.Button, {
                id: `${baseClass}__button`,
                onClick: ()=>{
                    if (modified) {
                        toast.info(t('general:unsavedChanges'));
                    } else {
                        toggleModal(drawerSlug);
                    }
                },
                children: t('localization:copyToLocale')
            }, void 0, false, {
                fileName: "src/elements/CopyLocaleData/index.tsx",
                lineNumber: 122,
                columnNumber: 7
            }, this),
            /*#__PURE__*/ _jsxDEV(Drawer, {
                className: baseClass,
                gutter: false,
                Header: /*#__PURE__*/ _jsxDEV(DrawerHeader, {
                    onClose: ()=>{
                        toggleModal(drawerSlug);
                    },
                    title: t('localization:copyToLocale')
                }, void 0, false, {
                    fileName: "src/elements/CopyLocaleData/index.tsx",
                    lineNumber: 138,
                    columnNumber: 11
                }, void 0),
                slug: drawerSlug,
                children: [
                    /*#__PURE__*/ _jsxDEV("div", {
                        className: `${baseClass}__sub-header`,
                        children: [
                            /*#__PURE__*/ _jsxDEV("span", {
                                children: fromLocale && toLocale ? /*#__PURE__*/ _jsxDEV("div", {
                                    children: t('localization:copyFromTo', {
                                        from: getLocaleLabel(fromLocale),
                                        to: getLocaleLabel(toLocale)
                                    })
                                }, void 0, false, {
                                    fileName: "src/elements/CopyLocaleData/index.tsx",
                                    lineNumber: 150,
                                    columnNumber: 15
                                }, this) : t('localization:selectLocaleToCopy')
                            }, void 0, false, {
                                fileName: "src/elements/CopyLocaleData/index.tsx",
                                lineNumber: 148,
                                columnNumber: 11
                            }, this),
                            /*#__PURE__*/ _jsxDEV(Button, {
                                buttonStyle: "primary",
                                disabled: !fromLocale || !toLocale,
                                iconPosition: "left",
                                onClick: async ()=>{
                                    if (fromLocale === toLocale) {
                                        toast.error(t('localization:cannotCopySameLocale'));
                                        return;
                                    }
                                    if (!copying) {
                                        await copyLocaleData({
                                            from: fromLocale,
                                            to: toLocale
                                        });
                                    }
                                },
                                size: "medium",
                                children: copying ? t('general:copying') + '...' : t('general:copy')
                            }, void 0, false, {
                                fileName: "src/elements/CopyLocaleData/index.tsx",
                                lineNumber: 160,
                                columnNumber: 11
                            }, this)
                        ]
                    }, void 0, true, {
                        fileName: "src/elements/CopyLocaleData/index.tsx",
                        lineNumber: 147,
                        columnNumber: 9
                    }, this),
                    /*#__PURE__*/ _jsxDEV("div", {
                        className: `${baseClass}__content`,
                        children: [
                            /*#__PURE__*/ _jsxDEV(SelectInput, {
                                label: t('localization:copyFrom'),
                                name: 'fromLocale',
                                onChange: (selectedOption)=>{
                                    if (selectedOption?.value) {
                                        setFromLocale(selectedOption.value);
                                    }
                                },
                                options: localeOptions,
                                path: "fromLocale",
                                readOnly: true,
                                value: fromLocale
                            }, void 0, false, {
                                fileName: "src/elements/CopyLocaleData/index.tsx",
                                lineNumber: 183,
                                columnNumber: 11
                            }, this),
                            /*#__PURE__*/ _jsxDEV(SelectInput, {
                                label: t('localization:copyTo'),
                                name: "toLocale",
                                onChange: (selectedOption)=>{
                                    if (selectedOption?.value) {
                                        setToLocale(selectedOption.value);
                                    }
                                },
                                options: localeOptionsWithoutCurrent,
                                path: "toLocale",
                                value: toLocale
                            }, void 0, false, {
                                fileName: "src/elements/CopyLocaleData/index.tsx",
                                lineNumber: 196,
                                columnNumber: 11
                            }, this),
                            /*#__PURE__*/ _jsxDEV(CheckboxField, {
                                checked: overwriteExisting,
                                field: {
                                    name: 'overwriteExisting',
                                    label: t('general:overwriteExistingData')
                                },
                                onChange: ()=>setOverwriteExisting(!overwriteExisting),
                                path: 'overwriteExisting'
                            }, void 0, false, {
                                fileName: "src/elements/CopyLocaleData/index.tsx",
                                lineNumber: 208,
                                columnNumber: 11
                            }, this)
                        ]
                    }, void 0, true, {
                        fileName: "src/elements/CopyLocaleData/index.tsx",
                        lineNumber: 182,
                        columnNumber: 9
                    }, this)
                ]
            }, void 0, true, {
                fileName: "src/elements/CopyLocaleData/index.tsx",
                lineNumber: 134,
                columnNumber: 7
            }, this)
        ]
    }, void 0, true, {
        fileName: "src/elements/CopyLocaleData/index.tsx",
        lineNumber: 121,
        columnNumber: 5
    }, this);
};

//# sourceMappingURL=index.js.map