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 _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 _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 { FileOther as DefaultFileIcon, FileAudio, FileCsv, FileDoc, FileFolders, FileMarkdown, FileOther, FilePdf, FilePic, FilePpt, FileVideo, FileXlsx, FileXml, FileZip } from "@sofa-design/icons";
import React from "react";
import { FILE_TYPES, FileCategory, getFileCategory } from "../types";
// 文件扩展名到图标的映射
var EXTENSION_ICON_MAP = {
    txt: /*#__PURE__*/ React.createElement(DefaultFileIcon, null),
    xlsx: /*#__PURE__*/ React.createElement(FileXlsx, null),
    xls: /*#__PURE__*/ React.createElement(FileXlsx, null),
    doc: /*#__PURE__*/ React.createElement(FileDoc, null),
    docx: /*#__PURE__*/ React.createElement(FileDoc, null),
    pdf: /*#__PURE__*/ React.createElement(FilePdf, null),
    csv: /*#__PURE__*/ React.createElement(FileCsv, null),
    xml: /*#__PURE__*/ React.createElement(FileXml, null),
    html: /*#__PURE__*/ React.createElement(FileXml, null),
    md: /*#__PURE__*/ React.createElement(FileMarkdown, null),
    markdown: /*#__PURE__*/ React.createElement(FileMarkdown, null),
    ppt: /*#__PURE__*/ React.createElement(FilePpt, null),
    pptx: /*#__PURE__*/ React.createElement(FilePpt, null),
    zip: /*#__PURE__*/ React.createElement(FileZip, null),
    rar: /*#__PURE__*/ React.createElement(FileZip, null),
    '7z': /*#__PURE__*/ React.createElement(FileZip, null),
    tar: /*#__PURE__*/ React.createElement(FileZip, null),
    gz: /*#__PURE__*/ React.createElement(FileZip, null),
    bz2: /*#__PURE__*/ React.createElement(FileZip, null)
};
var _obj;
// 文件类型到默认图标的映射
var TYPE_ICON_MAP = (_obj = {}, _define_property(_obj, FileCategory.Text, /*#__PURE__*/ React.createElement(FileMarkdown, null)), _define_property(_obj, FileCategory.Code, /*#__PURE__*/ React.createElement(FileXml, null)), _define_property(_obj, FileCategory.PDF, /*#__PURE__*/ React.createElement(FilePdf, null)), _define_property(_obj, FileCategory.Word, /*#__PURE__*/ React.createElement(FileDoc, null)), _define_property(_obj, FileCategory.Excel, /*#__PURE__*/ React.createElement(FileXlsx, null)), _define_property(_obj, FileCategory.Presentation, /*#__PURE__*/ React.createElement(FilePpt, null)), _define_property(_obj, FileCategory.Image, /*#__PURE__*/ React.createElement(FilePic, null)), _define_property(_obj, FileCategory.Video, /*#__PURE__*/ React.createElement(FileVideo, null)), _define_property(_obj, FileCategory.Audio, /*#__PURE__*/ React.createElement(FileAudio, null)), _define_property(_obj, FileCategory.Archive, /*#__PURE__*/ React.createElement(FileZip, null)), _define_property(_obj, FileCategory.Other, /*#__PURE__*/ React.createElement(FileOther, null)), _obj);
/**
 * 获取文件类型图标
 */ export var getFileTypeIcon = function getFileTypeIcon(type, customIcon, fileName) {
    if (customIcon) {
        return customIcon;
    }
    if (fileName) {
        var _fileName_split_pop;
        var extension = (_fileName_split_pop = fileName.split('.').pop()) === null || _fileName_split_pop === void 0 ? void 0 : _fileName_split_pop.toLowerCase();
        if (extension && extension in EXTENSION_ICON_MAP) {
            return EXTENSION_ICON_MAP[extension];
        }
    }
    var fileDefinition = FILE_TYPES[type];
    if (fileDefinition && fileDefinition.extensions[0] in EXTENSION_ICON_MAP) {
        return EXTENSION_ICON_MAP[fileDefinition.extensions[0]];
    }
    return TYPE_ICON_MAP[getFileCategory(type)] || /*#__PURE__*/ React.createElement(DefaultFileIcon, null);
};
/**
 * 获取分组图标 - 如果分组中有不同类型的文件，显示文件夹图标
 */ export var getGroupIcon = function getGroupIcon(group, groupType, customIcon) {
    if (customIcon) {
        return customIcon;
    }
    // 检查分组中是否有不同类型的文件
    if (group.children.length > 0) {
        var fileTypes = new Set(group.children.map(function(file) {
            var _file_name_split_pop;
            // 如果文件有明确的 type，使用 type
            if (file.type) {
                return file.type;
            }
            // 否则根据文件名推断类型
            var extension = (_file_name_split_pop = file.name.split('.').pop()) === null || _file_name_split_pop === void 0 ? void 0 : _file_name_split_pop.toLowerCase();
            if (extension) {
                var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
                try {
                    // 查找对应的文件类型
                    for(var _iterator = Object.entries(FILE_TYPES)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
                        var _step_value = _sliced_to_array(_step.value, 2), type = _step_value[0], definition = _step_value[1];
                        if (definition.extensions.includes(extension)) {
                            return type;
                        }
                    }
                } catch (err) {
                    _didIteratorError = true;
                    _iteratorError = err;
                } finally{
                    try {
                        if (!_iteratorNormalCompletion && _iterator.return != null) {
                            _iterator.return();
                        }
                    } finally{
                        if (_didIteratorError) {
                            throw _iteratorError;
                        }
                    }
                }
            }
            return 'other'; // 默认类型
        }));
        // 如果有多种不同的文件类型，显示文件夹图标
        if (fileTypes.size > 1) {
            return /*#__PURE__*/ React.createElement(FileFolders, null);
        }
        // 如果只有一种文件类型，使用该类型的图标
        if (fileTypes.size === 1) {
            var _group_children_;
            var singleType = Array.from(fileTypes)[0];
            var representativeFileName = (_group_children_ = group.children[0]) === null || _group_children_ === void 0 ? void 0 : _group_children_.name;
            return getFileTypeIcon(singleType, undefined, representativeFileName);
        }
    }
    // 如果没有子文件，使用默认的分组类型图标
    return getFileTypeIcon(groupType);
};
// 生成唯一ID的工具函数
export var generateUniqueId = function generateUniqueId(node) {
    if (node.id) return node.id;
    // 使用文件名、类型和时间戳组合生成唯一ID
    var timestamp = Date.now();
    var randomStr = Math.random().toString(36).substring(2, 8);
    return "".concat(node.type, "_").concat(node.name, "_").concat(timestamp, "_").concat(randomStr);
};
