<script setup lang="ts">
import { validateSeedPhrase18 } from '@/utils'
import { ref } from 'vue'

const emit = defineEmits<{
    (e: 'update:valid', valid: boolean): void
    (e: 'submit', words: string[]): void
}>()

const seedWords = ref<string[]>(Array.from({ length: 18 }, () => ''))
const seedError = ref('')

const updateValidity = () => {
    const words = seedWords.value.filter((w) => w.trim())
    const isValid = validateSeedPhrase18(words) && !seedError.value
    emit('update:valid', isValid)
}

const inputBoxFocus = (idx: number) => {
    document.getElementById('input-' + idx)?.focus()
}

const handleGridInput = (index: number, value: string) => {
    seedWords.value[index] = value
    updateValidity()
}

const handlePaste = (event: ClipboardEvent) => {
    event.preventDefault()
    const pastedData = event.clipboardData?.getData('text') || ''
    const words = pastedData
        .trim()
        .split(/\s+/)
        .filter((w) => w)

    if (words.length === 0) return

    seedWords.value = words
    seedError.value = ''
    updateValidity()
}

const validateSeed = () => {
    const words = seedWords.value.filter((w) => w.trim())
    return validateSeedPhrase18(words)
}

const handleSubmit = () => {
    if (validateSeed()) seedError.value = ''
    emit(
        'submit',
        seedWords.value.filter((w) => w.trim())
    )

    updateValidity()
}

defineExpose({
    handleSubmit,
})
</script>

<template>
    <div class="seed-phrase-tab">
        <div class="seed-row-radio">
            <div class="radio active">18 words</div>
        </div>

        <!-- Individual input grid -->
        <div class="seed-inputs">
            <div class="seed-input-grid">
                <div v-for="(word, i) in seedWords" :key="i" class="seed-box">
                    <input
                        :id="'input-' + i"
                        type="text"
                        inputmode="text"
                        autocapitalize="off"
                        autocomplete="off"
                        spellcheck="false"
                        :value="word"
                        :placeholder="i + 1 + '.'"
                        maxlength="24"
                        @keydown.enter="inputBoxFocus(i + 1)"
                        :class="{ error: seedError && !word.trim() }"
                        @focus="seedError = ''"
                        @input="handleGridInput(i, ($event.target as HTMLInputElement).value)"
                        @paste="handlePaste($event)"
                    />
                </div>
            </div>
        </div>
        <div v-if="seedError" class="error-text">{{ seedError }}</div>
    </div>
</template>

<style lang="scss" scoped>
.seed-phrase-tab {
    .seed-row-radio {
        display: flex;
        gap: 24px;
        align-items: center;
        margin-bottom: 16px;
        .radio {
            background: var(--bg-secondary);
            color: var(--text-secondary);
            border-radius: 14px;
            font-size: 1.08rem;
            padding: 7px 24px 7px 18px;
            font-weight: 500;
            display: flex;
            align-items: center;
            gap: 5px;
            &.active {
                background: var(--primary-color);
                color: var(--text-light);
            }
            opacity: 1;
        }
    }
    .seed-inputs {
        width: 100%;
    }
    .seed-input-grid {
        display: grid;
        grid-template-columns: repeat(4, 1fr);
        gap: 12px;
    }
    .seed-box input {
        width: 100%;
        padding: 8px 12px;
        border-radius: 9px;
        border: 2px solid var(--border-color);
        background: var(--bg-secondary);
        color: var(--text-secondary);
        outline: none;
        font-size: 15px;
        transition:
            border 0.16s,
            box-shadow 0.16s;
        &:focus {
            border-color: var(--primary-color);
            box-shadow: 0 2px 8px var(--shadow-primary);
        }
        &.error {
            border-color: var(--error-color);
            background: var(--bg-secondary);
            color: var(--text-light);
        }
    }
    .error-text {
        color: var(--error-color);
        font-size: 0.97em;
        margin-top: 8px;
    }
}

@include screen(mobile) {
    .seed-input-grid {
        gap: 8px;
        grid-template-columns: repeat(3, 1fr);
    }
}
</style>
