<template>
    <div class="wcc-multi-select-input" @click="focusInput">
        <div v-for="(item, index) in selectedItems" :key="index" class="wcc-multi-select-tag">
            <span>{{ item }}</span>
            <button @click.stop="removeItem(index)" class="wcc-close-icon">×</button>
        </div>

        <input type="text" v-model="currentInput" @keyup.enter="addItem" @blur="savePendingInput"  ref="inputField"
            :placeholder="showPlaceholderText" class="wcc-input-field" />
    </div>
</template>

<script>
export default {
    name: "MultiSelectInput",
    props: {
        title: String,
        placeholderText: String,
        initialItems: {
            type: Array,
            default: () => [],
        },
    },
    data() {
        return {
            currentInput: "",
            selectedItems: [...this.initialItems], // Initialize with a copy of initialItems
        };
    },
    watch: {
        initialItems(newItems) {
            // Update selectedItems whenever initialItems changes
            this.selectedItems = [...newItems];
        },
    },
    methods: {
        addItem() {
            const trimmedInput = this.currentInput.trim();
            if (trimmedInput && !this.selectedItems.includes(trimmedInput)) {
                this.selectedItems.push(trimmedInput);
                this.currentInput = "";
                this.$emit("update:selectedItems", this.selectedItems);
            }
        },
        removeItem(index) {
            this.selectedItems.splice(index, 1);
            this.$emit("update:selectedItems", this.selectedItems);
        },
        focusInput() {
            this.$refs.inputField.focus();
        },
        savePendingInput() {
            // Add any unsaved input to selectedItems
            if (this.currentInput.trim()) {
                this.addItem();
            }
        }
    },
    computed: {
        showPlaceholderText() {
            return this.selectedItems.length === 0 ? this.placeholderText : ""; // Show placeholder only when empty
        },
    },
};
</script>

<style scoped>
.wcc-multi-select-input {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    padding: 4px;
    border: 1px solid #ccc;
    border-radius: 4px;
    min-height: 38px;
    gap: 4px;
    max-width: 546px;
}

.wcc-multi-select-tag {
    display: flex;
    align-items: center;
    background-color: #ebf1fd;
    border-radius: 4px;
    padding: 4px 8px;
    font-size: 14px;
}

.wcc-close-icon {
    background: none;
    border: none;
    font-size: 14px;
    margin-left: 4px;
    cursor: pointer;
}

.wcc-input-field {
    border: none;
    outline: none;
    flex-grow: 1;
    min-width: 120px;
    padding: 4px;
    font-size: 14px;
    box-shadow: none;
}

.wcc-input-field:focus {
    box-shadow: none;
}

.wcc-pages-to-hide-help {
    font-family: Inter, sans-serif;
    /* Added fallback font */
    font-size: 12px;
    font-style: italic;
    font-weight: 400;
    line-height: 14.52px;
    text-align: left;
    padding-top: 10px;
}
</style>