<template>
  <svg :class="[prefixCls, spin && 'svg-icon-spin']" :style="getStyle" aria-hidden="true">
    <use :xlink:href="symbolId" />
  </svg>
</template>

<script lang="ts" setup>
import type { CSSProperties } from 'vue'

const props = defineProps({
  prefix: {
    type: String,
    default: 'icon'
  },
  name: {
    type: String,
    required: true
  },
  height: {
    type: [Number, String],
    default: 0
  },
  width: {
    type: [Number, String],
    default: 0
  },
  size: {
    type: [Number, String],
    default: 16
  },
  spin: {
    type: Boolean,
    default: false
  }
})
const prefixCls = ref<string>('svg-icon')
const symbolId = computed(() => `#${props.prefix}-${props.name}`)
const getStyle = computed((): CSSProperties => {
  const { width, height, size } = props
  let w = `${width || size}`
  w = `${w.replace('px', '')}px`
  let h = `${height || size}`
  h = `${h.replace('px', '')}px`
  return {
    width: w,
    height: h
  }
})
</script>

<style scoped>
.svg-icon {
  display: inline-block;
  overflow: hidden;
  vertical-align: middle;
  fill: currentColor;
}

.svg-icon-spin {
  animation: loadingCircle 1s infinite linear;
}
@keyframes loadingCircle {
  from {
    transform: rotate(0);
  }
  to {
    transform: rotate(360deg);
  }
}
</style>
