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;
}
import { ConfigProvider } from "antd";
import classNames from "clsx";
import React, { memo, useContext, useMemo } from "react";
import { I18nContext } from "../../../I18n";
import { useStyle } from "./style";
import { formatDisplayUrl, isExternalLink, isSafeHref, resolveDocCardsFields, splitTags } from "./utils";
/** 卡片列数硬上限：超过 4 列后单卡过窄、信息过密 */ var MAX_CARD_COLUMNS = 4;
/** title attribute 上完整 URL 不截断；卡片正文展示截断到该长度 */ var URL_DISPLAY_MAX_LENGTH = 64;
var toDisplayText = function toDisplayText(raw) {
    if (raw === undefined || raw === null) return '';
    return String(raw);
};
var DocCardsComponent = function DocCardsComponent(param) {
    var title = param.title, columns = param.columns, data = param.data, toolbar = param.toolbar, fieldMap = param.fieldMap, _param_cardColumns = param.cardColumns, cardColumns = _param_cardColumns === void 0 ? 2 : _param_cardColumns, className = param.className, style = param.style;
    var i18n = useContext(I18nContext);
    var getPrefixCls = useContext(ConfigProvider.ConfigContext).getPrefixCls;
    var prefixCls = getPrefixCls('agentic-doc-cards');
    var _useStyle = useStyle(prefixCls), wrapSSR = _useStyle.wrapSSR, hashId = _useStyle.hashId;
    var columnKeys = useMemo(function() {
        return columns.map(function(col) {
            return col.dataIndex;
        }).filter(Boolean);
    }, [
        columns
    ]);
    var fields = useMemo(function() {
        return resolveDocCardsFields(columnKeys, fieldMap);
    }, [
        columnKeys,
        fieldMap
    ]);
    var safeColumnCount = Math.max(1, Math.min(MAX_CARD_COLUMNS, Math.floor(cardColumns) || 1));
    // 用 repeat(N, ...) 精确控制最多 N 列；窄屏由 style.ts 中的媒体查询强制 1fr
    // minmax(0, 1fr) 防止子元素 min-content 撑破 grid track
    var gridStyle = useMemo(function() {
        return {
            gridTemplateColumns: "repeat(".concat(safeColumnCount, ", minmax(0, 1fr))")
        };
    }, [
        safeColumnCount
    ]);
    var headerNode = useMemo(function() {
        if (!title && !toolbar) return null;
        return /*#__PURE__*/ React.createElement("div", {
            className: classNames("".concat(prefixCls, "-header"), hashId)
        }, title ? /*#__PURE__*/ React.createElement("div", {
            className: classNames("".concat(prefixCls, "-title"), hashId)
        }, title) : null, toolbar ? /*#__PURE__*/ React.createElement("div", {
            className: classNames("".concat(prefixCls, "-toolbar"), hashId)
        }, toolbar) : null);
    }, [
        title,
        toolbar,
        prefixCls,
        hashId
    ]);
    if (!fields) {
        var _i18n_locale;
        return wrapSSR(/*#__PURE__*/ React.createElement("div", {
            className: classNames(prefixCls, hashId, className),
            style: style,
            contentEditable: false
        }, headerNode, /*#__PURE__*/ React.createElement("div", {
            className: classNames("".concat(prefixCls, "-empty"), hashId)
        }, (i18n === null || i18n === void 0 ? void 0 : (_i18n_locale = i18n.locale) === null || _i18n_locale === void 0 ? void 0 : _i18n_locale.docCards) || '卡片列表')));
    }
    return wrapSSR(/*#__PURE__*/ React.createElement("div", {
        className: classNames(prefixCls, hashId, className),
        style: style,
        contentEditable: false
    }, headerNode, /*#__PURE__*/ React.createElement("div", {
        className: classNames("".concat(prefixCls, "-grid"), hashId),
        style: gridStyle,
        role: "list"
    }, data.map(function(row, rowIndex) {
        var _i18n_locale;
        var titleText = toDisplayText(row[fields.title]).trim();
        var rawUrl = fields.url ? toDisplayText(row[fields.url]).trim() : '';
        var descText = fields.description ? toDisplayText(row[fields.description]).trim() : '';
        var tags = fields.tags ? splitTags(row[fields.tags]) : [];
        // 多行同名时 rowIndex 保唯一性；fields.title 已 trim 处理过
        var cardKey = "".concat(rowIndex, "-").concat(titleText);
        var safeLink = rawUrl && isSafeHref(rawUrl);
        var urlDisplay = rawUrl ? formatDisplayUrl(rawUrl, URL_DISPLAY_MAX_LENGTH) : '';
        return /*#__PURE__*/ React.createElement("article", {
            key: cardKey,
            className: classNames("".concat(prefixCls, "-item"), hashId),
            role: "listitem"
        }, titleText ? /*#__PURE__*/ React.createElement("h3", {
            className: classNames("".concat(prefixCls, "-item-title"), hashId)
        }, titleText) : null, rawUrl ? /*#__PURE__*/ React.createElement("div", {
            className: classNames("".concat(prefixCls, "-item-url"), hashId),
            title: rawUrl
        }, safeLink ? // 仅外部链接（http(s)/mailto/tel）开新 tab；站内绝对路径、相对路径、
        // 锚点 (#section) 走原 tab，避免破坏浏览器原生锚点滚动等行为
        /*#__PURE__*/ React.createElement("a", _object_spread({
            className: classNames("".concat(prefixCls, "-item-link"), hashId),
            href: rawUrl
        }, isExternalLink(rawUrl) ? {
            target: '_blank',
            rel: 'noopener noreferrer'
        } : {}), urlDisplay) : urlDisplay) : null, descText ? /*#__PURE__*/ React.createElement("p", {
            className: classNames("".concat(prefixCls, "-item-desc"), hashId)
        }, descText) : null, tags.length > 0 ? /*#__PURE__*/ React.createElement("div", {
            className: classNames("".concat(prefixCls, "-item-tags"), hashId),
            role: "list",
            "aria-label": (i18n === null || i18n === void 0 ? void 0 : (_i18n_locale = i18n.locale) === null || _i18n_locale === void 0 ? void 0 : _i18n_locale.docCardsTags) || '标签列表'
        }, tags.map(function(tag) {
            return /*#__PURE__*/ React.createElement("span", {
                key: tag,
                className: classNames("".concat(prefixCls, "-tag"), hashId),
                role: "listitem",
                title: tag
            }, tag);
        })) : null);
    }))));
};
DocCardsComponent.displayName = 'DocCards';
export var DocCards = /*#__PURE__*/ memo(DocCardsComponent);
