"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.handlePasteIntoCodePlugin = void 0;
exports.isInsideCode = isInsideCode;
exports.isInsideBlockCode = isInsideBlockCode;
exports.isInsideInlineCode = isInsideInlineCode;
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 state_1 = require("../../../pm/state.js");
const utils_1 = require("./utils.js");
const isCodeMark = (mark) => mark.type.spec.code;
function isInsideCode(state) {
    if (isInsideBlockCode(state))
        return 'block';
    if (isInsideInlineCode(state))
        return 'inline';
    return false;
}
function isInsideBlockCode(state) {
    // it is enough to check only $from
    // when pasting, the content is inserted into a block with the type from $from
    return Boolean(state.selection.$from.parent.type.spec.code);
}
function isInsideInlineCode(state) {
    // same as for block code
    const fromHasCodeMark = state.selection.$from.marks().some(isCodeMark);
    if (state.selection.empty) {
        return fromHasCodeMark || (state.storedMarks ?? []).some(isCodeMark);
    }
    return fromHasCodeMark;
}
/**
 * This plugin handles paste into any type of code: code block or code mark.
 * If selection is inside code, it always prevents execution of all next paste handlers.
 * It takes a value from following clipboard data types: uri-list, files or text data.
 */
const handlePasteIntoCodePlugin = () => {
    return new state_1.Plugin({
        props: {
            handleDOMEvents: {
                paste(view, event) {
                    if (!event.clipboardData)
                        return false;
                    const { clipboardData } = event;
                    const codeType = isInsideCode(view.state);
                    if (!codeType)
                        return false;
                    let text;
                    let dataFormat;
                    if ((0, utils_1.isIosSafariShare)(clipboardData)) {
                        dataFormat = utils_1.DataTransferType.UriList;
                        text = clipboardData.getData(utils_1.DataTransferType.UriList);
                    }
                    else if ((0, utils_1.isFilesOnly)(clipboardData)) {
                        dataFormat = utils_1.DataTransferType.Files;
                        text = Array.from(clipboardData.files)
                            .map((file) => file.name)
                            .join(' ');
                    }
                    else {
                        dataFormat = utils_1.DataTransferType.Text;
                        text = (0, ts_dedent_1.default)(clipboardData.getData(utils_1.DataTransferType.Text));
                    }
                    if (codeType === 'inline') {
                        text = text.replaceAll('\n', '↵');
                    }
                    const { state, dispatch } = view;
                    (0, _core_1.getLoggerFromState)(state).event({
                        codeType,
                        dataFormat,
                        empty: !text,
                        domEvent: 'paste',
                        event: 'paste-into-code',
                        dataTypes: clipboardData.types,
                    });
                    event.preventDefault();
                    const { tr } = state;
                    if (text)
                        tr.replaceSelectionWith(state.schema.text(text), true);
                    else
                        tr.replaceWith(tr.selection.from, tr.selection.to, model_1.Fragment.empty);
                    dispatch(tr.scrollIntoView());
                    return true;
                },
            },
        },
    });
};
exports.handlePasteIntoCodePlugin = handlePasteIntoCodePlugin;
//# sourceMappingURL=code.js.map
