/**
 * State provisions, in the shape they are actually written (Nash, 2026-09-25).
 *
 * The permit provider publishes each state's rules as six topics, and inside a
 * topic the content is not prose — it is a set of headed groups, and inside a
 * group either a plain rule or a condition and what it requires: "Over 95 feet
 * — 1 escort", "Width: 16 feet on 4-lanes". Storing that as one paragraph per
 * topic (what the first version did) throws the structure away and leaves the
 * reader to find the number in a wall of text.
 *
 * So a topic is a list of sections, a section is an optional heading plus
 * items, and an item is a detail with an optional term. A section whose items
 * all carry a term renders as a two-column list; anything else renders as
 * bullets. That is the whole model, and it is what the provider's API will
 * fill when it is connected.
 */

export type StateInfoTopicKey =
  | 'travel'
  | 'escort'
  | 'permit_limits'
  | 'legal_limits'
  | 'signs'
  | 'superloads'

/** One rule. `term` is the condition or the field it applies to, when there is one. */
export interface StateInfoItem {
  term?: string
  detail: string
}

export interface StateInfoSection {
  heading?: string
  /** A line that introduces the items, e.g. "On 4 or more lane divided highways:". */
  note?: string
  items: StateInfoItem[]
}

export type StateProvisions = Record<StateInfoTopicKey, StateInfoSection[]>

export interface StateInfoTopicMeta {
  key: StateInfoTopicKey
  label: string
  /** What the topic answers, shown under the title so the reader knows why to open it. */
  blurb: string
}

/** The provider's order, kept so the two screens read the same way. */
export const STATE_INFO_TOPIC_META: StateInfoTopicMeta[] = [
  { key: 'travel', label: 'Travel', blurb: 'When this load may move' },
  { key: 'escort', label: 'Escorts', blurb: 'How many, and from what size' },
  { key: 'permit_limits', label: 'Permit limits', blurb: 'The most a routine permit covers' },
  { key: 'legal_limits', label: 'Legal limits', blurb: 'Above this you need a permit' },
  { key: 'signs', label: 'Signs & lights', blurb: 'Signs, flags and beacons' },
  { key: 'superloads', label: 'Superloads', blurb: 'Past routine, and what it costs in time' },
]

export const STATE_INFO_TOPIC_KEYS: StateInfoTopicKey[] = STATE_INFO_TOPIC_META.map((t) => t.key)

export function topicMeta(key: StateInfoTopicKey): StateInfoTopicMeta {
  return STATE_INFO_TOPIC_META.find((t) => t.key === key) ?? STATE_INFO_TOPIC_META[0]
}

/** The next / previous topic, or null at either end — the footer buttons never wrap around. */
export function adjacentTopic(key: StateInfoTopicKey, step: 1 | -1): StateInfoTopicKey | null {
  const i = STATE_INFO_TOPIC_KEYS.indexOf(key)
  const next = i + step
  return next >= 0 && next < STATE_INFO_TOPIC_KEYS.length ? STATE_INFO_TOPIC_KEYS[next] : null
}

/** A section reads as a two-column list only when every item names what it applies to. */
export function isTermList(section: StateInfoSection): boolean {
  return section.items.length > 0 && section.items.every((i) => !!i.term)
}

/** How many rules a topic holds — shown on the rail so an empty topic is obvious before opening it. */
export function topicRuleCount(sections: StateInfoSection[] | undefined): number {
  return (sections ?? []).reduce((n, s) => n + s.items.length, 0)
}
