<template>
    <div>
        <div class="tag-list">
            <tag-list-item ref="items"
                v-for="(tag, index) in tags" v-if="tags.length > 0"
                v-bind:label="tag.tag_label"
                v-bind:index="index"
                v-bind:original-accuracy="tag.accuracy"
                v-bind:original-relevancy="tag.relevancy"></tag-list-item>
        </div>
        <typeahead ref="typeahead" placeholder="Rechercher"></typeahead>
    </div>
</template>

<script>

    import TagListItem from './TagListItem.vue'
    import Typeahead from './Typeahead.vue'

    export default {
        props: ['original-tags'],
        components: {
            "typeahead": Typeahead,
            "tag-list-item": TagListItem
        },
        data() {
            return {
                tags: []
            }
        },
        watch: {
            tags: function(tags) {
                this.$emit('change', { tags: tags });
            }
        },
        mounted() {
            if (this.originalTags) {
                this.tags = this.originalTags;
            }
            this.$refs.typeahead.$on('selected', (tag) => {
                this.tags.push(tag);
            });
        },

        methods: {
            hideAll: function() {
                this.$refs.items.forEach((item) => item.hide());
            },
            replaceItemAt: function(index, data) {
                const tag = this.tags[index];
                Object.assign(tag, data);
                this.tags.splice(index, 1, tag);
            },
            removeItemAt: function(index) {
                this.tags.$remove(this.tags[index]);
            },
        }

    }

</script>

<style scoped>

.tag-list {
    margin-bottom: 15px;
}

</style>
