<template>
  <span class="icon" :class="sizeClass" @click="$emit('click', $event)">
    <svg v-html="svgContent" v-bind="svgAttributes"></svg>
  </span>
</template>

<script>

  import X from "open-iconic/svg/x";
  import Person from "open-iconic/svg/person";
  import LockLocked from "open-iconic/svg/lock-locked";
  import LockUnlocked from "open-iconic/svg/lock-unlocked";

  const iconMap = {
    x: X,
    person: Person,
    'lock-locked': LockLocked,
    'lock-unlocked': LockUnlocked
  };

  const sizeMap = {
    sm: 'is-small',
    md: '' ,
    lg: 'is-medium',
    xl: 'is-large'
  };

  export default {
    props: {
      icon: {
        validator: (i) => iconMap[i] !== undefined
      },
      size: {
        required: false,
        type: String,
        validator: (s) => sizeMap[s] !== undefined,
        default: 'md'
      }
    },

    computed: {
      svgObj() {
        return iconMap[this.icon];
      },
      svgAttributes() {
        const attrs = {
          class: this.size
        };

        for (let a of ['viewBox', 'xmlns']) {
          if (this.svgObj.attributes[a]) {
            attrs[a] = this.svgObj.attributes[a];
          }
        }

        return attrs;
      },
      svgContent() {
        return this.svgObj.content;
      },
      sizeClass() {
        return sizeMap[this.size];
      }
    }
  }

</script>

<style lang="scss" scoped>

  .icon {
    svg {
      width: 100%;
      height: 100%;
      fill: currentColor;

      &.sm {
        width: 1em;
        height: 1em;
      }

      &.md {
        width: 1.33em;
        height: 1.33em;
      }

      &.lg {
        width: 2em;
        height: 2em;
      }

      &.xl {
        width: 3em;
        height: 3em;
      }
    }

  }

</style>