import Resume from './resume';
import Direct from './direct';
import Logger from '../logger';
import { Observable, MB, normalizeUploadConfig } from '../utils';
import { HostPool } from './hosts';
export * from './base';
export * from './resume';
export function createUploadManager(options, handlers, hostPool, logger) {
    if (options.config && options.config.forceDirect) {
        logger.info('ues forceDirect mode.');
        return new Direct(options, handlers, hostPool, logger);
    }
    if (options.file.size > 4 * MB) {
        logger.info('file size over 4M, use Resume.');
        return new Resume(options, handlers, hostPool, logger);
    }
    logger.info('file size less or equal than 4M, use Direct.');
    return new Direct(options, handlers, hostPool, logger);
}
/**
 * @param file 上传文件
 * @param key 目标文件名
 * @param token 上传凭证
 * @param putExtra 上传文件的相关资源信息配置
 * @param config 上传任务的配置
 * @returns 返回用于上传任务的可观察对象
 */
export default function upload(file, key, token, putExtra, config) {
    // 为每个任务创建单独的 Logger
    var logger = new Logger(token, config === null || config === void 0 ? void 0 : config.disableStatisticsReport, config === null || config === void 0 ? void 0 : config.debugLogLevel, file.name);
    var options = {
        file: file,
        key: key,
        token: token,
        putExtra: putExtra,
        config: normalizeUploadConfig(config, logger)
    };
    // 创建 host 池
    var hostPool = new HostPool(options.config.uphost);
    return new Observable(function (observer) {
        var manager = createUploadManager(options, {
            onData: function (data) { return observer.next(data); },
            onError: function (err) { return observer.error(err); },
            onComplete: function (res) { return observer.complete(res); }
        }, hostPool, logger);
        manager.putFile();
        return manager.stop.bind(manager);
    });
}
//# sourceMappingURL=index.js.map