<template>
  <div
    v-if="isExternal"
    :style="styleExternalIcon"
    class="svg-external-icon"
    v-on="$listeners"
  />
  <svg
    v-else
    class="svg-icon"
    :class="svgClass"
    width="1em"
    height="1em"
    fill="currentColor"
    aria-hidden="true"
    focusable="false"
    v-on="$listeners"
  >
    <use :href="iconName" />
  </svg>
</template>

<script>
import { isExternal } from "@mega-apps/utils";

export default {
  name: "SvgIcon",
  props: {
    iconClass: {
      type: String,
      required: true,
    },
    className: {
      type: String,
      default: "",
    },
  },
  computed: {
    isExternal() {
      return isExternal(this.iconClass);
    },
    iconName() {
      return `#icon-${this.iconClass}`;
    },
    svgClass() {
      if (this.className) {
        return this.className;
      }
      return "";
    },
    styleExternalIcon() {
      return {
        mask: `url(${this.iconClass}) no-repeat 50% 50%`,
        "-webkit-mask": `url(${this.iconClass}) no-repeat 50% 50%`,
      };
    },
  },
};
</script>

<style lang="less" scoped>
.svg-icon {
  display: inline-block;
  margin-top: -0.25em;
  vertical-align: middle;
}

.svg-external-icon {
  display: inline-block;
  background-color: currentColor;
  mask-size: cover !important;
}
</style>
