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 _object_spread(target) {
    for(var i = 1; i < arguments.length; i++){
        var source = arguments[i] != null ? arguments[i] : {};
        var ownKeys = Object.keys(source);
        if (typeof Object.getOwnPropertySymbols === "function") {
            ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
                return Object.getOwnPropertyDescriptor(source, sym).enumerable;
            }));
        }
        ownKeys.forEach(function(key) {
            _define_property(target, key, source[key]);
        });
    }
    return target;
}
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, Popover, Tooltip } from "antd";
import React, { useContext, useMemo, useState } from "react";
import { useRefFunction } from "../../Hooks/useRefFunction";
import { compileTemplate, I18nContext } from "../../I18n";
import { isMobileDevice, isVivoOrOppoDevice } from "./utils";
var CONTENT_STYLE = {
    fontSize: 16,
    lineHeight: '1.5em',
    maxWidth: 275
};
export var SupportedFileFormats = {
    image: {
        icon: /*#__PURE__*/ React.createElement(FileImageOutlined, null),
        type: 'image',
        extensions: [
            'jpg',
            'jpeg',
            'png',
            'gif',
            'bmp',
            'webp',
            'svg'
        ]
    },
    document: {
        icon: /*#__PURE__*/ React.createElement(FileTextFilled, null),
        type: 'document',
        extensions: [
            'pdf',
            'markdown',
            'ppt',
            'html',
            'xls',
            'xlsx',
            'cs',
            'docx',
            'pptx',
            'xml'
        ]
    },
    audio: {
        icon: /*#__PURE__*/ React.createElement(AudioOutlined, null),
        type: 'audio',
        extensions: [
            'mp3',
            'wav'
        ]
    },
    video: {
        icon: /*#__PURE__*/ React.createElement(VideoCameraOutlined, null),
        type: 'video',
        extensions: [
            'mp4',
            'avi',
            'mov'
        ]
    }
};
var DEFAULT_FORMAT_MESSAGE = 'Supports ${extensions} formats.';
var buildFormatMessage = function buildFormatMessage(format, locale) {
    var _ref;
    var _format_extensions;
    var extensions = ((_format_extensions = format.extensions) === null || _format_extensions === void 0 ? void 0 : _format_extensions.join(', ')) || '';
    var template = (_ref = locale === null || locale === void 0 ? void 0 : locale['input.supportedFormatMessage']) !== null && _ref !== void 0 ? _ref : DEFAULT_FORMAT_MESSAGE;
    return compileTemplate(template, {
        extensions: extensions
    });
};
var FormatContent = function FormatContent(param) {
    var format = param.format, locale = param.locale;
    if (format.content) return /*#__PURE__*/ React.createElement(React.Fragment, null, format.content);
    return /*#__PURE__*/ React.createElement("div", {
        style: CONTENT_STYLE
    }, buildFormatMessage(format, locale));
};
export var AttachmentSupportedFormatsContent = function AttachmentSupportedFormatsContent(param) {
    var supportedFormat = param.supportedFormat, locale = param.locale;
    var format = supportedFormat || SupportedFileFormats.image;
    return /*#__PURE__*/ React.createElement(FormatContent, {
        format: format,
        locale: locale
    });
};
export var AttachmentButtonPopover = function AttachmentButtonPopover(param) {
    var children = param.children, supportedFormat = param.supportedFormat, uploadImage = param.uploadImage, localeProp = param.locale;
    var _useState = _sliced_to_array(useState(false), 2), modalOpen = _useState[0], setModalOpen = _useState[1];
    var _useContext = useContext(I18nContext), contextLocale = _useContext.locale;
    var locale = localeProp !== undefined ? _object_spread({}, contextLocale, localeProp) : contextLocale;
    var isVivoOrOppo = useMemo(function() {
        return isVivoOrOppoDevice();
    }, []);
    var isMobile = useMemo(function() {
        return isMobileDevice();
    }, []);
    var trigger = useMemo(function() {
        return isVivoOrOppo ? [
            'click'
        ] : [
            'hover',
            'click'
        ];
    }, [
        isVivoOrOppo
    ]);
    var handleClick = useRefFunction(function(e) {
        if (isVivoOrOppo) {
            e.stopPropagation();
            e.preventDefault();
            setModalOpen(true);
        }
    });
    var handleOpenGallery = useRefFunction(function() {
        uploadImage === null || uploadImage === void 0 ? void 0 : uploadImage(true);
        setModalOpen(false);
    });
    var handleOpenFile = useRefFunction(function() {
        uploadImage === null || uploadImage === void 0 ? void 0 : uploadImage(false);
        setModalOpen(false);
    });
    if (isVivoOrOppo) {
        return /*#__PURE__*/ React.createElement("div", {
            onClick: function onClick(e) {
                if (isVivoOrOppo) {
                    e.stopPropagation();
                    e.preventDefault();
                }
            }
        }, /*#__PURE__*/ React.createElement("span", {
            onClick: handleClick
        }, children), /*#__PURE__*/ React.createElement(Modal, {
            open: modalOpen,
            onCancel: function onCancel() {
                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 onClick(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']) || 'Open Gallery'), /*#__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']) || 'Open File'))));
    }
    // 移动设备无稳定 hover：用点击 Popover 展示支持格式说明（vivo/oppo 仍走上方 Modal）
    if (isMobile) {
        return /*#__PURE__*/ React.createElement(Popover, {
            trigger: "click",
            placement: "top",
            arrow: false,
            content: /*#__PURE__*/ React.createElement(AttachmentSupportedFormatsContent, {
                supportedFormat: supportedFormat,
                locale: locale
            })
        }, /*#__PURE__*/ React.createElement("span", {
            style: {
                display: 'inline-flex'
            }
        }, children));
    }
    return /*#__PURE__*/ React.createElement(Tooltip, {
        arrow: false,
        mouseEnterDelay: 1,
        trigger: trigger,
        title: /*#__PURE__*/ React.createElement(AttachmentSupportedFormatsContent, {
            supportedFormat: supportedFormat,
            locale: locale
        })
    }, /*#__PURE__*/ React.createElement("span", {
        onClick: function onClick(e) {
            if (isVivoOrOppo) {
                e.stopPropagation();
                e.preventDefault();
            }
        }
    }, children));
};
export default AttachmentButtonPopover;
