import { Extension, getStyleProperty } from '@tiptap/core';
export const BlockLineHeight = Extension.create({
    name: 'lineHeight',
    addOptions() {
        return {
            types: ['paragraph', 'heading']
        };
    },
    addGlobalAttributes() {
        return [
            {
                types: this.options.types,
                attributes: {
                    lineHeight: {
                        default: null,
                        parseHTML: (element) => getStyleProperty(element, 'line-height') ?? element.style.lineHeight,
                        renderHTML: (attributes) => {
                            if (!attributes.lineHeight)
                                return {};
                            return {
                                style: `line-height: ${attributes.lineHeight}`
                            };
                        }
                    }
                }
            }
        ];
    },
    addCommands() {
        const setBlockLineHeight = (lineHeight) => ({ state, tr, dispatch }) => {
            const { selection } = state;
            const types = this.options.types;
            let changed = false;
            function updateNode(pos, node) {
                if (!types.includes(node.type.name))
                    return;
                if (node.attrs.lineHeight === lineHeight)
                    return;
                tr.setNodeMarkup(pos, undefined, {
                    ...node.attrs,
                    lineHeight
                });
                changed = true;
            }
            if (selection.empty) {
                for (let depth = selection.$from.depth; depth > 0; depth -= 1) {
                    const node = selection.$from.node(depth);
                    if (types.includes(node.type.name)) {
                        updateNode(selection.$from.before(depth), node);
                        break;
                    }
                }
            }
            else {
                tr.doc.nodesBetween(selection.from, selection.to, (node, pos) => {
                    if (types.includes(node.type.name)) {
                        updateNode(pos, node);
                        return false;
                    }
                    return true;
                });
            }
            if (!changed)
                return true;
            if (dispatch)
                dispatch(tr.scrollIntoView());
            return true;
        };
        return {
            setLineHeight: (lineHeight) => setBlockLineHeight(lineHeight),
            unsetLineHeight: () => setBlockLineHeight(null)
        };
    }
});
