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 _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 _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);
}
import { ConfigProvider, Skeleton } from "antd";
import classNames from "clsx";
import React, { useContext, useEffect, useState } from "react";
import { AvatarList } from "../../components/ContributorAvatar";
export function LinkCard(param) {
    var element = param.element, attributes = param.attributes, children = param.children;
    var _element_otherProps, _element_otherProps_collaborators, _element_otherProps1, _element_otherProps2;
    var context = useContext(ConfigProvider.ConfigContext);
    var contentCls = context === null || context === void 0 ? void 0 : context.getPrefixCls('agentic-md-editor-content');
    var blockCls = contentCls ? "".concat(contentCls, "-link-card") : '';
    var htmlRef = React.useRef(null);
    var _useState = _sliced_to_array(useState(false), 2), showAsText = _useState[0], setShowAsText = _useState[1];
    // 如果 finished 为 false，设置 5 秒超时，超时后显示为文本
    useEffect(function() {
        if (element.finished === false) {
            setShowAsText(false);
            var timer = setTimeout(function() {
                setShowAsText(true);
            }, 5000);
            return function() {
                clearTimeout(timer);
            };
        } else {
            setShowAsText(false);
        }
    }, [
        element.finished
    ]);
    // 如果是不完整状态
    if (element.finished === false) {
        // 如果 5 秒后仍未完成，显示为文本
        if (showAsText) {
            return /*#__PURE__*/ React.createElement("div", attributes, /*#__PURE__*/ React.createElement("div", {
                style: {
                    padding: '8px 12px',
                    border: '1px solid #d9d9d9',
                    borderRadius: '4px',
                    color: 'rgba(0, 0, 0, 0.65)',
                    wordBreak: 'break-all'
                }
            }, element.url || element.title || element.name || '链接卡片'), children);
        }
        // 5 秒内显示加载骨架屏
        return /*#__PURE__*/ React.createElement("div", attributes, /*#__PURE__*/ React.createElement(Skeleton, {
            active: true,
            paragraph: {
                rows: 2
            }
        }), children);
    }
    return /*#__PURE__*/ React.createElement("div", attributes, /*#__PURE__*/ React.createElement("div", {
        className: classNames(blockCls),
        "data-be": "link-card",
        "data-drag-el": true,
        draggable: false,
        onContextMenu: function onContextMenu(e) {
            e.stopPropagation();
        },
        onMouseDown: function onMouseDown(e) {
            e.stopPropagation();
        },
        style: {
            display: 'flex'
        }
    }, /*#__PURE__*/ React.createElement("div", {
        style: {
            display: 'flex',
            height: '100%',
            minWidth: '0',
            fontSize: 60,
            minHeight: '100px',
            lineHeight: '100px'
        }
    }, children.at(0)), /*#__PURE__*/ React.createElement("div", {
        ref: htmlRef,
        style: {
            flex: 1
        },
        onClick: function onClick() {
            if (typeof window === 'undefined') return;
            window.open(element === null || element === void 0 ? void 0 : element.url);
        },
        className: classNames("".concat(blockCls, "__container"))
    }, /*#__PURE__*/ React.createElement("div", {
        className: classNames("".concat(blockCls, "__content")),
        contentEditable: false
    }, element.icon ? /*#__PURE__*/ React.createElement("img", {
        className: classNames("".concat(blockCls, "__icon")),
        src: element.icon,
        width: 56
    }) : null, /*#__PURE__*/ React.createElement("div", {
        style: {
            flex: 1,
            minWidth: 0
        }
    }, /*#__PURE__*/ React.createElement("a", {
        href: element === null || element === void 0 ? void 0 : element.url,
        className: classNames("".concat(blockCls, "__title")),
        onClick: function onClick(e) {
            e.stopPropagation();
            e.preventDefault();
            if (typeof window === 'undefined') return;
            window.open(element === null || element === void 0 ? void 0 : element.url);
        },
        download: element.title || element.name || 'no title'
    }, element.title || element.name || 'no title'), /*#__PURE__*/ React.createElement("div", {
        className: classNames("".concat(blockCls, "__description"))
    }, element.description ? element.description : element === null || element === void 0 ? void 0 : element.url), /*#__PURE__*/ React.createElement("div", {
        className: classNames("".concat(blockCls, "__collaborators"))
    }, ((_element_otherProps = element.otherProps) === null || _element_otherProps === void 0 ? void 0 : _element_otherProps.collaborators) ? /*#__PURE__*/ React.createElement("div", null, /*#__PURE__*/ React.createElement(AvatarList, {
        displayList: ((_element_otherProps1 = element.otherProps) === null || _element_otherProps1 === void 0 ? void 0 : (_element_otherProps_collaborators = _element_otherProps1.collaborators) === null || _element_otherProps_collaborators === void 0 ? void 0 : _element_otherProps_collaborators.map(function(item) {
            var _Object_keys, _Object_values;
            return {
                name: (_Object_keys = Object.keys(item)) === null || _Object_keys === void 0 ? void 0 : _Object_keys.at(0),
                collaboratorNumber: ((_Object_values = Object.values(item)) === null || _Object_values === void 0 ? void 0 : _Object_values.at(0)) || 0
            };
        }).slice(0, 5)) || []
    })) : /*#__PURE__*/ React.createElement("div", null), ((_element_otherProps2 = element.otherProps) === null || _element_otherProps2 === void 0 ? void 0 : _element_otherProps2.updateTime) ? /*#__PURE__*/ React.createElement("div", {
        className: classNames("".concat(blockCls, "__update-time")),
        style: {
            color: 'rgba(0,0,0,0.45)',
            fontSize: 12
        }
    }, element.otherProps.updateTime) : null)))), /*#__PURE__*/ React.createElement("div", {
        style: {
            display: 'flex',
            height: '100%',
            minWidth: '4px',
            minHeight: '100px',
            fontSize: 60,
            lineHeight: '100px'
        }
    }, children.at(-1))));
}
