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 { AudioOutlined, FileImageOutlined, FileTextFilled, FolderOpenOutlined, PictureOutlined, VideoCameraOutlined } from "@ant-design/icons";
import { Button, Modal, Tooltip } from "antd";
import React, { useCallback, useContext, useMemo, useState } from "react";
import { I18nContext } from "../../I18n";
import { isMobileDevice, isVivoOrOppoDevice, kbToSize } from "./utils";
var FILE_SIZE_UNITS = {
    KB: 1024,
    MB: 1024 * 1024
};
var DEFAULT_MAX_SIZE = 5000;
var CONTENT_STYLE = {
    fontSize: 16,
    lineHeight: '1.5em',
    maxWidth: 275
};
export var SupportedFileFormats = {
    image: {
        icon: /*#__PURE__*/ React.createElement(FileImageOutlined, null),
        type: '图片',
        maxSize: 10 * FILE_SIZE_UNITS.KB,
        extensions: [
            'jpg',
            'jpeg',
            'png',
            'gif'
        ]
    },
    document: {
        icon: /*#__PURE__*/ React.createElement(FileTextFilled, null),
        type: '文档',
        maxSize: 10 * FILE_SIZE_UNITS.KB,
        extensions: [
            'pdf',
            'markdown',
            'ppt',
            'html',
            'xls',
            'xlsx',
            'cs',
            'docx',
            'pptx',
            'xml'
        ]
    },
    audio: {
        icon: /*#__PURE__*/ React.createElement(AudioOutlined, null),
        type: '音频',
        maxSize: 50 * FILE_SIZE_UNITS.KB,
        extensions: [
            'mp3',
            'wav'
        ]
    },
    video: {
        icon: /*#__PURE__*/ React.createElement(VideoCameraOutlined, null),
        type: '视频',
        maxSize: 100 * FILE_SIZE_UNITS.KB,
        extensions: [
            'mp4',
            'avi',
            'mov'
        ]
    }
};
var buildFormatMessage = function(format) {
    var _format_extensions;
    var maxSize = kbToSize(format.maxSize || DEFAULT_MAX_SIZE);
    var extensions = ((_format_extensions = format.extensions) === null || _format_extensions === void 0 ? void 0 : _format_extensions.join(', ')) || '';
    return "支持上传文件，每个文件不超过 ".concat(maxSize, "，支持 ").concat(extensions, "等格式。");
};
var FormatContent = function(param) {
    var format = param.format;
    if (format.content) return /*#__PURE__*/ React.createElement(React.Fragment, null, format.content);
    return /*#__PURE__*/ React.createElement("div", {
        style: CONTENT_STYLE
    }, buildFormatMessage(format));
};
export var AttachmentSupportedFormatsContent = function(param) {
    var supportedFormat = param.supportedFormat;
    var format = supportedFormat || SupportedFileFormats.image;
    return /*#__PURE__*/ React.createElement(FormatContent, {
        format: format
    });
};
export var AttachmentButtonPopover = function(param) {
    var children = param.children, supportedFormat = param.supportedFormat, uploadImage = param.uploadImage;
    var _useState = _sliced_to_array(useState(false), 2), modalOpen = _useState[0], setModalOpen = _useState[1];
    var locale = useContext(I18nContext).locale;
    var isVivoOrOppo = useMemo(function() {
        return isVivoOrOppoDevice();
    }, []);
    var isMobile = useMemo(function() {
        return isMobileDevice();
    }, []);
    var trigger = useMemo(function() {
        return isVivoOrOppo ? [
            'click'
        ] : [
            'hover',
            'click'
        ];
    }, [
        isVivoOrOppo
    ]);
    var handleClick = useCallback(function(e) {
        if (isVivoOrOppo) {
            e.stopPropagation();
            e.preventDefault();
            setModalOpen(true);
        }
    }, [
        isVivoOrOppo
    ]);
    var handleOpenGallery = useCallback(function() {
        uploadImage === null || uploadImage === void 0 ? void 0 : uploadImage(true);
        setModalOpen(false);
    }, [
        uploadImage
    ]);
    var handleOpenFile = useCallback(function() {
        uploadImage === null || uploadImage === void 0 ? void 0 : uploadImage(false);
        setModalOpen(false);
    }, [
        uploadImage
    ]);
    if (isVivoOrOppo) {
        return /*#__PURE__*/ React.createElement("div", {
            onClick: function(e) {
                if (isVivoOrOppo) {
                    e.stopPropagation();
                    e.preventDefault();
                }
            }
        }, /*#__PURE__*/ React.createElement("span", {
            onClick: handleClick
        }, children), /*#__PURE__*/ React.createElement(Modal, {
            open: modalOpen,
            onCancel: function() {
                return setModalOpen(false);
            },
            footer: null,
            closable: false,
            maskClosable: true,
            centered: true,
            styles: {
                content: {
                    padding: 0
                },
                body: {
                    padding: 0
                }
            },
            width: 120
        }, /*#__PURE__*/ React.createElement("div", {
            style: {
                display: 'flex',
                flexDirection: 'column',
                gap: 2,
                padding: '12px 0'
            },
            onClick: function(e) {
                e.stopPropagation();
                e.preventDefault();
            }
        }, /*#__PURE__*/ React.createElement(Button, {
            color: "default",
            variant: "text",
            icon: /*#__PURE__*/ React.createElement(PictureOutlined, null),
            onClick: handleOpenGallery
        }, (locale === null || locale === void 0 ? void 0 : locale['input.openGallery']) || '打开相册'), /*#__PURE__*/ React.createElement(Button, {
            color: "default",
            variant: "text",
            icon: /*#__PURE__*/ React.createElement(FolderOpenOutlined, null),
            onClick: handleOpenFile
        }, (locale === null || locale === void 0 ? void 0 : locale['input.openFile']) || '打开文件'))));
    }
    // 如果是移动设备，不显示 Tooltip
    if (isMobile) {
        return /*#__PURE__*/ React.createElement("span", null, children);
    }
    return /*#__PURE__*/ React.createElement(Tooltip, {
        arrow: false,
        mouseEnterDelay: 1,
        trigger: trigger,
        title: /*#__PURE__*/ React.createElement(AttachmentSupportedFormatsContent, {
            supportedFormat: supportedFormat
        })
    }, /*#__PURE__*/ React.createElement("span", {
        onClick: function(e) {
            if (isVivoOrOppo) {
                e.stopPropagation();
                e.preventDefault();
            }
        }
    }, children));
};
export default AttachmentButtonPopover;
