<template>
  <q-item clickable tag="a" target="self" :to="{ name: link }">
    <q-item-section v-if="icon" avatar>
      <q-icon :name="icon" />
    </q-item-section>

    <q-item-section>
      <q-item-label>{{ realTitle }}</q-item-label>
      <q-item-label caption>
        {{ caption }}
      </q-item-label>
    </q-item-section>
  </q-item>
</template>

<script lang="ts">
import { computed, defineComponent } from '@vue/composition-api';

export default defineComponent({
  name: 'EssentialLink',
  props: {
    title: {
      type: String,
      required: true
    },

    caption: {
      type: String,
      default: ''
    },

    link: {
      type: String,
      default: 'home'
    },

    icon: {
      type: String,
      default: ''
    }
  },

  setup(props, { root }) {
    let title = props.title;
    if (props.title.includes('loadFromStore')) {
      const startIndex = props.title.indexOf('(') + 1;
      const endIndex = props.title.indexOf(')');
      const substring = props.title
        .substring(startIndex, endIndex)
        .replace(/"/g, '');
      console.log(substring);
      console.log(root.$store.getters[substring]);
      console.log('loadFromStore');
      title = computed(() => {
        return root.$store.getters[substring];
      });
    }
    return { realTitle: title };
  }
});
</script>