"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.handlePaste = void 0;
exports.insertCode = insertCode;
exports.getCodeData = getCodeData;
exports.isInlineCode = isInlineCode;
const tslib_1 = require("tslib");
const ts_dedent_1 = tslib_1.__importDefault(require("ts-dedent"));
const _core_1 = require("../../../core/index.js");
const clipboard_1 = require("../../../utils/clipboard.js");
const const_1 = require("./const.js");
const handlePaste = (view, e) => {
    const data = e.clipboardData;
    if (!data)
        return false;
    const code = getCodeData(data);
    if (!code)
        return false;
    const { state } = view;
    const { tr, schema, selection } = state;
    const $from = selection.$from;
    const inCodeBlock = Boolean($from.parent.type.spec.code);
    (0, _core_1.getLoggerFromState)(state).event({
        domEvent: 'paste',
        event: 'paste-from-code-editor',
        editor: code.editor,
        editorMode: code.mode,
        empty: !code.value,
        inline: code.inline,
        dataTypes: Array.from(data.types),
    });
    if (!code.value) {
        return false;
    }
    insertCode({
        tr,
        schema,
        code,
        from: selection.from,
        to: selection.to,
        inCodeBlock,
    });
    view.dispatch(tr.scrollIntoView());
    e.preventDefault();
    return true;
};
exports.handlePaste = handlePaste;
function insertCode({ tr, schema, code, from, to, inCodeBlock }) {
    if (inCodeBlock) {
        tr.insertText(code.value, from, to);
    }
    else if (code.inline) {
        insertInlineCode(tr, schema, code.value);
    }
    else {
        insertCodeBlock(tr, schema, code.value);
    }
}
function insertInlineCode(tr, schema, value) {
    const codeMarkType = schema.marks.code;
    const marks = codeMarkType ? [codeMarkType.create()] : undefined;
    const textNode = schema.text(value, marks);
    tr.replaceSelectionWith(textNode, false);
}
function insertCodeBlock(tr, schema, value) {
    const nodeType = (0, const_1.codeBlockType)(schema);
    const textNode = schema.text(value);
    const codeBlockNode = nodeType.create(null, textNode);
    tr.replaceSelectionWith(codeBlockNode);
}
function getCodeData(data) {
    const text = data.getData(clipboard_1.DataTransferType.Text);
    if (!text)
        return null;
    if ((0, clipboard_1.isVSCode)(data)) {
        return processVSCodePaste(data, text);
    }
    if ((0, clipboard_1.isJetBrains)(data)) {
        return processJetBrainsPaste(text);
    }
    return null;
}
function processVSCodePaste(data, text) {
    const vsCodeData = (0, clipboard_1.tryParseVSCodeData)(data);
    return {
        editor: 'vscode',
        value: (0, ts_dedent_1.default)(text),
        inline: isInlineCode(text),
        mode: vsCodeData?.mode,
    };
}
function processJetBrainsPaste(text) {
    return {
        editor: 'jetbrains',
        value: (0, ts_dedent_1.default)(text),
        inline: isInlineCode(text),
    };
}
function isInlineCode(text) {
    return !text.includes('\n');
}
//# sourceMappingURL=handle-paste.js.map
