import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Button, DialogDescription, Heading, Portal, useToast, VModal, VModalBody, VModalFooter, VModalTitle, VTabs, VTabsBar, VTabsPanel } from "@vertesia/ui/core";
import { useUserSession } from "@vertesia/ui/session";
import { useCallback, useState } from "react";
import { CreateCollectionForm, SelectCollection } from "../../../collections";
import { useObjectsActionCallback } from "../ObjectsActionContext";
export function AddToCollectionActionComponent({ action, objectIds }) {
    const [showModal, setShowModal] = useState(false);
    const callback = useCallback(() => {
        if (objectIds.length > 0) {
            setShowModal(true);
        }
        return Promise.resolve(true);
    }, [objectIds]);
    const onClose = () => {
        setShowModal(false);
    };
    useObjectsActionCallback(action.id, callback);
    return showModal && _jsx(Portal, { children: _jsx(SelectCollectionModal, { objectIds: objectIds, isOpen: showModal, onClose: onClose }) });
}
export const AddToCollectionAction = {
    id: "addToCollection",
    name: 'Add to Collection',
    description: 'Add documents to a collection',
    confirm: false,
    component: AddToCollectionActionComponent,
};
function SelectCollectionModal({ isOpen, onClose, objectIds }) {
    return (_jsxs(VModal, { isOpen: isOpen, onClose: onClose, className: "max-w-lg w-full min-w-0 overflow-hidden", children: [_jsx(VModalTitle, { className: "flex flex-col min-w-0 overflow-hidden", children: "Add to a Collection" }), _jsx(DialogDescription, { className: "min-w-0 overflow-hidden", children: "Add the selected objects to an existing collection or create a new one." }), _jsx("div", { className: "min-w-0 max-w-full overflow-hidden", children: _jsx(AddToCollectionForm, { onClose: onClose, objectIds: objectIds }) })] }));
}
function AddToCollectionForm({ onClose, objectIds }) {
    const toast = useToast();
    const { client } = useUserSession();
    const [selectedCollectionId, setSelectedCollectionId] = useState();
    const onAddToCollection = ({ collectionId }) => {
        if (!collectionId || !objectIds?.length) {
            return;
        }
        client.store.collections.addMembers(collectionId, objectIds).then(() => {
            toast({
                title: 'Add to collection success',
                status: 'success',
                description: `Added ${objectIds.length} objects to the selected collection`,
                duration: 3000
            });
            onClose();
        }).catch(() => {
            toast({
                title: 'Add to collection failure',
                status: 'error',
                description: `Failed to add the selected objects to the collection`,
                duration: 5000
            });
        });
    };
    const onCollectionChange = (collectionId, _collection) => {
        if (typeof collectionId === "string" || typeof collectionId === "undefined") {
            setSelectedCollectionId(collectionId);
        }
        else if (Array.isArray(collectionId) && collectionId.length > 0) {
            setSelectedCollectionId(collectionId[0]);
        }
        else {
            setSelectedCollectionId(undefined);
        }
    };
    const tabs = [
        {
            name: 'select',
            label: 'Select Collection',
            content: (_jsxs("div", { className: "p-4 min-w-0 max-w-full overflow-hidden", children: [_jsx(Heading, { level: 5, children: "Choose from existing collections" }), _jsx(VModalBody, { className: "min-w-0 max-w-full overflow-hidden", children: _jsx("div", { className: "mb-4 min-w-0 max-w-full overflow-hidden", children: _jsx(SelectCollection, { onChange: onCollectionChange, value: selectedCollectionId }) }) }), _jsx(VModalFooter, { children: _jsx(Button, { isDisabled: !selectedCollectionId, onClick: () => selectedCollectionId && onAddToCollection({ collectionId: selectedCollectionId }), children: "Add to Collection" }) })] }))
        },
        {
            name: 'create',
            label: 'Create new',
            content: _jsx("div", { className: "p-4", children: _jsx(CreateCollectionForm, { onClose: onClose, onAddToCollection: (id) => onAddToCollection({ collectionId: id }), redirect: false }) })
        }
    ];
    return (_jsxs(VTabs, { defaultValue: "select", tabs: tabs, fullWidth: true, children: [_jsx(VTabsBar, {}), _jsx(VTabsPanel, {})] }));
}
//# sourceMappingURL=AddToCollectionAction.js.map