"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.codeBlockLineWrappingPlugin = exports.isNodeHasLineWrapping = exports.hasLineWrappingDecoration = void 0;
exports.enableLineWrapping = enableLineWrapping;
exports.disableLineWrapping = disableLineWrapping;
const state_1 = require("../../../../../pm/state.js");
const view_1 = require("../../../../../pm/view.js");
const CodeBlockSpecs_1 = require("../../CodeBlockSpecs/index.js");
const pluginKey = new state_1.PluginKey('code_block_line_wrapping');
const WRAPPING_SPEC_KEY = '__code_block_line_wrapping';
const WRAPPING_SPEC_VALUE = true;
const isLineWrappingDecoration = (deco) => deco.spec?.[WRAPPING_SPEC_KEY] === WRAPPING_SPEC_VALUE;
/** @internal */
const hasLineWrappingDecoration = (decorations) => {
    return decorations.some(isLineWrappingDecoration);
};
exports.hasLineWrappingDecoration = hasLineWrappingDecoration;
/** @internal */
const isNodeHasLineWrapping = (state, pos) => {
    const node = state.doc.nodeAt(pos);
    if (!node || node.type !== (0, CodeBlockSpecs_1.codeBlockType)(state.doc.type.schema))
        return false;
    const decos = pluginKey.getState(state)?.decorations.find(pos + 1, pos + node.nodeSize - 1);
    return decos?.some(isLineWrappingDecoration) ?? false;
};
exports.isNodeHasLineWrapping = isNodeHasLineWrapping;
/** @internal */
function enableLineWrapping(tr, pos) {
    return tr.setMeta(pluginKey, { type: 'add', pos });
}
/** @internal */
function disableLineWrapping(tr, pos) {
    return tr.setMeta(pluginKey, { type: 'remove', pos });
}
/** @internal */
const codeBlockLineWrappingPlugin = () => {
    return new state_1.Plugin({
        key: pluginKey,
        state: {
            init() {
                return {
                    decorations: view_1.DecorationSet.empty,
                };
            },
            apply(tr, value) {
                let decorations = value.decorations.map(tr.mapping, tr.doc);
                if (tr.getMeta(pluginKey)) {
                    const meta = tr.getMeta(pluginKey);
                    if (meta.type === 'add') {
                        const node = tr.doc.nodeAt(meta.pos);
                        if (node?.type === (0, CodeBlockSpecs_1.codeBlockType)(tr.doc.type.schema)) {
                            const from = meta.pos;
                            const to = meta.pos + node.nodeSize;
                            decorations = decorations.add(tr.doc, [
                                view_1.Decoration.node(from, to, {}, { [WRAPPING_SPEC_KEY]: WRAPPING_SPEC_VALUE }),
                            ]);
                        }
                    }
                    else if (meta.type === 'remove') {
                        const node = tr.doc.nodeAt(meta.pos);
                        if (node?.type === (0, CodeBlockSpecs_1.codeBlockType)(tr.doc.type.schema)) {
                            const start = meta.pos + 1;
                            const end = meta.pos + node.nodeSize - 1;
                            const found = decorations.find(start, end);
                            decorations = decorations.remove(found);
                        }
                    }
                }
                return { decorations };
            },
        },
        props: {
            decorations(state) {
                return pluginKey.getState(state)?.decorations;
            },
        },
    });
};
exports.codeBlockLineWrappingPlugin = codeBlockLineWrappingPlugin;
//# sourceMappingURL=codeBlockLineWrappingPlugin.js.map
