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 _class_call_check(instance, Constructor) {
    if (!(instance instanceof Constructor)) {
        throw new TypeError("Cannot call a class as a function");
    }
}
function _defineProperties(target, props) {
    for(var i = 0; i < props.length; i++){
        var descriptor = props[i];
        descriptor.enumerable = descriptor.enumerable || false;
        descriptor.configurable = true;
        if ("value" in descriptor) descriptor.writable = true;
        Object.defineProperty(target, descriptor.key, descriptor);
    }
}
function _create_class(Constructor, protoProps, staticProps) {
    if (protoProps) _defineProperties(Constructor.prototype, protoProps);
    if (staticProps) _defineProperties(Constructor, staticProps);
    return Constructor;
}
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 { FILE_TYPES, FileCategory, getFileCategory as getTypeCategoryFromType } from "../types";
import { PreviewCapability, dataSourceManager } from "./DataSourceStrategy";
/**
 * 文件类型处理器
 */ export var FileTypeProcessor = /*#__PURE__*/ function() {
    "use strict";
    function FileTypeProcessor(dataSourceManager) {
        _class_call_check(this, FileTypeProcessor);
        _define_property(this, "dataSourceManager", void 0);
        this.dataSourceManager = dataSourceManager;
    }
    _create_class(FileTypeProcessor, [
        {
            /**
   * 推断文件类型
   */ key: "inferFileType",
            value: function inferFileType(file) {
                var _file_file;
                // 1. 优先使用明确指定的类型
                if (file.type && file.type in FILE_TYPES) {
                    return {
                        displayType: file.displayType,
                        fileType: file.type,
                        category: getTypeCategoryFromType(file.type)
                    };
                }
                // 2. 如果有File对象，使用MIME类型推断
                if ((_file_file = file.file) === null || _file_file === void 0 ? void 0 : _file_file.type) {
                    var typeFromMime = this.getTypeFromMimeType(file.file.type);
                    if (typeFromMime) {
                        return {
                            displayType: file.displayType,
                            fileType: typeFromMime,
                            category: getTypeCategoryFromType(typeFromMime)
                        };
                    }
                }
                // 3. 根据文件名扩展名推断
                if (file.name) {
                    var typeFromExtension = this.getTypeFromFileName(file.name);
                    if (typeFromExtension) {
                        return {
                            displayType: file.displayType,
                            fileType: typeFromExtension,
                            category: getTypeCategoryFromType(typeFromExtension)
                        };
                    }
                }
                // 4. 根据URL扩展名推断
                if (file.url) {
                    var typeFromUrlExtension = this.getTypeFromUrl(file.url);
                    if (typeFromUrlExtension) {
                        return {
                            displayType: file.displayType,
                            fileType: typeFromUrlExtension,
                            category: getTypeCategoryFromType(typeFromUrlExtension)
                        };
                    }
                }
                // 5. 默认类型
                return {
                    displayType: file.displayType,
                    fileType: 'plainText',
                    category: FileCategory.Text
                };
            }
        },
        {
            /**
   * 处理文件的完整流程
   */ key: "processFile",
            value: function processFile(file) {
                var typeInference = this.inferFileType(file);
                var dataSource = this.dataSourceManager.processFile(file);
                var canPreview = this.determinePreviewCapability(typeInference, dataSource);
                var previewMode = this.determinePreviewMode(typeInference, dataSource);
                return {
                    typeInference: typeInference,
                    dataSource: dataSource,
                    canPreview: canPreview,
                    previewMode: previewMode
                };
            }
        },
        {
            /**
   * 清理处理结果
   */ key: "cleanupResult",
            value: function cleanupResult(result) {
                this.dataSourceManager.cleanupResult(result.dataSource);
            }
        },
        {
            key: "getTypeFromMimeType",
            value: /**
   * 从MIME类型推断文件类型
   */ function getTypeFromMimeType(mimeType) {
                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.mimeTypes.includes(mimeType)) {
                            return type;
                        }
                    }
                } catch (err) {
                    _didIteratorError = true;
                    _iteratorError = err;
                } finally{
                    try {
                        if (!_iteratorNormalCompletion && _iterator.return != null) {
                            _iterator.return();
                        }
                    } finally{
                        if (_didIteratorError) {
                            throw _iteratorError;
                        }
                    }
                }
                return null;
            }
        },
        {
            key: "getTypeFromFileName",
            value: /**
   * 从文件名推断文件类型
   */ function getTypeFromFileName(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) return null;
                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 null;
            }
        },
        {
            key: "getTypeFromUrl",
            value: /**
   * 从URL推断文件类型
   */ function getTypeFromUrl(url) {
                var _url_split_pop_split_, _url_split_pop;
                var urlExtension = (_url_split_pop = url.split('.').pop()) === null || _url_split_pop === void 0 ? void 0 : (_url_split_pop_split_ = _url_split_pop.split('?')[0]) === null || _url_split_pop_split_ === void 0 ? void 0 : _url_split_pop_split_.toLowerCase();
                if (!urlExtension) return null;
                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(urlExtension)) {
                            return type;
                        }
                    }
                } catch (err) {
                    _didIteratorError = true;
                    _iteratorError = err;
                } finally{
                    try {
                        if (!_iteratorNormalCompletion && _iterator.return != null) {
                            _iterator.return();
                        }
                    } finally{
                        if (_didIteratorError) {
                            throw _iteratorError;
                        }
                    }
                }
                return null;
            }
        },
        {
            key: "determinePreviewCapability",
            value: /**
   * 确定预览能力
   */ function determinePreviewCapability(typeInference, dataSource) {
                // 如果数据源不支持预览，则无法预览
                if (dataSource.previewCapability === PreviewCapability.NONE) {
                    return false;
                }
                // 对于基础预览能力，只支持特定类型
                if (dataSource.previewCapability === PreviewCapability.BASIC) {
                    return typeInference.category === FileCategory.Image;
                }
                // 完全预览能力支持更多类型
                return [
                    FileCategory.Text,
                    FileCategory.Code,
                    FileCategory.Image,
                    FileCategory.Video,
                    FileCategory.Audio,
                    FileCategory.PDF
                ].includes(typeInference.category);
            }
        },
        {
            key: "determinePreviewMode",
            value: /**
   * 确定预览模式
   */ function determinePreviewMode(typeInference, dataSource) {
                if (!this.determinePreviewCapability(typeInference, dataSource)) {
                    return 'none';
                }
                switch(typeInference.category){
                    case FileCategory.Text:
                    case FileCategory.Code:
                        return 'inline';
                    case FileCategory.Image:
                        return 'modal';
                    case FileCategory.Video:
                        return 'inline';
                    case FileCategory.Audio:
                        return 'inline';
                    case FileCategory.PDF:
                        return 'inline';
                    default:
                        return 'external';
                }
            }
        }
    ]);
    return FileTypeProcessor;
}();
/**
 * 全局文件类型处理器实例
 */ export var fileTypeProcessor = new FileTypeProcessor(dataSourceManager);
var DEFAULT_MIME_TYPE = 'application/octet-stream';
var isFileCategory = function isFileCategory(file, category) {
    return fileTypeProcessor.inferFileType(file).category === category;
};
/**
 * 文件类型判断便捷函数
 */ export var isImageFile = function isImageFile(file) {
    return isFileCategory(file, FileCategory.Image);
};
export var isVideoFile = function isVideoFile(file) {
    return isFileCategory(file, FileCategory.Video);
};
export var isPdfFile = function isPdfFile(file) {
    return isFileCategory(file, FileCategory.PDF);
};
export var isTextFile = function isTextFile(file) {
    return isFileCategory(file, FileCategory.Text);
};
export var isArchiveFile = function isArchiveFile(file) {
    return isFileCategory(file, FileCategory.Archive);
};
export var isAudioFile = function isAudioFile(file) {
    return isFileCategory(file, FileCategory.Audio);
};
export var getMimeType = function getMimeType(file) {
    var result = fileTypeProcessor.processFile(file);
    return result.dataSource.mimeType || DEFAULT_MIME_TYPE;
};
