<script setup lang="ts">
import { ref, watch, computed, onMounted, unref } from 'vue'
import { useAppStore } from '../../../store/modules/app'
import { useDesign } from '../../../hooks/web/useDesign'
import { getTextScaleStyle } from '../../../utils/domUtils'

const { getPrefixCls } = useDesign()

const prefixCls = getPrefixCls('logo')

const appStore = useAppStore()

const show = ref(true)

// 文字以 · 为分隔符 开显示多行文本
const title1 = computed(() => appStore.getTitle || '').value.split('·')[0]
const title2 = computed(() => appStore.getTitle || '').value.split('·')[1]

const layout = computed(() => appStore.getLayout)

const collapse = computed(() => appStore.getCollapse)

onMounted(() => {
  if (unref(collapse)) show.value = false
})

watch(
  () => collapse.value,
  (collapse: boolean) => {
    if (unref(layout) === 'topLeft' || unref(layout) === 'cutMenu') {
      show.value = true
      return
    }
    show.value = !collapse
  }
)

watch(
  () => layout.value,
  (layout) => {
    if (layout === 'top' || layout === 'cutMenu') {
      show.value = true
    } else {
      if (unref(collapse)) {
        show.value = false
      } else {
        show.value = true
      }
    }
  }
)
</script>

<template>
  <div>
    <router-link
      :class="[
        prefixCls,
        layout !== 'classic' ? `${prefixCls}__Top` : '',
        'flex !h-[var(--logo-height)] items-center cursor-pointer pl-8px relative decoration-none overflow-hidden text-inherit'
      ]"
      to="/"
    >
      <img src="@common/src/assets/imgs/logo.png" class="w-34px h-42px" />
      <div
        v-if="show"
        :title="title1"
        :class="[
          'ml-8px pt-4px text-16px font-700 overflow-hidden overflow-ellipsis whitespace-nowrap flex-1',
          {
            'text-[var(--logo-title-text-color)]': layout === 'classic',
            'text-[var(--top-header-text-color)]':
              layout === 'topLeft' || layout === 'top' || layout === 'cutMenu'
          }
        ]"
      >
        <div :style="getTextScaleStyle(title1, 16, 160)"> {{ title1 }}</div>
        <!-- 第二行文字-->
        <div class="mt4px leading-none" v-if="title2">
          <el-text
            class="!text-inherit"
            :truncated="true"
            :style="getTextScaleStyle(title2, 14, 160)"
            >{{ title2 }}</el-text
          >
        </div>
      </div>
    </router-link>
  </div>
</template>
