<template>
  <div class="mobile-header">
    <SideBarLogo
      :logo-url="tenantMake.logo_url"
      :make-name="tenantMake.name"
      class="mobile-logo"
    />

    <LanguageSelector
      :initial-language="initialLanguage"
      :current-language="currentLanguage"
      :supported-language-details="supportedLanguageDetails"
      :allowed-languages="allowedLanguages"
      @language-changed="handleLanguageChange"
      class="mobile-language-selector"
    />
  </div>
</template>

<script setup>
import { ref } from 'vue';
import SideBarLogo from './SideBarLogo.vue';
import LanguageSelector from './LanguageSelector.vue';

const props = defineProps({
  tenantMake: {
    type: Object,
    default: () => ({
      name: '',
      logo_url: '',
      subtitle: ''
    })
  },
  initialLanguage: {
    type: String,
    default: 'en'
  },
  supportedLanguageDetails: {
    type: Object,
    default: () => ({})
  },
  allowedLanguages: {
    type: Array,
    default: () => ['nl', 'en', 'fr', 'de']
  },
  apiPrefix: {
    type: String,
    default: ''
  }
});

const emit = defineEmits(['language-changed']);

const currentLanguage = ref(props.initialLanguage);

const handleLanguageChange = (newLanguage) => {
  currentLanguage.value = newLanguage;

  // Emit to parent
  emit('language-changed', newLanguage);

  // Global event for backward compatibility
  const globalEvent = new CustomEvent('language-changed', {
    detail: { language: newLanguage }
  });
  document.dispatchEvent(globalEvent);

  // Update chatConfig
  if (window.chatConfig) {
    window.chatConfig.language = newLanguage;
  }

  // Save preference
  localStorage.setItem('preferredLanguage', newLanguage);
};
</script>

<style scoped>
.mobile-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;             /* allow wrapping to next line on narrow screens */
  padding: 10px 15px;
  background: var(--sidebar-background);
  color: var(--sidebar-color);
  border-bottom: 1px solid rgba(0,0,0,0.1);
  min-height: 60px;
  max-width: 100%;            /* never exceed viewport width */
  overflow: hidden;           /* clip any accidental overflow */
}

/* Mobile logo container - meer specifieke styling */
.mobile-logo {
  flex-shrink: 1;             /* allow logo area to shrink */
  min-width: 0;               /* allow shrinking below intrinsic width */
  display: flex !important;
  align-items: center;
  justify-content: flex-start;
  height: 50px; /* Vaste hoogte voor consistentie */
}

/* Diepere styling voor het logo component */
.mobile-logo :deep(.sidebar-logo) {
  padding: 0 !important;
  margin: 0 !important;
  display: flex !important;
  align-items: center !important;
  justify-content: flex-start !important;
  height: 100% !important;
  min-width: 0 !important;    /* allow inner content to shrink */
}

.mobile-logo :deep(.logo-image) {
  max-height: 40px !important;
  max-width: 120px !important;
  height: auto !important;
  width: auto !important;
  object-fit: contain !important;
  display: block !important;
}

.mobile-logo :deep(.logo-placeholder) {
  width: 40px !important;
  height: 40px !important;
  font-size: 1rem !important;
  display: flex !important;
  align-items: center !important;
  justify-content: center !important;
}

/* Mobile language selector styling */
.mobile-language-selector {
  flex-shrink: 1;
  min-width: 0;               /* allow selector area to shrink */
}

.mobile-language-selector :deep(.language-selector) {
  margin: 0;
}

.mobile-language-selector :deep(label) {
  display: none; /* Hide label in mobile header */
}

.mobile-language-selector :deep(.language-select) {
  padding: 6px 10px;
  font-size: 0.85rem;
  min-width: 0;               /* allow the select to shrink */
  max-width: 100%;            /* never exceed container width */
  margin: 0;
}

/* Extra constraints on ultra-small screens */
@media (max-width: 360px) {
  .mobile-language-selector :deep(.language-select) {
    max-width: 60vw;          /* avoid pushing beyond viewport */
  }
}

/* Media queries voor responsiviteit */
@media (max-width: 768px) {
  .mobile-header {
    display: flex;
  }
}

@media (min-width: 769px) {
  .mobile-header {
    display: none;
  }
}
</style>