<script setup lang="ts">
import { ref, watch } from "vue";

const emit = defineEmits<{ search: [query: string] }>();
const query = ref("");
const inputRef = ref<HTMLInputElement | null>(null);
let timeout: ReturnType<typeof setTimeout>;

watch(query, (val) => {
  clearTimeout(timeout);
  timeout = setTimeout(() => emit("search", val), 300);
});

defineExpose({ focus: () => inputRef.value?.focus() });
</script>

<template>
  <input
    ref="inputRef"
    v-model="query"
    type="text"
    placeholder="Search notes..."
    class="search-input"
  />
</template>

<style scoped>
.search-input {
  width: 100%;
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--color-input-border);
  border-radius: var(--radius-sm);
  font-size: 1rem;
  box-sizing: border-box;
  background: var(--color-bg-card);
  color: var(--color-text);
}
.search-input::placeholder {
  color: var(--color-text-muted);
}
</style>
