<template>
  <view class="sidebar-toggle-wrap">
    <uni-tooltip :content="toggleLabel" placement="right">
      <view class="sidebar-toggle-btn" @click="toggleLeftSidebar">
        <view class="sidebar-toggle-icon" :class="{ collapsed: isCollapsed }">
          <span class="toggle-bar toggle-bar-top"></span>
          <span class="toggle-bar toggle-bar-mid">
            <span class="toggle-arrow"></span>
          </span>
          <span class="toggle-bar toggle-bar-bottom"></span>
        </view>
      </view>
    </uni-tooltip>
  </view>
</template>

<script setup lang="ts">
import { computed, onMounted } from 'vue'
import { useI18n } from 'vue-i18n'
import { useLeftSidebarCollapse } from '@/composables/useLeftSidebarCollapse'

const { t } = useI18n()
const { toggleLeftSidebar, syncCollapsedFromDom, isCollapsed } = useLeftSidebarCollapse()

const toggleLabel = computed(() =>
  isCollapsed.value ? t('Home.msg.sidebarExpand') : t('Home.msg.sidebarCollapse')
)

onMounted(() => {
  syncCollapsedFromDom()
})
</script>

<style scoped lang="scss">
@import "@/uni.scss";

.sidebar-toggle-wrap {
  :deep(.uni-tooltip-popup) {
    width: auto !important;
    padding: 4px 8px;
    white-space: nowrap;
    line-height: 1.4;
  }
}

.sidebar-toggle-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: px2rpx(36);
  height: px2rpx(36);
  cursor: pointer;
  flex-shrink: 0;
  background: transparent;
  border: none;
}

.sidebar-toggle-icon {
  width: 18px;
  height: 14px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;

  .toggle-bar {
    display: block;
    height: 2px;
    background-color: #97A1C0;
    border-radius: 2px;
    transition: width 0.28s cubic-bezier(0.4, 0, 0.2, 1),
      margin 0.28s cubic-bezier(0.4, 0, 0.2, 1),
      transform 0.28s cubic-bezier(0.4, 0, 0.2, 1);
  }

  .toggle-bar-top,
  .toggle-bar-bottom {
    width: 100%;
  }

  .toggle-bar-mid {
    position: relative;
    width: 100%;
    margin-left: 0;
  }

  .toggle-arrow {
    position: absolute;
    left: 0;
    top: 50%;
    width: 100%;
    height: 2px;
    background-color: #97A1C0;
    border-radius: 2px;
    transform: translateY(-50%);
    transform-origin: left center;
    transition: width 0.28s cubic-bezier(0.4, 0, 0.2, 1),
      left 0.28s cubic-bezier(0.4, 0, 0.2, 1),
      transform 0.28s cubic-bezier(0.4, 0, 0.2, 1);

    &::before {
      content: '';
      position: absolute;
      top: 50%;
      width: 6px;
      height: 6px;
      border-left: 2px solid #97A1C0;
      border-bottom: 2px solid #97A1C0;
      transform: translateY(-50%) rotate(45deg);
      transform-origin: center;
      transition: transform 0.28s cubic-bezier(0.4, 0, 0.2, 1),
        left 0.28s cubic-bezier(0.4, 0, 0.2, 1),
        right 0.28s cubic-bezier(0.4, 0, 0.2, 1),
        opacity 0.28s cubic-bezier(0.4, 0, 0.2, 1);
    }
  }

  &:not(.collapsed) {
    .toggle-bar-mid {
      width: 100%;
      margin-left: 0;
    }

    .toggle-arrow {
      padding-left: 8px;
      box-sizing: border-box;

      &::before {
        left: 0;
        right: auto;
        opacity: 1;
        transform: translateY(-50%) rotate(45deg);
      }
    }
  }

  &.collapsed {
    .toggle-bar-mid {
      width: 55%;
      margin-left: 45%;
    }

    .toggle-arrow {
      padding-left: 0;

      &::before {
        left: auto;
        right: 0;
        opacity: 1;
        transform: translateY(-50%) rotate(-135deg);
      }
    }
  }
}
</style>
