function _array_like_to_array(arr, len) {
    if (len == null || len > arr.length) len = arr.length;
    for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
    return arr2;
}
function _array_with_holes(arr) {
    if (Array.isArray(arr)) return arr;
}
function _array_without_holes(arr) {
    if (Array.isArray(arr)) return _array_like_to_array(arr);
}
function _iterable_to_array(iter) {
    if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
}
function _iterable_to_array_limit(arr, i) {
    var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
    if (_i == null) return;
    var _arr = [];
    var _n = true;
    var _d = false;
    var _s, _e;
    try {
        for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
            _arr.push(_s.value);
            if (i && _arr.length === i) break;
        }
    } catch (err) {
        _d = true;
        _e = err;
    } finally{
        try {
            if (!_n && _i["return"] != null) _i["return"]();
        } finally{
            if (_d) throw _e;
        }
    }
    return _arr;
}
function _non_iterable_rest() {
    throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _non_iterable_spread() {
    throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _sliced_to_array(arr, i) {
    return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
}
function _to_consumable_array(arr) {
    return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
}
function _unsupported_iterable_to_array(o, minLen) {
    if (!o) return;
    if (typeof o === "string") return _array_like_to_array(o, minLen);
    var n = Object.prototype.toString.call(o).slice(8, -1);
    if (n === "Object" && o.constructor) n = o.constructor.name;
    if (n === "Map" || n === "Set") return Array.from(n);
    if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
}
import { Editor, Element, Node, Path } from "slate";
import { EditorUtils } from "../utils/editorUtils";
var htmlReg = /<[a-z]+[\s"'=:;()\w\-[\]/.]*\/?>(.*<\/[a-z]+>:?)?/g;
var linkReg = /(https?|ftp):\/\/[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]/gi;
export var cacheTextNode = new WeakMap();
export var clearInlineKatex = function(editor) {
    var inlineMath = Array.from(Editor.nodes(editor, {
        match: function(n) {
            return n.type === 'inline-katex';
        },
        at: []
    }));
    inlineMath.map(function(c) {
        return cacheTextNode.delete(c[0]);
    });
};
var highlightNodes = new Set([
    'paragraph',
    'table-cell',
    'table',
    'code',
    'head',
    'inline-katex'
]);
export function useHighlight(store) {
    return function(param) {
        var _param = _sliced_to_array(param, 2), node = _param[0], path = _param[1];
        if (Element.isElement(node) && highlightNodes.has(node.type)) {
            var ranges = (store === null || store === void 0 ? void 0 : store.highlightCache.get(node)) || [];
            var cacheText = cacheTextNode.get(node);
            // footnote
            if ([
                'paragraph',
                'table-cell'
            ].includes(node.type)) {
                for(var i = 0; i < node.children.length; i++){
                    var c = node.children[i];
                    if (c.text && !EditorUtils.isDirtLeaf(c)) {
                        if (cacheText && Path.equals(cacheText.path, path)) {
                            var _ranges;
                            (_ranges = ranges).push.apply(_ranges, _to_consumable_array(cacheText.range));
                        } else {
                            var _ranges1;
                            var textRanges = [];
                            var matchHtml = c.text.matchAll(htmlReg);
                            var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
                            try {
                                for(var _iterator = matchHtml[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
                                    var m = _step.value;
                                    textRanges.push({
                                        anchor: {
                                            path: _to_consumable_array(path).concat([
                                                i
                                            ]),
                                            offset: m.index
                                        },
                                        focus: {
                                            path: _to_consumable_array(path).concat([
                                                i
                                            ]),
                                            offset: m.index + m[0].length
                                        },
                                        html: true
                                    });
                                }
                            } catch (err) {
                                _didIteratorError = true;
                                _iteratorError = err;
                            } finally{
                                try {
                                    if (!_iteratorNormalCompletion && _iterator.return != null) {
                                        _iterator.return();
                                    }
                                } finally{
                                    if (_didIteratorError) {
                                        throw _iteratorError;
                                    }
                                }
                            }
                            var match = c.text.matchAll(/\[\^.+?]:?/g);
                            var _iteratorNormalCompletion1 = true, _didIteratorError1 = false, _iteratorError1 = undefined;
                            try {
                                for(var _iterator1 = match[Symbol.iterator](), _step1; !(_iteratorNormalCompletion1 = (_step1 = _iterator1.next()).done); _iteratorNormalCompletion1 = true){
                                    var m1 = _step1.value;
                                    if (typeof m1.index !== 'number') continue;
                                    textRanges.push({
                                        anchor: {
                                            path: _to_consumable_array(path).concat([
                                                i
                                            ]),
                                            offset: m1.index
                                        },
                                        focus: {
                                            path: _to_consumable_array(path).concat([
                                                i
                                            ]),
                                            offset: m1.index + m1[0].length
                                        },
                                        fnc: !m1[0].endsWith(':'),
                                        fnd: m1[0].endsWith(':')
                                    });
                                }
                            } catch (err) {
                                _didIteratorError1 = true;
                                _iteratorError1 = err;
                            } finally{
                                try {
                                    if (!_iteratorNormalCompletion1 && _iterator1.return != null) {
                                        _iterator1.return();
                                    }
                                } finally{
                                    if (_didIteratorError1) {
                                        throw _iteratorError1;
                                    }
                                }
                            }
                            cacheTextNode.set(node, {
                                path: path,
                                range: textRanges
                            });
                            (_ranges1 = ranges).push.apply(_ranges1, _to_consumable_array(textRanges));
                        }
                    }
                    if (c.text && !c.url && !c.docId && !c.hash) {
                        var _ranges2;
                        var textRanges1 = [];
                        var links = c.text.matchAll(linkReg);
                        var _iteratorNormalCompletion2 = true, _didIteratorError2 = false, _iteratorError2 = undefined;
                        try {
                            for(var _iterator2 = links[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true){
                                var m2 = _step2.value;
                                textRanges1.push({
                                    anchor: {
                                        path: _to_consumable_array(path).concat([
                                            i
                                        ]),
                                        offset: m2.index
                                    },
                                    focus: {
                                        path: _to_consumable_array(path).concat([
                                            i
                                        ]),
                                        offset: m2.index + m2[0].length
                                    },
                                    link: m2[0]
                                });
                            }
                        } catch (err) {
                            _didIteratorError2 = true;
                            _iteratorError2 = err;
                        } finally{
                            try {
                                if (!_iteratorNormalCompletion2 && _iterator2.return != null) {
                                    _iterator2.return();
                                }
                            } finally{
                                if (_didIteratorError2) {
                                    throw _iteratorError2;
                                }
                            }
                        }
                        (_ranges2 = ranges).push.apply(_ranges2, _to_consumable_array(textRanges1));
                    }
                }
            }
            if (node.type === 'paragraph' && node.children.length === 1 && !EditorUtils.isDirtLeaf(node.children[0])) {
                if (cacheText && Path.equals(cacheText.path, path)) {
                    var _ranges3;
                    (_ranges3 = ranges).push.apply(_ranges3, _to_consumable_array(cacheText.range));
                } else {
                    var str = Node.string(node);
                    if (str.startsWith('```')) {
                        ranges.push({
                            anchor: {
                                path: _to_consumable_array(path).concat([
                                    0
                                ]),
                                offset: 0
                            },
                            focus: {
                                path: _to_consumable_array(path).concat([
                                    0
                                ]),
                                offset: 3
                            },
                            color: '#a3a3a3'
                        });
                        cacheTextNode.set(node, {
                            path: path,
                            range: ranges
                        });
                    } else if (/^\|([^|]+\|)+$/.test(str)) {
                        ranges.push({
                            anchor: {
                                path: _to_consumable_array(path).concat([
                                    0
                                ]),
                                offset: 0
                            },
                            focus: {
                                path: _to_consumable_array(path).concat([
                                    0
                                ]),
                                offset: str.length
                            },
                            color: '#a3a3a3'
                        });
                        cacheTextNode.set(node, {
                            path: path,
                            range: ranges
                        });
                    }
                }
            }
            return ranges;
        }
        return [];
    };
}
