/* eslint-disable @typescript-eslint/no-use-before-define */ import { message } from "antd";
import { ELEMENT_TAGS } from "../../plugins/insertParsedHtmlNodes";
export var TEXT_TAGS = {
    CODE: function() {
        return {
            code: true
        };
    },
    DEL: function() {
        return {
            strikethrough: true
        };
    },
    EM: function() {
        return {
            italic: true
        };
    },
    I: function() {
        return {
            italic: true
        };
    },
    S: function() {
        return {
            strikethrough: true
        };
    },
    B: function() {
        return {
            bold: true
        };
    },
    U: function() {
        return {
            underline: true
        };
    }
};
export var makeDeserializer = function(jsx) {
    var deserialize = function(el, imageTags) {
        if (el.attributes && el.attributes.getNamedItem('class') && el.attributes.getNamedItem('class').value.match(/done/g)) {
            return null;
        }
        if (isList(el)) {
            return deserializeList(el, imageTags);
        }
        return deserializeElement(el, imageTags);
    };
    function deserializeList(el, imageTags) {
        var siblings = getSiblings(el);
        var type = 'UL';
        var list_wrapper = document.createElement(type);
        for(var i = 0; i < siblings.length; i++){
            list_wrapper.appendChild(siblings[i]);
        }
        var attrs = ELEMENT_TAGS[type]();
        var children = Array.from(list_wrapper.childNodes).map(function(child) {
            return deserializeListItem(child, imageTags);
        }).flat();
        return jsx('element', attrs, children);
    }
    function deserializeElement(el, imageTags) {
        var _el_childNodes, _el_childNodes_, _el_childNodes1;
        if (el.nodeType === 3) {
            if (el.parentNode.nodeName === 'O:P') {
                if (el.parentNode.parentNode.nodeName === 'P') {
                    return el.textContent;
                }
            }
            if (el.textContent.match(/^[\s]*$/gm)) {
                var _el_textContent;
                return ((_el_textContent = el.textContent) === null || _el_textContent === void 0 ? void 0 : _el_textContent.trim()) || null;
            } else {
                // sometimes work adds line breaks when pasting
                var regex = /\n(?!\n)/g;
                el.textContent = el.textContent.replace(regex, ' ');
                return el.textContent;
            }
        } else if (el.nodeType !== 1) {
            return null;
        } else if (el.nodeName === 'BR') {
            return '\n';
        }
        var nodeName = el.nodeName;
        var parent = el;
        if (nodeName === 'PRE' && ((_el_childNodes = el.childNodes) === null || _el_childNodes === void 0 ? void 0 : _el_childNodes[0]) && ((_el_childNodes1 = el.childNodes) === null || _el_childNodes1 === void 0 ? void 0 : (_el_childNodes_ = _el_childNodes1[0]) === null || _el_childNodes_ === void 0 ? void 0 : _el_childNodes_.nodeName) === 'CODE') {
            var _el_childNodes2;
            parent = (_el_childNodes2 = el.childNodes) === null || _el_childNodes2 === void 0 ? void 0 : _el_childNodes2[0];
        }
        var children = Array.from(parent.childNodes).map(function(child) {
            return deserialize(child, imageTags);
        }).flat();
        if (el.nodeName === 'BODY') {
            var filler = jsx('element', {
                type: 'paragraph',
                className: 'P'
            }, [
                {
                    text: ' '
                }
            ]);
            children.unshift(filler);
            return jsx('fragment', {}, children);
        }
        if (ELEMENT_TAGS[nodeName]) {
            var _ELEMENT_TAGS_nodeName;
            if (nodeName === 'IMG') {
                var src = el.getAttribute('src');
                if (imageTags[src]) {
                    el.setAttribute('src', imageTags[src]);
                    children = [
                        {
                            text: ''
                        }
                    ];
                }
                var attrs = ELEMENT_TAGS[nodeName](el);
                return jsx('element', attrs, (attrs === null || attrs === void 0 ? void 0 : attrs.children) || children);
            }
            if (nodeName === 'H3' || nodeName === 'H2' || nodeName === 'H1') {
                return jsx('element', {
                    type: 'head',
                    className: nodeName,
                    level: nodeName === null || nodeName === void 0 ? void 0 : nodeName.replace('H', '')
                }, children);
            }
            var attrs1 = (_ELEMENT_TAGS_nodeName = ELEMENT_TAGS[nodeName]) === null || _ELEMENT_TAGS_nodeName === void 0 ? void 0 : _ELEMENT_TAGS_nodeName.call(ELEMENT_TAGS, el);
            return jsx('element', attrs1, children);
        }
        if (TEXT_TAGS[nodeName]) {
            var _TEXT_TAGS_nodeName;
            var attrs2 = (_TEXT_TAGS_nodeName = TEXT_TAGS[nodeName]) === null || _TEXT_TAGS_nodeName === void 0 ? void 0 : _TEXT_TAGS_nodeName.call(TEXT_TAGS);
            try {
                return children.filter(Boolean).map(function(child) {
                    if (typeof child === 'string') {
                        return jsx('text', attrs2, child);
                    }
                    return jsx('element', ELEMENT_TAGS.P(), child);
                });
            } catch (error) {
                console.error(error);
            }
        }
        return children;
    }
    function deserializeListItem(el, imageTags) {
        var level = el.getAttribute('style');
        var content = extractTextFromNodes(el).map(function(c) {
            try {
                return deserializeElement(c, imageTags);
            } catch (error) {
                message.error('Error deserializing list item:' + c.innerHTML);
                console.error(error);
            }
            return jsx('element', {
                type: 'paragraph'
            }, [
                {
                    text: c.innerHTML
                }
            ]);
        }).flat();
        return jsx('element', {
            type: 'list-item',
            className: 'level'.concat(level)
        }, content);
    }
    return deserialize;
};
// gets ALL the
function getSiblings(el) {
    // const level = el.attributes.getNamedItem('style').value.match(/level(\d+)/)[1]
    var siblings = [];
    while(el && el.attributes.getNamedItem('class') && el.attributes.getNamedItem('class').value.match(/MsoListParagraph/g)){
        var _el_attributes_getNamedItem_value_match, _el_attributes_getNamedItem_value, _el_attributes_getNamedItem, _el_attributes;
        var level = (el === null || el === void 0 ? void 0 : (_el_attributes = el.attributes) === null || _el_attributes === void 0 ? void 0 : (_el_attributes_getNamedItem = _el_attributes.getNamedItem('style')) === null || _el_attributes_getNamedItem === void 0 ? void 0 : (_el_attributes_getNamedItem_value = _el_attributes_getNamedItem.value) === null || _el_attributes_getNamedItem_value === void 0 ? void 0 : (_el_attributes_getNamedItem_value_match = _el_attributes_getNamedItem_value.match(/level(\d+)/)) === null || _el_attributes_getNamedItem_value_match === void 0 ? void 0 : _el_attributes_getNamedItem_value_match[1]) || '4';
        el.setAttribute('class', 'done'); // we set this attribute to avoid getting stuck in an infinite loop
        el.setAttribute('style', level);
        siblings.push(el);
        // eslint-disable-next-line no-param-reassign
        el = el.nextElementSibling;
    }
    return siblings;
}
// Docx lists begin with "MsoListParagraph".
function isList(el) {
    if (el.attributes && el.attributes.getNamedItem('class') && el.attributes.getNamedItem('class').value.match(/MsoListParagraph/g)) {
        return true;
    }
    return false;
}
// receives a list item and returns the text inside it
// sometimes the text will be inside a text tag or inside a span tag.
// when it is inside a text tag, the span is irrelevant, but it contains empty text inside
function extractTextFromNodes(el) {
    var children = Array.from(el.childNodes);
    var result = [];
    children.forEach(function(child) {
        if (child.nodeName in TEXT_TAGS || child.nodeName === '#text') {
            result.push(child);
        } else if (child.nodeName === 'SPAN') {
            child.textContent = child.textContent.replace(/(^(\W)(?=\s)*)|(o\s)(?!\w)/gm, '');
            result.push(child);
        }
    });
    return result;
}
