import {
  FieldLabel,
  FormTooltip,
} from '@/app/FormFields'

export const LAST_PROJECT_KEY = 'ask-qien-last-project'
export const PRIORITIES = ['Critical', 'High', 'Medium', 'Low'] as const
export const SUCCESS_CRITERIA_MAX = 500

export function formatLabel(template: string, replacements: Record<string, string | number>) {
  return Object.entries(replacements).reduce(
    (text, [key, value]) => text.replaceAll(`{${key}}`, String(value)),
    template,
  )
}

export function RequiredSubmitLabel({
  htmlFor,
  children,
}: {
  htmlFor: string
  children: React.ReactNode
}) {
  return (
    <FieldLabel htmlFor={htmlFor}>
      {children}
      <span className="ml-1 text-red-600 dark:text-red-400" aria-hidden="true">*</span>
      <span className="sr-only"> required</span>
    </FieldLabel>
  )
}

export function RequiredSubmitLabelWithTooltip({
  htmlFor,
  children,
  tooltipId,
  tooltipText,
}: {
  htmlFor: string
  children: React.ReactNode
  tooltipId: string
  tooltipText: string
}) {
  return (
    <div className="flex items-center gap-2">
      <RequiredSubmitLabel htmlFor={htmlFor}>{children}</RequiredSubmitLabel>
      <FormTooltip id={tooltipId} text={tooltipText} />
    </div>
  )
}

export function getTodayDateInputValue() {
  const today = new Date()
  const year = today.getFullYear()
  const month = String(today.getMonth() + 1).padStart(2, '0')
  const day = String(today.getDate()).padStart(2, '0')

  return `${year}-${month}-${day}`
}
