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 ownKeys(object, enumerableOnly) {
    var keys = Object.keys(object);
    if (Object.getOwnPropertySymbols) {
        var symbols = Object.getOwnPropertySymbols(object);
        if (enumerableOnly) {
            symbols = symbols.filter(function(sym) {
                return Object.getOwnPropertyDescriptor(object, sym).enumerable;
            });
        }
        keys.push.apply(keys, symbols);
    }
    return keys;
}
function _object_spread_props(target, source) {
    source = source != null ? source : {};
    if (Object.getOwnPropertyDescriptors) {
        Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
    } else {
        ownKeys(Object(source)).forEach(function(key) {
            Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
        });
    }
    return target;
}
import { debugInfo } from "../../../../Utils/debugUtils";
import { EditorUtils } from "../../utils";
import { decodeURIComponentUrl, findAttachment } from "./parseHtml";
/**
 * 处理图片节点
 * @param currentElement - 当前处理的图片元素，包含url和alt属性
 * @returns 返回格式化的图片节点对象
 */ export var handleImage = function(currentElement) {
    debugInfo('handleImage - 处理图片', {
        url: currentElement === null || currentElement === void 0 ? void 0 : currentElement.url,
        alt: currentElement === null || currentElement === void 0 ? void 0 : currentElement.alt,
        finished: currentElement === null || currentElement === void 0 ? void 0 : currentElement.finished
    });
    var result = EditorUtils.createMediaNode(decodeURIComponentUrl(currentElement === null || currentElement === void 0 ? void 0 : currentElement.url), 'image', _object_spread({
        alt: currentElement.alt
    }, currentElement.finished !== undefined && {
        finished: currentElement.finished
    }));
    debugInfo('handleImage - 图片处理完成', {
        type: currentElement === null || currentElement === void 0 ? void 0 : currentElement.type,
        url: currentElement === null || currentElement === void 0 ? void 0 : currentElement.url
    });
    return result;
};
/**
 * 处理附件链接
 */ export var handleAttachmentLink = function(currentElement) {
    var _currentElement_children;
    debugInfo('handleAttachmentLink - 处理附件链接', {
        childrenCount: (_currentElement_children = currentElement.children) === null || _currentElement_children === void 0 ? void 0 : _currentElement_children.length
    });
    var text = currentElement.children.map(function(n) {
        return n.value || '';
    }).join('');
    var attach = findAttachment(text);
    if (!attach) {
        debugInfo('handleAttachmentLink - 未找到附件');
        return null;
    }
    var name = text.match(/>(.*)<\/a>/);
    var result = {
        type: 'attach',
        url: decodeURIComponentUrl(attach === null || attach === void 0 ? void 0 : attach.url),
        size: attach.size,
        children: [
            {
                type: 'card-before',
                children: [
                    {
                        text: ''
                    }
                ]
            },
            {
                type: 'card-after',
                children: [
                    {
                        text: ''
                    }
                ]
            }
        ],
        name: name ? name[1] : attach === null || attach === void 0 ? void 0 : attach.url
    };
    debugInfo('handleAttachmentLink - 附件链接处理完成', {
        type: result.type,
        url: result.url,
        size: result.size,
        name: result.name
    });
    return result;
};
/**
 * 处理链接卡片
 */ export var handleLinkCard = function(currentElement, config) {
    var _currentElement_children, _currentElement_children1;
    debugInfo('handleLinkCard - 处理链接卡片', {
        childrenCount: currentElement === null || currentElement === void 0 ? void 0 : (_currentElement_children = currentElement.children) === null || _currentElement_children === void 0 ? void 0 : _currentElement_children.length,
        configType: config === null || config === void 0 ? void 0 : config.type
    });
    var link = currentElement === null || currentElement === void 0 ? void 0 : (_currentElement_children1 = currentElement.children) === null || _currentElement_children1 === void 0 ? void 0 : _currentElement_children1.at(0);
    var result = _object_spread_props(_object_spread({}, config), {
        type: 'link-card',
        url: decodeURIComponentUrl(link === null || link === void 0 ? void 0 : link.url),
        children: [
            {
                type: 'card-before',
                children: [
                    {
                        text: ''
                    }
                ]
            },
            {
                type: 'card-after',
                children: [
                    {
                        text: ''
                    }
                ]
            }
        ],
        name: link.title
    });
    debugInfo('handleLinkCard - 链接卡片处理完成', {
        type: result.type,
        url: result.url,
        name: result.name
    });
    return result;
};
