'use client';


import { cn } from "../../../utils/cn.mjs";
import { ButtonColor, ButtonVariant } from "../../Button/Button.mjs";
import { useAuth } from "../../../hooks/useAuth/useAuth.mjs";
import { useDeleteDictionary, usePushDictionaries, useWriteDictionary } from "../../../hooks/reactQuery.mjs";
import { Modal, ModalSize } from "../../Modal/Modal.mjs";
import { Form } from "../../Form/Form.mjs";
import { ArrowUpFromLine, Download, RotateCcw, Save, Trash } from "lucide-react";
import { useState } from "react";
import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
import { useIntlayer } from "react-intlayer";
import { useDictionariesRecordActions, useEditedContent } from "@intlayer/editor-react";

//#region src/components/DictionaryFieldEditor/SaveForm/SaveForm.tsx
const SaveForm = ({ dictionary, mode, className, onDelete, onSave,...props }) => {
	const [isFormatAlertModalOpen, setIsFormatAlertModalOpen] = useState(false);
	const { setLocaleDictionary } = useDictionariesRecordActions();
	const { mutate: deleteDictionary, isPending: isDeleting } = useDeleteDictionary();
	const { mutate: writeDictionary, isPending: isWriting } = useWriteDictionary();
	const { mutate: pushDictionaries, isPending: isPushing } = usePushDictionaries();
	const isLoading = isWriting || isPushing;
	const { editedContent, restoreEditedContent } = useEditedContent();
	const { deleteButton, resetButton, saveButton, publishButton, downloadButton, confirmation } = useIntlayer("save-dictionary-details");
	const { isAuthenticated } = useAuth();
	const editedDictionary = editedContent?.[dictionary.localId];
	const isEdited = editedDictionary && JSON.stringify(editedDictionary) !== JSON.stringify(dictionary);
	const isDistantDictionary = typeof dictionary?.id !== "undefined";
	const handleSaveDictionaryConfirmation = async () => {
		if (!editedContent?.[dictionary.localId]) return;
		writeDictionary({ dictionary: {
			...dictionary,
			...editedContent?.[dictionary.localId]
		} }, { onSuccess: () => {
			setLocaleDictionary(editedContent?.[dictionary.localId]);
			restoreEditedContent(dictionary.localId);
			onSave?.();
		} });
	};
	const handlePushDictionary = () => {
		if (!editedContent?.[dictionary.localId]) return;
		pushDictionaries([{
			...dictionary,
			...editedContent?.[dictionary.localId]
		}], { onSuccess: (res) => {
			if (res) {
				setLocaleDictionary(editedContent?.[dictionary.localId]);
				restoreEditedContent(dictionary.localId);
				onSave?.();
			}
		} });
	};
	const handleDeleteDictionary = () => {
		if (!dictionary.id) return;
		deleteDictionary(dictionary.id.toString(), { onSuccess: (res) => {
			if (res) onDelete?.();
		} });
	};
	return /* @__PURE__ */ jsxs(Fragment$1, { children: [/* @__PURE__ */ jsx(Modal, {
		isOpen: isFormatAlertModalOpen,
		title: confirmation.title.value,
		size: ModalSize.MD,
		children: /* @__PURE__ */ jsxs("form", {
			className: "size-full px-3",
			children: [/* @__PURE__ */ jsx("p", {
				className: "py-4 text-neutral text-sm",
				children: confirmation.message
			}), /* @__PURE__ */ jsxs("div", {
				className: "mt-12 flex justify-end gap-2 max-md:flex-col",
				children: [/* @__PURE__ */ jsx(Form.Button, {
					label: confirmation.cancelButton.label.value,
					disabled: !isEdited || isLoading,
					color: ButtonColor.TEXT,
					className: "max-md:w-full",
					variant: ButtonVariant.OUTLINE,
					onClick: () => setIsFormatAlertModalOpen(false),
					children: confirmation.cancelButton.text
				}), /* @__PURE__ */ jsx(Form.Button, {
					label: confirmation.confirmButton.label.value,
					disabled: !isEdited || isLoading,
					Icon: Save,
					color: ButtonColor.TEXT,
					className: "max-md:w-full",
					isLoading: isPushing,
					onClick: handleSaveDictionaryConfirmation,
					children: confirmation.confirmButton.text
				})]
			})]
		})
	}), /* @__PURE__ */ jsxs("form", {
		className: cn("flex justify-end gap-2 max-md:flex-col", className),
		...props,
		children: [
			mode.includes("remote") && isDistantDictionary && onDelete && isAuthenticated && /* @__PURE__ */ jsx(Form.Button, {
				label: deleteButton.label.value,
				Icon: Trash,
				color: ButtonColor.ERROR,
				variant: ButtonVariant.OUTLINE,
				className: "max-md:w-full",
				isLoading: isDeleting,
				onClick: handleDeleteDictionary,
				children: deleteButton.text
			}),
			isEdited && /* @__PURE__ */ jsx(Form.Button, {
				label: resetButton.label.value,
				disabled: !isEdited,
				Icon: RotateCcw,
				variant: ButtonVariant.OUTLINE,
				color: ButtonColor.TEXT,
				className: "max-md:w-full",
				onClick: () => restoreEditedContent(dictionary.localId),
				children: resetButton.text
			}),
			mode.includes("local") && /* @__PURE__ */ jsx(Form.Button, {
				label: downloadButton.label.value,
				disabled: !isEdited || isLoading,
				Icon: Download,
				color: ButtonColor.TEXT,
				variant: isAuthenticated ? ButtonVariant.OUTLINE : ButtonVariant.DEFAULT,
				className: "max-md:w-full",
				isLoading: isWriting,
				onClick: () => setIsFormatAlertModalOpen(true),
				children: downloadButton.text
			}),
			mode.includes("remote") && isAuthenticated && !isDistantDictionary && /* @__PURE__ */ jsx(Form.Button, {
				label: publishButton.label.value,
				disabled: isLoading,
				Icon: ArrowUpFromLine,
				color: ButtonColor.TEXT,
				className: "max-md:w-full",
				isLoading: isPushing,
				onClick: handlePushDictionary,
				children: publishButton.text
			}),
			mode.includes("remote") && isAuthenticated && isDistantDictionary && isEdited && /* @__PURE__ */ jsx(Form.Button, {
				label: saveButton.label.value,
				disabled: !isEdited || isLoading,
				Icon: Save,
				color: "text",
				className: "max-md:w-full",
				isLoading: isPushing,
				onClick: handlePushDictionary,
				children: saveButton.text
			})
		]
	})] });
};

//#endregion
export { SaveForm };
//# sourceMappingURL=SaveForm.mjs.map