<script setup lang="ts">
import { watch } from 'vue';
import { useI18n } from 'vue-i18n';
import BetSlipPanel from './BetSlipPanel.vue';
import { useBetSlipStore } from '../../stores/betSlip';

const { t } = useI18n();
const slip = useBetSlipStore();

watch(
  () => slip.totalCount,
  (next, prev) => {
    if (next > prev) slip.expandPanel();
  },
);
</script>

<template>
  <aside
    class="desktop-betslip-rail"
    :class="{ 'is-collapsed': !slip.panelExpanded }"
    :aria-expanded="slip.panelExpanded"
  >
    <button
      type="button"
      class="rail-toggle"
      :class="{ 'is-collapsed': !slip.panelExpanded }"
      :aria-label="slip.panelExpanded ? t('common.collapse') : t('common.expand')"
      @click="slip.togglePanelExpanded()"
    >
      <svg class="rail-toggle-icon" viewBox="0 0 24 24" fill="none" aria-hidden="true">
        <path
          d="M10 8l4 4-4 4"
          stroke="currentColor"
          stroke-width="2"
          stroke-linecap="round"
          stroke-linejoin="round"
        />
      </svg>
    </button>

    <button
      v-if="!slip.panelExpanded"
      type="button"
      class="rail-collapsed-hint"
      :aria-label="t('bet.bet_slip')"
      @click="slip.expandPanel()"
    >
      <span v-if="slip.totalCount" class="rail-badge">{{ slip.totalCount > 99 ? '99+' : slip.totalCount }}</span>
      <span class="rail-label">{{ t('bet.bet_slip') }}</span>
    </button>

    <div class="rail-content" :hidden="!slip.panelExpanded">
      <BetSlipPanel />
    </div>
  </aside>
</template>

<style scoped>
.desktop-betslip-rail {
  position: relative;
  flex-shrink: 0;
  width: var(--desktop-right-betslip-w);
  min-width: 0;
  border-left: 1px solid var(--border);
  background: var(--bg-card);
  display: flex;
  flex-direction: column;
  min-height: 0;
  transition: width 0.28s cubic-bezier(0.4, 0, 0.2, 1);
  overflow: visible;
}

.rail-toggle {
  position: absolute;
  left: -18px;
  top: 50%;
  z-index: 30;
  width: 36px;
  height: 36px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 50%;
  background: var(--bg-card);
  color: var(--primary-light);
  cursor: pointer;
  display: grid;
  place-items: center;
  transform: translateY(-50%);
  box-shadow:
    0 2px 10px rgba(0, 61, 107, 0.12),
    0 0 0 3px rgba(255, 255, 255, 0.92);
  transition:
    transform 0.22s cubic-bezier(0.4, 0, 0.2, 1),
    background 0.2s,
    color 0.2s,
    border-color 0.2s,
    box-shadow 0.2s;
}

.rail-toggle-icon {
  width: 16px;
  height: 16px;
  transition: transform 0.22s cubic-bezier(0.4, 0, 0.2, 1);
}

.rail-toggle.is-collapsed .rail-toggle-icon {
  transform: rotate(180deg);
}

.rail-toggle:hover {
  border-color: var(--border-active);
  background: var(--primary);
  color: #fff;
  box-shadow:
    0 4px 14px rgba(0, 61, 107, 0.2),
    0 0 0 3px rgba(0, 102, 204, 0.1);
}

.rail-toggle:active {
  transform: translateY(-50%) scale(0.94);
}

.rail-toggle:focus-visible {
  outline: 2px solid var(--border-active);
  outline-offset: 2px;
}

.rail-content {
  flex: 1;
  min-height: 0;
  width: var(--desktop-right-betslip-expanded-w, 288px);
  overflow: hidden;
}

.desktop-betslip-rail.is-collapsed .rail-content {
  visibility: hidden;
}

.rail-collapsed-hint {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 16px 0;
  border: none;
  background: transparent;
  cursor: pointer;
  transition: background 0.2s;
}

.rail-collapsed-hint:hover {
  background: var(--bg-hover);
}

.rail-label {
  writing-mode: vertical-rl;
  text-orientation: mixed;
  font-size: 12px;
  font-weight: 800;
  color: var(--primary);
  letter-spacing: 0.12em;
}

.rail-badge {
  min-width: 20px;
  height: 20px;
  padding: 0 5px;
  border-radius: 10px;
  background: var(--odds-accent, #F8971F);
  color: #fff;
  font-size: 10px;
  font-weight: 800;
  line-height: 20px;
  text-align: center;
}
</style>
