import { jsxs as _jsxs, jsx as _jsx } from "react/jsx-runtime";
import { useToast } from "@vertesia/ui/core";
import { useUserSession } from "@vertesia/ui/session";
import { useCallback, useState } from "react";
import { SelectContentTypeModal } from "../../../types";
import { useObjectsActionCallback } from "../ObjectsActionContext";
export function ChangeTypeActionComponent({ action, objectIds, children }) {
    const { store } = useUserSession();
    const toast = useToast();
    const [isOpen, setOpen] = useState(false);
    const callback = useCallback(() => {
        setOpen(true);
        return Promise.resolve(true);
    }, []);
    useObjectsActionCallback(action.id, callback);
    const handleTypeSelect = (typeId) => {
        // Close the modal
        setOpen(false);
        // If no selection was made
        if (typeId === undefined) {
            return;
        }
        // Execute the operation with the selected typeId
        store.runOperation({
            name: "change_type",
            ids: objectIds,
            params: { typeId }
        }).then((r) => {
            toast({
                status: 'success',
                title: 'Change Type',
                description: `Change the type of ${objectIds.length} objects is ${r.status === 'in_progress' ? 'in progress' : 'completed'}`,
                duration: 2000
            });
        }).catch(err => {
            toast({
                status: 'error',
                title: 'Error changing type',
                description: err.message,
                duration: 5000
            });
        });
    };
    return (_jsxs("div", { children: [children, _jsx(SelectContentTypeModal, { isOpen: isOpen, onClose: handleTypeSelect, title: "Change Content Type", confirmLabel: "Change Type", children: _jsxs("div", { className: "text-sm flex flex-col gap-x-2", children: [_jsxs("p", { children: ["Choose a new Content Type for the selected object", objectIds.length > 1 ? 's' : ' ', "or let the system choose or generate a type based on the content."] }), _jsxs("p", { className: "pt-2 text-xs", children: ["Note: This action will raise the ", _jsx("code", { children: "change_type" }), " event and will trigger the standard intake workflows which may reset the object properties."] })] }) })] }));
}
export const ChangeTypeAction = {
    id: "changeType",
    name: 'Change Content Type',
    description: 'Change the Content Type of the selected documents',
    confirm: false,
    component: ChangeTypeActionComponent,
};
//# sourceMappingURL=ChangeTypeAction.js.map