<script>
import { GlLoadingIcon } from '@gitlab-org/gitlab-ui';
import getIconForFile from './file_icon/file_icon_map';
import icon from '../../vue_shared/components/icon.vue';

/* This is a re-usable vue component for rendering a svg sprite
    icon

    Sample configuration:

    <file-icon
      name="retry"
      :size="32"
      css-classes="top"
    />

  */
export default {
  components: {
    icon,
    GlLoadingIcon,
  },
  props: {
    fileName: {
      type: String,
      required: true,
    },

    folder: {
      type: Boolean,
      required: false,
      default: false,
    },

    opened: {
      type: Boolean,
      required: false,
      default: false,
    },

    loading: {
      type: Boolean,
      required: false,
      default: false,
    },

    size: {
      type: Number,
      required: false,
      default: 16,
    },

    cssClasses: {
      type: String,
      required: false,
      default: '',
    },
  },
  computed: {
    spriteHref() {
      const iconName = getIconForFile(this.fileName) || 'file';
      return `${gon.sprite_file_icons}#${iconName}`;
    },
    folderIconName() {
      return this.opened ? 'folder-open' : 'folder';
    },
    iconSizeClass() {
      return this.size ? `s${this.size}` : '';
    },
  },
};
</script>
<template>
  <span>
    <svg
      v-if="!loading && !folder"
      :class="[iconSizeClass, cssClasses]"
    >
      <use v-bind="{ 'xlink:href':spriteHref }" />
    </svg>
    <icon
      v-if="!loading && folder"
      :name="folderIconName"
      :size="size"
      css-classes="folder-icon"
    />
    <gl-loading-icon
      v-if="loading"
      :inline="true"
    />
  </span>
</template>
