/**
 * Icon 自定义组件，提供定制Icon组件能力
 *
 *
 */

import * as React from 'react';
import { createFromIconfontCN } from '@ant-design/icons';
import { Util } from '@/utils';

export declare type IconType = React.ReactNode | string;

const Icon = createFromIconfontCN({
  scriptUrl: [
    // @ts-ignore
    `${Util.getPublicPath()}css/iconfont/iconfont.js`,
  ],
});

/**
 * 根据参数构建图标
 * @param icon
 */
export const buildIcon: (
  type: string,
  icon: IconType,
  style?: React.CSSProperties,
  className?: string,
) => React.ReactNode | null = function (
  type,
  icon,
  style = {},
  className = '',
  ) {
    let comp;
    switch (type) {
      case 'IMAGE':
        comp =
          typeof icon === 'string' ? (
            <img
              src={icon}
              className={className}
              style={{ width: 16, height: 16, ...style }}
            />
          ) : (
            icon
          );
        break;
      default:
        comp =
          typeof icon === 'string' ? (
            <Icon type={icon} style={style} className={className} />
          ) : (
            icon
          );
    }
    return comp;
  };

export default Icon;
