import { jsxs as _jsxs, jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
import { Button, Popover, PopoverContent, PopoverTrigger, SelectList } from '@vertesia/ui/core';
import clsx from 'clsx';
import { EllipsisVertical, X } from 'lucide-react';
import { useState } from "react";
import { DocumentUploadModal, useDocumentSelection } from "../../../store";
import { ExportPropertiesAction } from "./actions/ExportPropertiesAction";
import { StartWorkflowAction } from "./actions/StartWorkflowComponent";
import { ObjectsActionContextProvider, useObjectsActionContext } from "./ObjectsActionContext";
export function SelectionActions() {
    const selection = useDocumentSelection();
    const size = selection.size();
    const plural = size > 1 ? "s" : "";
    const hasSelection = selection?.hasSelection();
    const hasSingleSelection = selection?.isSingleSelection();
    const onClearSelection = () => {
        selection?.removeAll();
    };
    return (_jsx(ObjectsActionContextProvider, { children: _jsxs("div", { className: "flex items-center gap-x-2", children: [hasSelection && !hasSingleSelection &&
                    _jsxs("div", { className: "flex items-center gap-x-1 shrink-0", children: [_jsxs("div", { className: 'text-sm nowrap', children: [size, " document", plural, " selected"] }), _jsx(Button, { title: "Clear selection", variant: "ghost", onClick: onClearSelection, children: _jsx(X, { className: "size-4" }) })] }), _jsx(SelectionActionsPopover, { selection: selection, children: _jsx(Button, { variant: "ghost", alt: "More action", size: "sm", children: _jsx(EllipsisVertical, { size: 16 }) }) }), hasSelection && _jsx(ActionsWrapper, { selection: selection })] }) }));
}
function ActionsWrapper({}) {
    return _jsx(StartWorkflowButton, {});
}
export function UploadObjectsButton({ collectionId }) {
    const [files, setFiles] = useState([]);
    const selection = useDocumentSelection();
    const hasSelection = selection?.hasSelection();
    const selectFile = () => {
        const fileInput = document.createElement("input");
        fileInput.type = "file";
        fileInput.multiple = true;
        fileInput.accept = "*";
        fileInput?.click();
        fileInput.onchange = (event) => {
            const files = event.target.files;
            if (files) {
                setFiles(Array.from(files));
            }
        };
    };
    return (!hasSelection &&
        _jsxs(_Fragment, { children: [_jsx(Button, { onClick: () => selectFile(), children: "Upload" }), _jsx(DocumentUploadModal, { collectionId: collectionId ?? '', isOpen: files.length > 0, onClose: () => setFiles([]), files: files, title: "Upload Files", onUploadComplete: (result) => {
                        if (result && result.success) {
                            setFiles([]);
                        }
                    }, children: _jsx("div", { className: "text-sm", children: "Select the associated Content Type, or let the system choose or generate the type based on the content." }) })] }));
}
function StartWorkflowButton() {
    const ctx = useObjectsActionContext();
    const selection = ctx.params.selection;
    const hasSelection = selection.hasSelection();
    return (hasSelection &&
        _jsx(Button, { onClick: () => ctx.run(StartWorkflowAction.id), children: "Start Workflow" }));
}
function optionLayout(option) {
    return {
        label: option.name,
        className: clsx('flex-1 px-2 py-2 hover:bg-accent nowrap', option.destructive ? 'text-destructive' : ''),
    };
}
function SelectionActionsPopover({ selection, children }) {
    const context = useObjectsActionContext();
    const executeAction = (action) => {
        context.run(action.id);
    };
    return (_jsxs(Popover, { hover: true, children: [_jsx(PopoverTrigger, { children: children }), _jsx(PopoverContent, { className: 'p-0 w-50', align: 'end', sideOffset: 6, children: _jsx(PopoverBody, { executeAction: executeAction, selection: selection }) })] }));
}
function PopoverBody({ executeAction, selection }) {
    const context = useObjectsActionContext();
    const _executeAction = (action) => {
        executeAction(action);
    };
    const _selection = selection?.hasSelection() ? context.actions.filter(action => !action.hideInList) : [ExportPropertiesAction];
    return (_jsx("div", { className: "rounded-md shadow-md py-2", children: _jsx("div", { className: "px-1 text-sm", children: _jsx(SelectList, { options: _selection, optionLayout: optionLayout, onChange: _executeAction, noCheck: true }) }) }));
}
//# sourceMappingURL=SelectionActions.js.map