var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
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, selectionToOffset } from "../../utils/prosemirror";
export function WebClipSettings(props) {
    const { editor } = props;
    const isBottom = useToolbarLocation() === "bottom";
    if (!editor.isActive("webclip") || !isBottom)
        return null;
    return (_jsx(MoreTools, Object.assign({}, props, { autoCloseOnUnmount: true, popupId: "webclipSettings", tools: ["webclipFullScreen", "webclipOpenSource"] })));
}
export function WebClipFullScreen(props) {
    const { editor } = props;
    return (_jsx(ToolButton, Object.assign({}, props, { toggled: false, onClick: () => {
            const offset = selectionToOffset(editor.state);
            if (!offset)
                return;
            const dom = editor.view.nodeDOM(offset.from);
            if (!dom || !(dom instanceof HTMLElement))
                return;
            const iframe = dom.querySelector("iframe");
            if (!iframe)
                return;
            iframe.requestFullscreen();
            editor.commands.updateAttributes("webclip", {
                fullscreen: true
            });
        } })));
}
export function WebClipOpenExternal(props) {
    const { editor } = props;
    return (_jsx(ToolButton, Object.assign({}, props, { toggled: false, onClick: () => __awaiter(this, void 0, void 0, function* () {
            var _a, _b;
            const offset = selectionToOffset(editor.state);
            if (!offset)
                return;
            const dom = editor.view.nodeDOM(offset.from);
            if (!dom || !(dom instanceof HTMLElement))
                return;
            const iframe = dom.querySelector("iframe");
            if (!iframe || !iframe.contentDocument)
                return;
            const url = URL.createObjectURL(new Blob(["\ufeff", iframe.contentDocument.documentElement.outerHTML], { type: "text/html" }));
            (_b = (_a = editor.storage).openLink) === null || _b === void 0 ? void 0 : _b.call(_a, url);
        }) })));
}
export function WebClipOpenSource(props) {
    const { editor } = props;
    return (_jsx(ToolButton, Object.assign({}, props, { toggled: false, onClick: () => __awaiter(this, void 0, void 0, function* () {
            var _a, _b;
            const node = findSelectedNode(editor, "webclip");
            if (!node)
                return;
            (_b = (_a = editor.storage).openLink) === null || _b === void 0 ? void 0 : _b.call(_a, node.attrs.src);
        }) })));
}
