/**
 * remark 插件：将 remark-directive 的 containerDirective 转为与 remarkContainer 相同的 HTML 结构
 * （div.markdown-container + type 类名，可选 title 通过 attributes.title）
 *
 * 依赖 remark-directive 解析，语法示例：
 * :::info
 * 这是信息提示块。
 * :::
 *
 * :::warning{title="警告标题"}
 * 这是带标题的警告块。
 * :::
 */ 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 _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 _type_of(obj) {
    "@swc/helpers - typeof";
    return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
}
import { visit } from "unist-util-visit";
var DEFAULT_CLASS_NAME = 'markdown-container';
var DEFAULT_CONTAINER_TAG = 'div';
var TITLE_CLASS_NAME = 'markdown-container__title';
function createTitleParagraph(title, titleProps) {
    return {
        type: 'paragraph',
        children: [
            {
                type: 'text',
                value: title
            }
        ],
        data: {
            hName: 'div',
            hProperties: _object_spread({
                className: [
                    TITLE_CLASS_NAME
                ]
            }, titleProps)
        }
    };
}
export var remarkDirectiveContainer = function remarkDirectiveContainer() {
    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 : DEFAULT_CLASS_NAME;
    var containerTag = (_options_containerTag = options.containerTag) !== null && _options_containerTag !== void 0 ? _options_containerTag : DEFAULT_CONTAINER_TAG;
    var titleElement = (_options_titleElement = options.titleElement) !== null && _options_titleElement !== void 0 ? _options_titleElement : {
        className: [
            TITLE_CLASS_NAME
        ]
    };
    var titleProps = titleElement && (typeof titleElement === "undefined" ? "undefined" : _type_of(titleElement)) === 'object' ? titleElement : {
        className: [
            TITLE_CLASS_NAME
        ]
    };
    return function(tree) {
        visit(tree, 'containerDirective', function(node) {
            var _ref, _node_attributes, _node_data;
            var _node_name;
            var name = (_ref = (_node_name = node.name) === null || _node_name === void 0 ? void 0 : _node_name.toLowerCase()) !== null && _ref !== void 0 ? _ref : 'note';
            var attrs = (_node_attributes = node.attributes) !== null && _node_attributes !== void 0 ? _node_attributes : {};
            var title = typeof attrs.title === 'string' ? attrs.title : attrs.title ? String(attrs.title) : undefined;
            var data = (_node_data = node.data) !== null && _node_data !== void 0 ? _node_data : node.data = {};
            data.hName = containerTag;
            data.hProperties = {
                className: [
                    className,
                    name
                ]
            };
            if (title === null || title === void 0 ? void 0 : title.trim()) {
                var titleNode = createTitleParagraph(title.trim(), titleProps);
                if (Array.isArray(node.children)) {
                    node.children.unshift(titleNode);
                } else {
                    node.children = [
                        titleNode
                    ];
                }
            }
        });
    };
};
export default remarkDirectiveContainer;
