<template>
  <div v-if="isColorIcon || isSvgIcon">
    <svg :style="setIconSVGStyle" aria-hidden="true" class="icon">
      <use :xlink:href="'#icon-' + name"></use>
    </svg>
  </div>
  <i v-else :class="getIconName" :style="setIconSvgStyle"/>
</template>

<script lang="ts" name="svgIcon" setup>
import 'http://at.alicdn.com/t/c/font_4766628_7ekfe85jxt9.js' // 引入阿里图标库
import {computed} from 'vue';

// 定义父组件传过来的值
const props = defineProps({
  name: { // svg 图标组件名字
    type: String,
  },
  size: { // svg 大小
    type: Number,
    default: () => 14,
  },
  color: { // svg 颜色
    type: String,
  },
  colorIcon: { //彩色
    type: Boolean,
    default: false,
  },
  isSvg: { // 是否是阿里图标库
    type: Boolean,
    default: true,
  }
});

const linesString = ['https', 'http', '/src', '/assets', 'data:image', import.meta.env.VITE_PUBLIC_PATH];

const getIconName = computed(() => {
  return 'iconfont icon-' + props?.name;
});
// 用于判断 element plus 自带 svg 图标的显示、隐藏
const isShowIconSvg = computed(() => {
  return props?.name?.startsWith('ele-');
});
// 用于判断在线链接、本地引入等图标显示、隐藏
const isShowIconImg = computed(() => {
  return linesString.find((str) => props.name?.startsWith(str));
});
// 设置图标样式
const setIconSvgStyle = computed(() => {
  return `font-size: ${props.size}px;color: ${props.color};`;
});
// 设置图片样式
const setIconImgOutStyle = computed(() => {
  return `width: ${props.size}px;height: ${props.size}px;display: inline-block;overflow: hidden;`;
});
// 设置图片样式
// https://gitee.com/lyt-top/vue-next-admin/issues/I59ND0
const setIconSvgInsStyle = computed(() => {
  const filterStyle: string[] = [];
  const compatibles: string[] = ['-webkit', '-ms', '-o', '-moz'];
  compatibles.forEach((j) => filterStyle.push(`${j}-filter: drop-shadow(${props.color} 30px 0);`));
  return `width: ${props.size}px;height: ${props.size}px;position: relative;left: -${props.size}px;${filterStyle.join('')}`;
});

const setIconSVGStyle = computed(() => {
  return `width: ${props.size}px;height: ${props.size}px;display: inline-block;overflow: hidden;`;
});
//是否是彩色图标
const isColorIcon = computed(() => {
  
  return props.colorIcon;
});
// 是否是阿里图标库
const isSvgIcon = computed(() => {
  
  return props.isSvg;
});
</script>
