"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.handlePaste = void 0;
const tslib_1 = require("tslib");
const ts_dedent_1 = tslib_1.__importDefault(require("ts-dedent"));
const _core_1 = require("../../../core/index.js");
const model_1 = require("../../../pm/model.js");
const clipboard_1 = require("../../../utils/clipboard.js");
const CodeBlockSpecs_1 = require("./CodeBlockSpecs/index.js");
const const_1 = require("./const.js");
const handlePaste = (view, e) => {
    if (!e.clipboardData || view.state.selection.$from.parent.type.spec.code)
        return false;
    const code = getCodeData(e.clipboardData);
    if (!code)
        return false;
    (0, _core_1.getLoggerFromState)(view.state).event({
        domEvent: 'paste',
        event: 'paste-from-code-editor',
        editor: code.editor,
        editorMode: code.mode,
        empty: !code.value,
        dataTypes: e.clipboardData.types,
    });
    const { tr, schema } = view.state;
    if (code.value) {
        const codeBlockNode = (0, const_1.codeBlockType)(schema).create({ [CodeBlockSpecs_1.CodeBlockNodeAttr.Lang]: code.mode }, schema.text(code.value));
        tr.replaceSelectionWith(codeBlockNode);
    }
    else {
        tr.replaceWith(tr.selection.from, tr.selection.to, model_1.Fragment.empty);
    }
    view.dispatch(tr.scrollIntoView());
    return true;
};
exports.handlePaste = handlePaste;
function getCodeData(data) {
    if (data.getData(clipboard_1.DataTransferType.Text)) {
        let editor = 'unknown';
        let mode;
        if ((0, clipboard_1.isVSCode)(data)) {
            editor = 'vscode';
            mode = (0, clipboard_1.tryParseVSCodeData)(data)?.mode;
        }
        else
            return null;
        return { editor, mode, value: (0, ts_dedent_1.default)(data.getData(clipboard_1.DataTransferType.Text)) };
    }
    return null;
}
//# sourceMappingURL=handle-paste.js.map
