const APPLICATION_SUBMIT_CONFIRM_TEXT_PATTERN = /^(确认|确定|确认提交|确定提交|提交|提交审批|确认审批|确认无误|核对无误|信息无误|无误|没问题|可以提交|确认进入审批|提交至审批流程|确认提交审批|同意提交)$/
const APPLICATION_SUBMIT_CANCEL_TEXT_PATTERN = /^(不|否|否定|取消|暂不|先不|不确认|不提交|再检查|再看看|等等|等一下)/
const STEWARD_RUNTIME_CONTINUE_TEXT_PATTERN = /^(继续|继续执行|下一步|继续下一步|开始下一步|处理下一项|继续处理|确认开始|确定开始|可以|好的|好|行)$/
const STEWARD_RUNTIME_CANCEL_TEXT_PATTERN = /^(取消|暂不|先不|不用|不要|不继续|不处理|先等等|等一下|停止|终止|算了)$/
const STEWARD_RUNTIME_NEW_TASK_MIN_LENGTH = 12
const STEWARD_RUNTIME_BUSINESS_HINT_PATTERN = /(申请|报销|出差|差旅|招待|交通费|住宿费|餐费|发票|票据|费用|预算|借款|付款|审批|审核)/
const STEWARD_RUNTIME_TIME_OR_ACTION_HINT_PATTERN = /(今天|明天|后天|昨天|前天|\d{1,2}月\d{1,2}日|\d{4}-\d{1,2}-\d{1,2}|我要|帮我|需要|创建|填写|处理|去|前往)/
const STEWARD_RUNTIME_CURRENT_CONTEXT_HINT_PATTERN = /(当前|这个|这一步|上面|上述|申请单|核对表|出行方式|交通方式|火车|高铁|动车|飞机|轮船|提交|审批|确认)/

export function isApplicationSubmitConfirmationText(value = '') {
  const normalized = String(value || '').trim()
  if (!normalized) {
    return ''
  }
  if (APPLICATION_SUBMIT_CONFIRM_TEXT_PATTERN.test(normalized)) {
    return 'confirm'
  }
  if (APPLICATION_SUBMIT_CANCEL_TEXT_PATTERN.test(normalized)) {
    return 'cancel'
  }
  return ''
}

export function normalizeStewardRuntimeInputText(value = '') {
  return String(value || '').trim().replace(/\s+/g, '')
}

export function isStewardRuntimeContinueText(value = '') {
  const normalized = normalizeStewardRuntimeInputText(value)
  return Boolean(normalized && STEWARD_RUNTIME_CONTINUE_TEXT_PATTERN.test(normalized))
}

export function isStewardRuntimeCancelText(value = '') {
  const normalized = normalizeStewardRuntimeInputText(value)
  return Boolean(normalized && STEWARD_RUNTIME_CANCEL_TEXT_PATTERN.test(normalized))
}

export function resolveStewardRuntimeTransportAlias(value = '') {
  const normalized = normalizeStewardRuntimeInputText(value)
  if (/^(火车|高铁|动车|train)$/.test(normalized)) return '火车'
  if (/^(飞机|航班|机票|flight)$/.test(normalized)) return '飞机'
  if (/^(轮船|船|ship|ferry)$/.test(normalized)) return '轮船'
  return ''
}

export function shouldPlanNewStewardTasksLocally(rawText = '', runtimeState = {}) {
  const text = String(rawText || '').trim()
  const normalized = normalizeStewardRuntimeInputText(text)
  if (
    !normalized ||
    normalized.length < STEWARD_RUNTIME_NEW_TASK_MIN_LENGTH ||
    isStewardRuntimeContinueText(normalized) ||
    isStewardRuntimeCancelText(normalized)
  ) {
    return false
  }
  if (!STEWARD_RUNTIME_BUSINESS_HINT_PATTERN.test(text) || !STEWARD_RUNTIME_TIME_OR_ACTION_HINT_PATTERN.test(text)) {
    return false
  }
  const waitingFor = String(runtimeState?.waiting_for || runtimeState?.waitingFor || '').trim()
  if (waitingFor && STEWARD_RUNTIME_CURRENT_CONTEXT_HINT_PATTERN.test(text)) {
    return false
  }
  return true
}
