import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useUserSession } from "@vertesia/ui/session";
import { Button, VModal, VModalBody, VModalFooter, VModalTitle, SelectList, useToast } from "@vertesia/ui/core";
import { useCallback, useState } from "react";
import { useObjectsActionCallback, useObjectsActionContext } from "../ObjectsActionContext";
export function StartWorkflowComponent({ action, objectIds, collectionId }) {
    const toast = useToast();
    const { client } = useUserSession();
    const [isOpen, setOpen] = useState(false);
    const callback = useCallback(() => {
        setOpen(true);
        return Promise.resolve(true);
    }, []);
    const onStartWorkflow = (workflowId) => {
        setOpen(false);
        if (!workflowId) {
            return;
        }
        // const objectIds = params.selection.getObjectIds();
        // const workflowId = params.action.id;
        return client.store.workflows.rules
            .execute(workflowId, objectIds, { collection_id: collectionId })
            .then(() => {
            toast({
                title: "Workflow started",
                status: "success",
                duration: 3000,
            });
        })
            .catch((err) => {
            toast({
                title: "Error starting workflow",
                status: "error",
                description: err.message,
                duration: 9000,
            });
        });
    };
    useObjectsActionCallback(action.id, callback);
    return _jsx(StartWorkflowModal, { isOpen: isOpen, onClose: onStartWorkflow });
}
export const StartWorkflowAction = {
    id: "startWorkflow",
    name: "Start Workflow",
    description: "Start an workflow on the selected objects",
    confirm: false,
    hideInList: true,
    component: StartWorkflowComponent,
};
function StartWorkflowModal({ isOpen, onClose }) {
    return (_jsxs(VModal, { onClose: () => onClose(undefined), isOpen: isOpen, className: "", children: [_jsx(VModalTitle, { children: "Start a Workflow by Rule" }), _jsx(StartWorkflowBody, { onClose: onClose })] }));
}
function optionLayout(option) {
    return {
        label: (_jsxs("div", { children: [_jsx("div", { children: option.name }), _jsx("div", { className: "text-sm text-muted", children: option.description })] })),
        reverse: true,
    };
}
function StartWorkflowBody({ onClose }) {
    const [selected, setSelected] = useState(undefined);
    const context = useObjectsActionContext();
    const onSelect = (value) => {
        setSelected(value);
    };
    const onStart = () => {
        if (selected) {
            onClose(selected.id);
        }
    };
    return (_jsxs("div", { children: [_jsxs(VModalBody, { children: [_jsx("div", { className: "pb-2", children: "Choose a workflow rule to start:" }), _jsx("div", { className: "max-h-[420px] overflow-y-scroll border-border border rounded-md", children: _jsx(SelectList, { options: context.wfRules, optionLayout: optionLayout, onChange: onSelect, value: selected }) })] }), _jsxs(VModalFooter, { children: [_jsx(Button, { variant: "secondary", onClick: () => onClose(), children: "Cancel" }), _jsx(Button, { onClick: onStart, isDisabled: !selected, children: "Start" })] })] }));
}
//# sourceMappingURL=StartWorkflowComponent.js.map