import { jsx as _jsx } from "react/jsx-runtime";
import { ToolButton } from "../components/tool-button";
import { MoreTools } from "../components/more-tools";
import { useToolbarLocation } from "../stores/toolbar-store";
import { findSelectedNode } from "../../utils/prosemirror";
export function AttachmentSettings(props) {
    const { editor } = props;
    const isBottom = useToolbarLocation() === "bottom";
    if (!editor.isActive("attachment") || !isBottom)
        return null;
    return (_jsx(MoreTools, Object.assign({}, props, { autoCloseOnUnmount: true, popupId: "attachmentSettings", tools: editor.isEditable
            ? ["downloadAttachment"]
            : ["downloadAttachment", "removeAttachment"] })));
}
export function DownloadAttachment(props) {
    const { editor } = props;
    return (_jsx(ToolButton, Object.assign({}, props, { toggled: false, onClick: () => {
            var _a, _b;
            const attachmentNode = findSelectedNode(editor, "attachment") ||
                findSelectedNode(editor, "image");
            const attachment = ((attachmentNode === null || attachmentNode === void 0 ? void 0 : attachmentNode.attrs) || {});
            (_b = (_a = editor.storage).downloadAttachment) === null || _b === void 0 ? void 0 : _b.call(_a, attachment);
        } })));
}
export function PreviewAttachment(props) {
    const { editor } = props;
    const attachmentNode = findSelectedNode(editor, "attachment") || findSelectedNode(editor, "image");
    const attachment = ((attachmentNode === null || attachmentNode === void 0 ? void 0 : attachmentNode.attrs) || {});
    if (!editor.isActive("image") && !canPreviewAttachment(attachment))
        return null;
    return (_jsx(ToolButton, Object.assign({}, props, { toggled: false, onClick: () => {
            var _a, _b;
            const attachmentNode = findSelectedNode(editor, "attachment") ||
                findSelectedNode(editor, "image");
            const attachment = ((attachmentNode === null || attachmentNode === void 0 ? void 0 : attachmentNode.attrs) || {});
            (_b = (_a = editor.storage).previewAttachment) === null || _b === void 0 ? void 0 : _b.call(_a, attachment);
        } })));
}
export function RemoveAttachment(props) {
    const { editor } = props;
    return (_jsx(ToolButton, Object.assign({}, props, { toggled: false, onClick: () => editor.chain().focus().removeAttachment().run() })));
}
const previewableFileExtensions = ["pdf"];
const previewableMimeTypes = ["application/pdf"];
function canPreviewAttachment(attachment) {
    var _a;
    if (!attachment)
        return false;
    if (attachment.mime &&
        previewableMimeTypes.some((mime) => attachment.mime.startsWith(mime)))
        return true;
    const extension = (_a = attachment.filename) === null || _a === void 0 ? void 0 : _a.split(".").pop();
    if (!extension)
        return false;
    return previewableFileExtensions.indexOf(extension) > -1;
}
