/**
 * remark 插件：支持 ::: type [title] 容器语法（兼容 markdown-it-container）
 *
 * 语法示例：
 * :::info
 * 这是信息提示块。
 * :::
 *
 * ::: warning 警告标题
 * 这是带标题的警告块。
 * :::
 */ 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 _define_property(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        });
    } else {
        obj[key] = value;
    }
    return obj;
}
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 _object_spread(target) {
    for(var i = 1; i < arguments.length; i++){
        var source = arguments[i] != null ? arguments[i] : {};
        var ownKeys = Object.keys(source);
        if (typeof Object.getOwnPropertySymbols === "function") {
            ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
                return Object.getOwnPropertyDescriptor(source, sym).enumerable;
            }));
        }
        ownKeys.forEach(function(key) {
            _define_property(target, key, source[key]);
        });
    }
    return target;
}
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 _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);
}
var REGEX_BEGIN = /^\s*:::\s*(\w+)\s*(.*)?/;
var REGEX_END = /^\s*:::$/;
var isLiteralNode = function isLiteralNode(node) {
    return node && 'value' in node && typeof node.value === 'string';
};
var isParagraph = function isParagraph(node) {
    return (node === null || node === void 0 ? void 0 : node.type) === 'paragraph';
};
export var remarkContainer = function remarkContainer() {
    var options = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
    var _options_className, _options_containerTag, _options_titleElement;
    var className = (_options_className = options.className) !== null && _options_className !== void 0 ? _options_className : 'markdown-container';
    var containerTag = (_options_containerTag = options.containerTag) !== null && _options_containerTag !== void 0 ? _options_containerTag : 'div';
    var titleElement = (_options_titleElement = options.titleElement) !== null && _options_titleElement !== void 0 ? _options_titleElement : {
        className: [
            'markdown-container__title'
        ]
    };
    var constructContainer = function constructContainer(children, type) {
        return {
            type: 'container',
            children: children,
            data: {
                hName: containerTag,
                hProperties: {
                    className: [
                        className,
                        type.toLowerCase()
                    ]
                }
            }
        };
    };
    var constructTitle = function constructTitle(title) {
        return {
            type: 'paragraph',
            children: [
                {
                    type: 'text',
                    value: title
                }
            ],
            data: {
                hName: 'div',
                hProperties: _object_spread({
                    className: [
                        "".concat(className, "__title")
                    ]
                }, titleElement && _object_spread({}, titleElement))
            }
        };
    };
    var transformChildren = function transformChildren(parent) {
        if (!(parent === null || parent === void 0 ? void 0 : parent.children)) return;
        var children = [];
        var len = parent.children.length;
        var currentIndex = -1;
        while(currentIndex < len - 1){
            var _currentNode_children;
            currentIndex += 1;
            var currentNode = parent.children[currentIndex];
            children.push(currentNode);
            if (!isParagraph(currentNode)) continue;
            var firstChild = (_currentNode_children = currentNode.children) === null || _currentNode_children === void 0 ? void 0 : _currentNode_children[0];
            if (!isLiteralNode(firstChild)) continue;
            var match = firstChild.value.match(REGEX_BEGIN);
            if (!match) continue;
            children.pop();
            var beginIndex = currentIndex;
            var innerIndex = currentIndex - 1;
            while(innerIndex < len - 1){
                var _ref;
                var _innerNode_children;
                innerIndex += 1;
                var innerNode = parent.children[innerIndex];
                if (!isParagraph(innerNode)) continue;
                var innerFirst = (_innerNode_children = innerNode.children) === null || _innerNode_children === void 0 ? void 0 : _innerNode_children[0];
                if (!isLiteralNode(innerFirst) || !REGEX_END.test(innerFirst.value)) continue;
                var _match = _sliced_to_array(match, 3), type = _match[1], title = _match[2];
                var containerChildren = parent.children.slice(beginIndex + 1, innerIndex);
                if ((title === null || title === void 0 ? void 0 : title.trim()) && titleElement !== null) {
                    containerChildren.unshift(constructTitle(title.trim()));
                }
                var container = constructContainer(containerChildren, (_ref = type === null || type === void 0 ? void 0 : type.toLowerCase()) !== null && _ref !== void 0 ? _ref : 'note');
                children.push(container);
                currentIndex = innerIndex;
                break;
            }
        }
        parent.children = children;
    };
    return function(tree) {
        if ((tree === null || tree === void 0 ? void 0 : tree.type) === 'root') {
            transformChildren(tree);
        }
    };
};
export default remarkContainer;
