export function normalizeText(value) {
  return String(value ?? '').trim()
}

export function compactText(value) {
  return normalizeText(value).replace(/\s+/g, '')
}

export function normalizeDateText(value) {
  const text = normalizeText(value)
  const matched = text.match(/^(\d{4})[-/.年](\d{1,2})[-/.月](\d{1,2})/)
  if (!matched) {
    return ''
  }
  return [
    matched[1],
    String(matched[2]).padStart(2, '0'),
    String(matched[3]).padStart(2, '0')
  ].join('-')
}

export function parseDate(value) {
  const text = normalizeDateText(value)
  if (!text) {
    return null
  }
  const date = new Date(`${text}T00:00:00Z`)
  return Number.isNaN(date.getTime()) ? null : date
}

export function formatDate(date) {
  return date.toISOString().slice(0, 10)
}
