import { ChevronDown as DownIcon, Download as DownloadIcon, ChevronRight as RightIcon } from "@sofa-design/icons";
import classNames from "clsx";
import React from "react";
import { ActionIconBox } from "../../../Components/ActionIconBox";
import { fileTypeProcessor } from "../FileTypeProcessor";
import { getGroupIcon } from "../utils";
import { AccessibleButton } from "./AccessibleButton";
/**
 * 分组标题栏：可折叠 + 文件数 + 可选下载操作
 *
 * 下载按钮显示规则：
 * 1. 用户显式设置了 `group.canDownload`，遵循设置
 * 2. 未传入 `onGroupDownload`，不显示
 * 3. 否则只要组内存在任意一个可下载的子文件就显示
 */ var GroupHeaderComponent = function GroupHeaderComponent(param) {
    var group = param.group, onToggle = param.onToggle, onGroupDownload = param.onGroupDownload, prefixCls = param.prefixCls, hashId = param.hashId, locale = param.locale;
    var groupTypeInfo = fileTypeProcessor.inferFileType(group);
    var groupType = group.type || groupTypeInfo.fileType;
    var handleToggle = function handleToggle() {
        onToggle === null || onToggle === void 0 ? void 0 : onToggle(group.id, groupType, !group.collapsed);
    };
    var handleDownload = function handleDownload(e) {
        e.stopPropagation();
        onGroupDownload === null || onGroupDownload === void 0 ? void 0 : onGroupDownload(group.children, groupType);
    };
    var CollapseIcon = group.collapsed ? RightIcon : DownIcon;
    var groupIcon = getGroupIcon(group, groupType, group.icon);
    var showDownloadButton = function() {
        if (group.canDownload !== undefined) return group.canDownload;
        if (!onGroupDownload) return false;
        return group.children.some(function(child) {
            return child.canDownload === true || child.canDownload !== false && Boolean(child.url || child.content || child.file);
        });
    }();
    var expandLabel = group.collapsed ? (locale === null || locale === void 0 ? void 0 : locale['workspace.expand']) || '展开' : (locale === null || locale === void 0 ? void 0 : locale['workspace.collapse']) || '收起';
    var groupLabel = (locale === null || locale === void 0 ? void 0 : locale['workspace.group']) || '分组';
    return /*#__PURE__*/ React.createElement(AccessibleButton, {
        icon: /*#__PURE__*/ React.createElement(React.Fragment, null, /*#__PURE__*/ React.createElement("div", {
            className: classNames("".concat(prefixCls, "-group-header-left"), hashId)
        }, /*#__PURE__*/ React.createElement(CollapseIcon, {
            className: classNames("".concat(prefixCls, "-group-toggle-icon"), hashId)
        }), /*#__PURE__*/ React.createElement("div", {
            className: classNames("".concat(prefixCls, "-group-type-icon"), hashId)
        }, groupIcon), /*#__PURE__*/ React.createElement("span", {
            className: classNames("".concat(prefixCls, "-group-type-name"), hashId)
        }, group.name)), /*#__PURE__*/ React.createElement("div", {
            className: classNames("".concat(prefixCls, "-group-header-right"), hashId)
        }, /*#__PURE__*/ React.createElement("span", {
            className: classNames("".concat(prefixCls, "-group-count"), hashId)
        }, group.children.length), showDownloadButton && /*#__PURE__*/ React.createElement(ActionIconBox, {
            title: (locale === null || locale === void 0 ? void 0 : locale['workspace.file.download']) || '下载',
            onClick: handleDownload,
            tooltipProps: {
                mouseEnterDelay: 0.3
            },
            className: classNames("".concat(prefixCls, "-group-action-btn"), hashId)
        }, /*#__PURE__*/ React.createElement(DownloadIcon, null)))),
        onClick: handleToggle,
        className: classNames("".concat(prefixCls, "-group-header"), hashId),
        ariaLabel: "".concat(expandLabel).concat(group.name).concat(groupLabel)
    });
};
GroupHeaderComponent.displayName = 'GroupHeader';
export var GroupHeader = /*#__PURE__*/ React.memo(GroupHeaderComponent);
