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 classNames from "clsx";
import React, { useEffect, useRef, useState } from "react";
import { useRefFunction } from "../../../Hooks/useRefFunction";
import { compileTemplate } from "../../../I18n";
import { FileItem } from "./FileItem";
import { GroupHeader } from "./GroupHeader";
/** 分组首屏展示的文件数 */ export var GROUP_INITIAL_PAGE_SIZE = 50;
/** 「查看更多」每次展开的额外文件数 */ export var GROUP_PAGE_SIZE_INCREMENT = 100;
/**
 * 文件分组：标题栏 + 可折叠文件列表 + 渐进展示（首屏 50，每次再加 100）
 */ var FileGroupComponent = function FileGroupComponent(param) {
    var group = param.group, onToggle = param.onToggle, onGroupDownload = param.onGroupDownload, onDownload = param.onDownload, onFileClick = param.onFileClick, onPreview = param.onPreview, onShare = param.onShare, onLocate = param.onLocate, prefixCls = param.prefixCls, hashId = param.hashId, locale = param.locale, bindDomId = param.bindDomId;
    var _useState = _sliced_to_array(useState(GROUP_INITIAL_PAGE_SIZE), 2), visibleCount = _useState[0], setVisibleCount = _useState[1];
    // 分组数据变化时重置分页，避免切换数据源后一次性渲染过多节点
    useEffect(function() {
        setVisibleCount(GROUP_INITIAL_PAGE_SIZE);
    }, [
        group.id,
        group.children.length
    ]);
    // 内容挂载状态：展开时立即挂载、折叠时等过渡结束再卸载
    // 这样既保留 CSS 折叠动画的视觉过渡，又让折叠后内容真正不可达（query / a11y / Tab）
    var _useState1 = _sliced_to_array(useState(!group.collapsed), 2), contentMounted = _useState1[0], setContentMounted = _useState1[1];
    var collapseTimerRef = useRef(null);
    useEffect(function() {
        if (collapseTimerRef.current) {
            clearTimeout(collapseTimerRef.current);
            collapseTimerRef.current = null;
        }
        if (!group.collapsed) {
            // 展开：立即挂载
            setContentMounted(true);
            return;
        }
        // 折叠：等 CSS 过渡结束（与 style.ts 中 0.26s 对齐）后卸载
        collapseTimerRef.current = setTimeout(function() {
            setContentMounted(false);
            collapseTimerRef.current = null;
        }, 280);
    }, [
        group.collapsed
    ]);
    useEffect(function() {
        return function() {
            if (collapseTimerRef.current) {
                clearTimeout(collapseTimerRef.current);
            }
        };
    }, []);
    var totalCount = group.children.length;
    var visibleFiles = group.children.slice(0, visibleCount);
    var remainingCount = totalCount - visibleCount;
    var hasMore = remainingCount > 0;
    var handleShowMore = useRefFunction(function(e) {
        e.stopPropagation();
        setVisibleCount(function(prev) {
            return prev + GROUP_PAGE_SIZE_INCREMENT;
        });
    });
    var handleShowMoreKeyDown = useRefFunction(function(e) {
        if (e.key === 'Enter' || e.key === ' ') {
            e.preventDefault();
            e.stopPropagation();
            setVisibleCount(function(prev) {
                return prev + GROUP_PAGE_SIZE_INCREMENT;
            });
        }
    });
    var showMoreLabel = hasMore ? compileTemplate((locale === null || locale === void 0 ? void 0 : locale['workspace.file.showMore']) || '查看更多（还有 ${count} 个）', {
        count: String(remainingCount)
    }) : (locale === null || locale === void 0 ? void 0 : locale['workspace.file.showMoreFiles']) || '查看更多文件';
    return /*#__PURE__*/ React.createElement("div", {
        className: classNames("".concat(prefixCls, "-container--group"), hashId)
    }, /*#__PURE__*/ React.createElement(GroupHeader, {
        group: group,
        onToggle: onToggle,
        onGroupDownload: onGroupDownload,
        prefixCls: prefixCls,
        hashId: hashId,
        locale: locale
    }), /*#__PURE__*/ React.createElement("div", {
        className: classNames("".concat(prefixCls, "-group-content-wrapper"), hashId),
        "data-collapsed": group.collapsed ? 'true' : 'false',
        "aria-hidden": group.collapsed ? true : undefined
    }, /*#__PURE__*/ React.createElement("div", {
        className: classNames("".concat(prefixCls, "-group-content"), hashId)
    }, contentMounted && visibleFiles.map(function(file) {
        return /*#__PURE__*/ React.createElement(FileItem, {
            key: file.id,
            file: file,
            onClick: onFileClick,
            onDownload: onDownload,
            onPreview: onPreview,
            onShare: onShare,
            onLocate: onLocate,
            prefixCls: prefixCls,
            hashId: hashId,
            locale: locale,
            bindDomId: !!bindDomId
        });
    }), contentMounted && hasMore && /*#__PURE__*/ React.createElement("div", {
        role: "button",
        tabIndex: group.collapsed ? -1 : 0,
        className: classNames("".concat(prefixCls, "-show-more"), hashId),
        onClick: handleShowMore,
        onKeyDown: handleShowMoreKeyDown,
        "aria-label": showMoreLabel
    }, showMoreLabel))));
};
FileGroupComponent.displayName = 'FileGroup';
export var FileGroup = /*#__PURE__*/ React.memo(FileGroupComponent);
