<script setup lang="ts">
import { computed } from 'vue'
import { useAgentsStore, useSelectionStore } from '@/stores'

const agentsStore = useAgentsStore()
const selectionStore = useSelectionStore()
const emit = defineEmits<{
  (e: 'click'): void
}>()

// 获取分支数量
const branchCount = computed(() => {
  const extraBranches = selectionStore.flowBranches?.length || 1
  return extraBranches
})

const handleClick = () => {
  emit('click')
  agentsStore.openPlanModification()
}
</script>
<template>
  <div
    class="branch-button"
    :class="{ 'has-branches': branchCount > 1 }"
    @click="handleClick"
    :title="`${branchCount} 个分支`"
  >
    <!-- 分支图标 -->
    <svg-icon icon-class="branch" size="24px" class="branch-icon" />

    <!-- 分支数量显示 -->
    <span class="branch-count">
      {{ branchCount }}
    </span>
  </div>
</template>

<style scoped lang="scss">
.branch-button {
  position: absolute;
  right: 0;
  bottom: 0;

  /* 尺寸 */
  width: 36px;
  height: 32px;

  /* 样式 */
  background-color: #43a8aa;
  border-radius: 10px 0 0 0;
  cursor: pointer;
  user-select: none;
  z-index: 100;

  /* 布局 */
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;

  /* 交互 */
  transition: all 0.2s ease;

  &:hover {
    filter: brightness(0.9);
  }

  &.has-branches::after {
    content: '';
    position: absolute;
    top: -2px;
    right: -2px;
    width: 8px;
    height: 8px;
    background: #ff6b6b;
    border-radius: 50%;
    border: 2px solid white;
  }
}

.branch-icon {
  color: white;
}

.branch-count {
  position: absolute;
  right: 4px;
  bottom: 2px;
  font-size: 12px;
  color: white;
  font-weight: 800;
  text-align: right;
  line-height: 1;
}
</style>
