function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
    try {
        var info = gen[key](arg);
        var value = info.value;
    } catch (error) {
        reject(error);
        return;
    }
    if (info.done) {
        resolve(value);
    } else {
        Promise.resolve(value).then(_next, _throw);
    }
}
function _async_to_generator(fn) {
    return function() {
        var self = this, args = arguments;
        return new Promise(function(resolve, reject) {
            var gen = fn.apply(self, args);
            function _next(value) {
                asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
            }
            function _throw(err) {
                asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
            }
            _next(undefined);
        });
    };
}
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 _instanceof(left, right) {
    if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
        return !!right[Symbol.hasInstance](left);
    } else {
        return left instanceof right;
    }
}
function _ts_generator(thisArg, body) {
    var f, y, t, _ = {
        label: 0,
        sent: function() {
            if (t[0] & 1) throw t[1];
            return t[1];
        },
        trys: [],
        ops: []
    }, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype), d = Object.defineProperty;
    return d(g, "next", {
        value: verb(0)
    }), d(g, "throw", {
        value: verb(1)
    }), d(g, "return", {
        value: verb(2)
    }), typeof Symbol === "function" && d(g, Symbol.iterator, {
        value: function() {
            return this;
        }
    }), g;
    function verb(n) {
        return function(v) {
            return step([
                n,
                v
            ]);
        };
    }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while(g && (g = 0, op[0] && (_ = 0)), _)try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [
                op[0] & 2,
                t.value
            ];
            switch(op[0]){
                case 0:
                case 1:
                    t = op;
                    break;
                case 4:
                    _.label++;
                    return {
                        value: op[1],
                        done: false
                    };
                case 5:
                    _.label++;
                    y = op[1];
                    op = [
                        0
                    ];
                    continue;
                case 7:
                    op = _.ops.pop();
                    _.trys.pop();
                    continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
                        _ = 0;
                        continue;
                    }
                    if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
                        _.label = op[1];
                        break;
                    }
                    if (op[0] === 6 && _.label < t[1]) {
                        _.label = t[1];
                        t = op;
                        break;
                    }
                    if (t && _.label < t[2]) {
                        _.label = t[2];
                        _.ops.push(op);
                        break;
                    }
                    if (t[2]) _.ops.pop();
                    _.trys.pop();
                    continue;
            }
            op = body.call(thisArg, _);
        } catch (e) {
            op = [
                6,
                e
            ];
            y = 0;
        } finally{
            f = t = 0;
        }
        if (op[0] & 5) throw op[1];
        return {
            value: op[0] ? op[1] : void 0,
            done: true
        };
    }
}
import { Paperclip } from "@sofa-design/icons";
import { ConfigProvider, message } from "antd";
import classNames from "classnames";
import React, { useContext } from "react";
import { compileTemplate } from "../../I18n";
import AttachmentButtonPopover, { SupportedFileFormats } from "./AttachmentButtonPopover";
import { useStyle } from "./style";
import { isImageFile } from "./utils";
export * from "./AttachmentButtonPopover";
var WAIT_TIME_MS = 16;
var KB_SIZE = 1024;
var DEFAULT_MESSAGES = {
    uploading: 'Uploading...',
    uploadSuccess: 'Upload success',
    uploadFailed: 'Upload failed',
    maxFileCountExceeded: function(count) {
        return "最多只能上传 ".concat(count, " 个文件");
    },
    minFileCountRequired: function(count) {
        return "至少需要上传 ".concat(count, " 个文件");
    },
    fileSizeExceeded: function(size) {
        return "文件大小超过 ".concat(size, " KB");
    }
};
var waitTime = function(ms) {
    return new Promise(function(resolve) {
        return setTimeout(resolve, ms);
    });
};
var generateFileUUID = function(fileName) {
    return Date.now() + Math.random() * 1000 + fileName;
};
var prepareFile = function(file) {
    file.status = 'uploading';
    file.uuid = generateFileUUID(file.name);
    if (isImageFile(file)) {
        file.previewUrl = URL.createObjectURL(file);
    }
};
var getLocaleMessage = function(locale, key, defaultMsg) {
    return (locale === null || locale === void 0 ? void 0 : locale[key]) || defaultMsg;
};
var validateFileCount = function(newFileCount, existingFileCount, props) {
    var totalFileCount = newFileCount + existingFileCount;
    if (props.maxFileCount && totalFileCount > props.maxFileCount) {
        var _props_locale;
        var msg = ((_props_locale = props.locale) === null || _props_locale === void 0 ? void 0 : _props_locale['markdownInput.maxFileCountExceeded']) ? compileTemplate(props.locale['markdownInput.maxFileCountExceeded'], {
            maxFileCount: String(props.maxFileCount)
        }) : DEFAULT_MESSAGES.maxFileCountExceeded(props.maxFileCount);
        message.error(msg);
        return false;
    }
    if (props.minFileCount && totalFileCount < props.minFileCount) {
        var _props_locale1;
        var msg1 = ((_props_locale1 = props.locale) === null || _props_locale1 === void 0 ? void 0 : _props_locale1['markdownInput.minFileCountRequired']) ? compileTemplate(props.locale['markdownInput.minFileCountRequired'], {
            minFileCount: String(props.minFileCount)
        }) : DEFAULT_MESSAGES.minFileCountRequired(props.minFileCount);
        message.error(msg1);
        return false;
    }
    return true;
};
var validateFileSize = function(file, props) {
    var _props_locale_markdownInputfileSizeExceeded, _props_locale;
    if (!props.maxFileSize || file.size <= props.maxFileSize) return true;
    var maxSizeKB = props.maxFileSize / KB_SIZE;
    var msg = ((_props_locale = props.locale) === null || _props_locale === void 0 ? void 0 : (_props_locale_markdownInputfileSizeExceeded = _props_locale['markdownInput.fileSizeExceeded']) === null || _props_locale_markdownInputfileSizeExceeded === void 0 ? void 0 : _props_locale_markdownInputfileSizeExceeded.replace('${maxSize}', "".concat(maxSizeKB))) || DEFAULT_MESSAGES.fileSizeExceeded(maxSizeKB);
    message.error(msg);
    return false;
};
var updateFileMap = function(map, file, onFileMapChange) {
    if (file.uuid) {
        map.set(file.uuid, file);
        onFileMapChange === null || onFileMapChange === void 0 ? void 0 : onFileMapChange(map);
    }
};
var uploadSingleFile = function(file, index, props) {
    return _async_to_generator(function() {
        var result, url;
        return _ts_generator(this, function(_state) {
            switch(_state.label){
                case 0:
                    if (!props.uploadWithResponse) return [
                        3,
                        2
                    ];
                    return [
                        4,
                        props.uploadWithResponse(file, index)
                    ];
                case 1:
                    result = _state.sent();
                    file.uploadResponse = result;
                    return [
                        2,
                        {
                            url: result.fileUrl,
                            isSuccess: result.uploadStatus === 'SUCCESS',
                            errorMsg: result.errorMessage || null
                        }
                    ];
                case 2:
                    if (!props.upload) return [
                        3,
                        4
                    ];
                    return [
                        4,
                        props.upload(file, index)
                    ];
                case 3:
                    url = _state.sent();
                    return [
                        2,
                        {
                            url: url,
                            isSuccess: !!url,
                            errorMsg: null
                        }
                    ];
                case 4:
                    return [
                        2,
                        {
                            url: file.previewUrl,
                            isSuccess: !!file.previewUrl,
                            errorMsg: null
                        }
                    ];
            }
        });
    })();
};
var handleUploadSuccess = function(file, url, map, props) {
    file.status = 'done';
    file.url = url;
    updateFileMap(map, file, props.onFileMapChange);
    message.success(getLocaleMessage(props.locale, 'uploadSuccess', DEFAULT_MESSAGES.uploadSuccess));
};
var handleUploadError = function(file, errorMsg, map, props) {
    file.status = 'error';
    updateFileMap(map, file, props.onFileMapChange);
    var msg = errorMsg || getLocaleMessage(props.locale, 'uploadFailed', DEFAULT_MESSAGES.uploadFailed);
    message.error(msg);
};
var processFile = function(file, index, map, props) {
    return _async_to_generator(function() {
        var _ref, url, isSuccess, errorMsg, error, errorMessage;
        return _ts_generator(this, function(_state) {
            switch(_state.label){
                case 0:
                    return [
                        4,
                        waitTime(WAIT_TIME_MS)
                    ];
                case 1:
                    _state.sent();
                    if (!validateFileSize(file, props)) {
                        file.status = 'error';
                        updateFileMap(map, file, props.onFileMapChange);
                        return [
                            2
                        ];
                    }
                    _state.label = 2;
                case 2:
                    _state.trys.push([
                        2,
                        4,
                        ,
                        5
                    ]);
                    return [
                        4,
                        uploadSingleFile(file, index, props)
                    ];
                case 3:
                    _ref = _state.sent(), url = _ref.url, isSuccess = _ref.isSuccess, errorMsg = _ref.errorMsg;
                    if (isSuccess && url) {
                        handleUploadSuccess(file, url, map, props);
                    } else {
                        handleUploadError(file, errorMsg, map, props);
                    }
                    return [
                        3,
                        5
                    ];
                case 4:
                    error = _state.sent();
                    errorMessage = _instanceof(error, Error) ? error.message : getLocaleMessage(props.locale, 'uploadFailed', DEFAULT_MESSAGES.uploadFailed);
                    handleUploadError(file, errorMessage, map, props);
                    return [
                        3,
                        5
                    ];
                case 5:
                    return [
                        2
                    ];
            }
        });
    })();
};
/**
 * 上传文件到服务器
 *
 * @param files - 要上传的文件列表
 * @param props - 上传配置项
 * @param props.upload - 上传函数，返回文件 URL
 * @param props.uploadWithResponse - 上传函数（返回完整响应）
 * @param props.fileMap - 文件映射表
 * @param props.onFileMapChange - 文件映射变更回调
 * @param props.maxFileSize - 单文件最大大小（字节）
 * @param props.maxFileCount - 最大文件数量
 * @param props.minFileCount - 最小文件数量
 * @param props.locale - 国际化文案
 */ export var upLoadFileToServer = function(files, props) {
    return _async_to_generator(function() {
        var map, existingFileCount, hideLoading, fileList, i, error;
        return _ts_generator(this, function(_state) {
            switch(_state.label){
                case 0:
                    map = props.fileMap || new Map();
                    existingFileCount = map.size;
                    hideLoading = message.loading(getLocaleMessage(props.locale, 'uploading', DEFAULT_MESSAGES.uploading));
                    fileList = Array.from(files);
                    fileList.forEach(prepareFile);
                    // 在添加到 fileMap 之前先验证文件数量
                    if (!validateFileCount(fileList.length, existingFileCount, props)) {
                        hideLoading();
                        return [
                            2
                        ];
                    }
                    // 验证通过后再添加到 fileMap
                    fileList.forEach(function(file) {
                        return updateFileMap(map, file, props.onFileMapChange);
                    });
                    _state.label = 1;
                case 1:
                    _state.trys.push([
                        1,
                        6,
                        7,
                        8
                    ]);
                    i = 0;
                    _state.label = 2;
                case 2:
                    if (!(i < fileList.length)) return [
                        3,
                        5
                    ];
                    return [
                        4,
                        processFile(fileList[i], i, map, props)
                    ];
                case 3:
                    _state.sent();
                    _state.label = 4;
                case 4:
                    i++;
                    return [
                        3,
                        2
                    ];
                case 5:
                    return [
                        3,
                        8
                    ];
                case 6:
                    error = _state.sent();
                    fileList.forEach(function(file) {
                        file.status = 'error';
                        updateFileMap(map, file, props.onFileMapChange);
                    });
                    message.error(getLocaleMessage(props.locale, 'uploadFailed', DEFAULT_MESSAGES.uploadFailed));
                    return [
                        3,
                        8
                    ];
                case 7:
                    hideLoading();
                    return [
                        7
                    ];
                case 8:
                    return [
                        2
                    ];
            }
        });
    })();
};
var BUTTON_WITH_TITLE_STYLE = {
    display: 'flex',
    alignItems: 'center',
    gap: 4
};
var BUTTON_TITLE_STYLE = {
    font: 'var(--font-text-body-base)',
    letterSpacing: 'var(--letter-spacing-body-base, normal)',
    color: 'var(--color-gray-text-default)'
};
var ButtonContent = function(param) {
    var title = param.title;
    return /*#__PURE__*/ React.createElement(React.Fragment, null, /*#__PURE__*/ React.createElement(Paperclip, null), title !== null && title ? /*#__PURE__*/ React.createElement("div", {
        style: BUTTON_TITLE_STYLE
    }, title) : null);
};
/**
 * 附件上传按钮组件
 *
 * 提供文件附件上传功能，支持图片、文档、音频、视频等多种格式
 *
 * @example
 * ```tsx
 * <AttachmentButton
 *   supportedFormat={SupportedFileFormats.image}
 *   upload={uploadFile}
 *   fileMap={fileMap}
 *   onFileMapChange={setFileMap}
 *   uploadImage={handleUpload}
 * />
 * ```
 */ export var AttachmentButton = function(param) {
    var disabled = param.disabled, uploadImage = param.uploadImage, title = param.title, supportedFormat = param.supportedFormat, render = param.render;
    var context = useContext(ConfigProvider.ConfigContext);
    var prefix = context === null || context === void 0 ? void 0 : context.getPrefixCls('agentic-md-editor-attachment-button');
    var _useStyle = useStyle(prefix), wrapSSR = _useStyle.wrapSSR, hashId = _useStyle.hashId;
    var format = supportedFormat || SupportedFileFormats.image;
    var handleClick = function() {
        if (disabled) return;
        uploadImage === null || uploadImage === void 0 ? void 0 : uploadImage();
    };
    var buttonWithStyle = /*#__PURE__*/ React.createElement("div", {
        style: BUTTON_WITH_TITLE_STYLE
    }, /*#__PURE__*/ React.createElement(ButtonContent, {
        title: title
    }));
    var wrapper = render ? render({
        children: buttonWithStyle,
        supportedFormat: format
    }) : /*#__PURE__*/ React.createElement(AttachmentButtonPopover, {
        supportedFormat: format,
        uploadImage: uploadImage
    }, buttonWithStyle);
    return wrapSSR(/*#__PURE__*/ React.createElement("div", {
        className: classNames("".concat(prefix), hashId, _define_property({}, "".concat(prefix, "-disabled"), disabled)),
        onClick: handleClick,
        "data-testid": "attachment-button"
    }, wrapper));
};
