export const DEFAULT_PRIMARY_GROUPS = ['A', 'B', 'C', 'D']

export function normalizedGroupCode(value) {
  return String(value || '').trim().toUpperCase()
}

export function parseGroupList(raw) {
  const items = String(raw || '')
    .split(',')
    .map((item) => normalizedGroupCode(item))
    .filter(Boolean)
  return [...new Set(items)]
}

export function loadPrimaryGroups(storage) {
  const stored = storage.getItem('shooting_group_list')
  const parsed = parseGroupList(stored)
  return parsed.length > 0 ? parsed : DEFAULT_PRIMARY_GROUPS
}

export function groupKey(code) {
  const normalized = normalizedGroupCode(code)
  if (normalized.startsWith('A')) return 'a'
  if (normalized.startsWith('B')) return 'b'
  if (normalized.startsWith('C')) return 'c'
  if (normalized.startsWith('D')) return 'd'
  return 'u'
}
