<script setup lang="ts">
import { ref, computed, onMounted, onUnmounted } from 'vue';
import { useI18n } from 'vue-i18n';
import LocaleFlag from './LocaleFlag.vue';
import { useAppLocale } from '../composables/useAppLocale';

withDefaults(
  defineProps<{
    compact?: boolean;
  }>(),
  { compact: false },
);

const { locale } = useI18n();
const { locales, setLocale } = useAppLocale();

const open = ref(false);
const root = ref<HTMLElement | null>(null);

const currentLabel = computed(
  () => locales.find((l) => l.code === locale.value)?.label ?? locale.value,
);

async function pick(code: string) {
  open.value = false;
  await setLocale(code);
}

function toggle() {
  open.value = !open.value;
}

function onOutsideClick(e: Event) {
  if (!root.value?.contains(e.target as Node)) open.value = false;
}

onMounted(() => {
  document.addEventListener('click', onOutsideClick);
  document.addEventListener('touchend', onOutsideClick);
});
onUnmounted(() => {
  document.removeEventListener('click', onOutsideClick);
  document.removeEventListener('touchend', onOutsideClick);
});
</script>

<template>
  <div ref="root" class="locale-switch" :class="{ compact, open }">
    <button type="button" class="locale-trigger" :aria-expanded="open" aria-haspopup="listbox" @click.stop="toggle">
      <LocaleFlag :locale="locale" :size="compact ? 16 : 18" />
    </button>    <ul v-show="open" class="locale-menu" role="listbox" :aria-label="compact ? 'Language' : undefined">
      <li
        v-for="l in locales"
        :key="l.code"
        role="option"
        :aria-selected="locale === l.code"
        class="locale-option"
        :class="{ active: locale === l.code }"
        @click="pick(l.code)"
      >
        <LocaleFlag :locale="l.code" :size="16" />
        <span>{{ l.label }}</span>
      </li>
    </ul>
  </div>
</template>

<style scoped>
.locale-switch {
  position: relative;
  display: inline-flex;
  flex-shrink: 0;
}

.locale-trigger {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg-body);
  color: var(--primary-light);
  font-size: 11px;
  font-weight: 700;
  font-family: inherit;
  cursor: pointer;
  box-sizing: border-box;
  flex-shrink: 0;
}

.locale-switch.compact .locale-trigger {
  width: 30px;
  height: 30px;
}

.locale-menu {
  position: absolute;
  top: calc(100% + 4px);
  right: 0;
  z-index: 50;
  min-width: 100%;
  margin: 0;
  padding: 4px;
  list-style: none;
  background: var(--dropdown-bg);
  backdrop-filter: none;
  -webkit-backdrop-filter: none;
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.locale-option {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-radius: 6px;
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}

.locale-option:hover,
.locale-option.active {
  background: rgba(255, 255, 255, 0.08);
  color: var(--primary-light);
}
</style>
