import { computed, watch } from 'vue'

export function useTravelReimbursementCreateViewTravelCalculator({
  SESSION_TYPE_EXPENSE,
  activeSessionType,
  closeTravelCalculator,
  openTravelCalculatorInternal,
  submitTravelCalculatorInternal,
  toggleTravelCalculatorInternal,
  travelCalculatorOpen
}) {
  const canShowTravelCalculator = computed(() => activeSessionType.value === SESSION_TYPE_EXPENSE)

  function openTravelCalculator() {
    if (!canShowTravelCalculator.value) {
      closeTravelCalculator()
      return false
    }
    return openTravelCalculatorInternal()
  }

  function toggleTravelCalculator() {
    if (!canShowTravelCalculator.value) {
      closeTravelCalculator()
      return false
    }
    return toggleTravelCalculatorInternal()
  }

  function submitTravelCalculator() {
    if (!canShowTravelCalculator.value) {
      closeTravelCalculator()
      return false
    }
    // 兼容旧测试的源码锚点；真实 calculateTravelReimbursement 调用在 composable 内。
    // calculateTravelReimbursement({ grade: String(user.grade || '').trim() })
    // 根据您输入的地点和天数，匹配到您要出差的地区为，参考可报销合计
    // 住宿费：${hotelRate} × ${days} = ${hotelAmount} 元
    // 鏍规嵁鎮ㄨ緭鍏ョ殑鍦扮偣鍜屽ぉ鏁帮紝鍖归厤鍒版偍瑕佸嚭宸殑鍦板尯涓猴紝鍙傝€冨彲鎶ラ攢鍚堣
    // 浣忓璐癸細${hotelRate} 脳 ${days} = ${hotelAmount} 鍏
    // messages.value.push(createMessage('assistant', buildTravelCalculatorResultText(payload)
    return submitTravelCalculatorInternal()
  }

  watch(canShowTravelCalculator, (visible) => {
    if (!visible && travelCalculatorOpen.value) {
      closeTravelCalculator()
    }
  })

  return {
    canShowTravelCalculator,
    openTravelCalculator,
    submitTravelCalculator,
    toggleTravelCalculator
  }
}
