"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Icon = exports._InternalIcon = exports.IconContext = void 0;
exports.IconContextProvider = IconContextProvider;
const tslib_1 = require("tslib");
const classnames_1 = tslib_1.__importDefault(require("classnames"));
const react_1 = tslib_1.__importDefault(require("react"));
const iconManager_1 = require("./iconfont/iconManager");
const util_1 = require("./util");
require("./styles.less");
exports.IconContext = react_1.default.createContext({
    getIcon: (iconKey, options) => iconKey,
});
function IconContextProvider(props) {
    return (react_1.default.createElement(exports.IconContext.Provider, { value: props.value },
        react_1.default.createElement(exports.IconContext.Consumer, null, (value) => (props.value === value ? props.children : null))));
}
/**
 * 在 IDE 集成环境下使用自定义图标时，建议提供自定义 IconKeys 类型
 * @example
 * ```ts
 * const demo = () => (
 *    <Icon<'foo' | 'bar'> icon='bar' />
 * );
 * //自定义前缀
 * const demo = () => (
 *    <Icon<'foo' | 'bar'> iconClass=`${customPrefix} bar` />
 * );
 * ```
 */
const IconBase = function (props, ref) {
    const { size = 'small', loading, icon, iconClass, className, tooltip, rotate, anim, fill, disabled, onClick, children, resourceOptions, animate, style = {}, ...restProps } = props;
    const iconShapeOptions = { rotate, anim, fill };
    let iconClx;
    if (icon) {
        // 因为 getIcon 默认是倒序查找优先级
        // 这里的逻辑会导致是优先用 defaultIconFont 里面去查找
        // 所以这里应该是跟 getIcon 逻辑表现不太一样
        // 此外这里的 useContext 的话，作用看起来意义不大了，因为 getIcon 是个函数，本质上无法做到通知
        // 这里为了兼容被废弃的 icon 使用了 `defaultIconMap` 去取值
        // 但是上方的类型 DefaultIconKeys 里面已经不包含被废弃的 icon 了，避免继续使用废弃的图标
        if (iconManager_1.defaultIconMap[icon]) {
            iconClx = (0, util_1.getIcon)(icon, iconShapeOptions);
        }
        else {
            const { getIcon: getContextIcon, getResourceIcon } = react_1.default.useContext(exports.IconContext);
            if (resourceOptions && getResourceIcon) {
                iconClx = getResourceIcon(icon, resourceOptions);
            }
            else {
                iconClx = getContextIcon(icon, iconShapeOptions);
            }
        }
    }
    else {
        iconClx = iconClass;
    }
    if (animate && animate === 'spin') {
        style['animation'] = 'kt-icon-spin 1.5s steps(30) infinite';
    }
    return (react_1.default.createElement("span", { ...restProps, title: tooltip, onClick: onClick, ref: ref, className: (0, classnames_1.default)('kt-icon', iconClx, className, {
            'kt-icon-loading': loading,
            'kt-icon-disabled': !!disabled,
            [`kt-icon-${size}`]: !!size,
            'kt-icon-clickable': !!onClick,
            'kt-icon-resource': !!resourceOptions,
            expanded: !!resourceOptions && resourceOptions.isOpenedDirectory,
        }), style: style }, children));
};
// for ts type usage for iconfont-cn.tsx
exports._InternalIcon = react_1.default.memo(react_1.default.forwardRef(IconBase));
exports.Icon = react_1.default.memo(react_1.default.forwardRef(IconBase));
//# sourceMappingURL=icon.js.map