/**
 * amis v3.1.0
 * Copyright 2018-2023 baidu
 */

import { __extends, __decorate, __metadata } from 'tslib';
import React from 'react';
import { filter, CustomStyle, autobind, Renderer } from 'amis-core';
import { getIcon, withBadge } from 'amis-ui';
import { isObject } from 'lodash';

var Icon = /** @class */ (function (_super) {
    __extends(Icon, _super);
    function Icon() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    Icon.prototype.handleClick = function (e) {
        var _a = this.props, dispatchEvent = _a.dispatchEvent, data = _a.data;
        dispatchEvent(e, data);
    };
    Icon.prototype.handleMouseEnter = function (e) {
        var _a = this.props, dispatchEvent = _a.dispatchEvent, data = _a.data;
        dispatchEvent(e, data);
    };
    Icon.prototype.handleMouseLeave = function (e) {
        var _a = this.props, dispatchEvent = _a.dispatchEvent, data = _a.data;
        dispatchEvent(e, data);
    };
    Icon.prototype.render = function () {
        var _a = this.props, vendor = _a.vendor, cx = _a.classnames, className = _a.className, style = _a.style, data = _a.data, id = _a.id, themeCss = _a.themeCss, css = _a.css, env = _a.env;
        var icon = this.props.icon;
        if (typeof icon !== 'string') {
            if (isObject(icon) &&
                typeof icon.id === 'string' &&
                icon.id.startsWith('svg-')) {
                return (React.createElement("svg", { className: cx('icon', className), style: style, onClick: this.handleClick, onMouseEnter: this.handleMouseEnter, onMouseLeave: this.handleMouseLeave },
                    React.createElement("use", { xlinkHref: "#".concat(icon.id.replace(/^svg-/, '')) })));
            }
            return;
        }
        icon = filter(icon, data);
        var CustomIcon = getIcon(icon);
        if (CustomIcon) {
            return (React.createElement(CustomIcon, { className: cx(className, "icon-".concat(icon)), style: style, onClick: this.handleClick, onMouseEnter: this.handleMouseEnter, onMouseLeave: this.handleMouseLeave }));
        }
        var isURLIcon = (icon === null || icon === void 0 ? void 0 : icon.indexOf('.')) !== -1 || icon.startsWith('data:image/');
        var iconPrefix = '';
        if (vendor === 'iconfont') {
            iconPrefix = "iconfont icon-".concat(icon);
        }
        else if (vendor === 'fa') {
            //默认是fontawesome v4，兼容之前配置
            iconPrefix = "".concat(vendor, " ").concat(vendor, "-").concat(icon);
        }
        else {
            // 如果vendor为空，则不设置前缀,这样可以支持fontawesome v5、fontawesome v6或者其他框架
            iconPrefix = "".concat(icon);
        }
        return (React.createElement(React.Fragment, null,
            isURLIcon ? (React.createElement("img", { className: cx('Icon', className), src: icon, style: style, onClick: this.handleClick, onMouseEnter: this.handleMouseEnter, onMouseLeave: this.handleMouseLeave })) : (React.createElement("i", { className: cx(iconPrefix, className), style: style, onClick: this.handleClick, onMouseEnter: this.handleMouseEnter, onMouseLeave: this.handleMouseLeave })),
            React.createElement(CustomStyle, { config: {
                    themeCss: themeCss || css,
                    classNames: [
                        {
                            key: 'className',
                            value: className
                        }
                    ],
                    id: id
                }, env: env })));
    };
    Icon.defaultProps = {
        icon: '',
        vendor: 'fa'
    };
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Object]),
        __metadata("design:returntype", void 0)
    ], Icon.prototype, "handleClick", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Object]),
        __metadata("design:returntype", void 0)
    ], Icon.prototype, "handleMouseEnter", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Object]),
        __metadata("design:returntype", void 0)
    ], Icon.prototype, "handleMouseLeave", null);
    return Icon;
}(React.Component));
/** @class */ ((function (_super) {
    __extends(IconRenderer, _super);
    function IconRenderer() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    IconRenderer = __decorate([
        Renderer({
            type: 'icon'
        })
        // @ts-ignore 类型没搞定
        ,
        withBadge
    ], IconRenderer);
    return IconRenderer;
})(Icon));

export { Icon };
