```ts
/** A sectioned sales report: item lines, subtotals, and a ranked summary. */

function ranked(a: [string, number, number], b: [string, number, number]): number {
  if (a[2] !== b[2]) {
    return b[2] - a[2];
  }
  return a[0] < b[0] ? -1 : 1;
}

export function sectionReport(rows: [string, string, number][]): { lines: [string, string, string, number][]; sections: [string, number, number][]; grand: number } {
  if (!Array.isArray(rows) || rows.length === 0) {
    throw new Error("Invalid input: rows must be a non-empty array");
  }

  const lines: [string, string, string, number][] = [];
  const sections: [string, number, number][] = [];
  let current = "";
  let subtotal = 0;
  let count = 0;
  let grand = 0;

  for (const [section, label, amount] of rows) {
    if (!Array.isArray(section) || section.length !== 3) {
      throw new Error("Invalid input: each row must be a three-item list");
    }

    if (current !== "" && section[0] !== current) {
      lines.push(["section", current, "", subtotal]);
      sections.push([current, count, subtotal]);
      count = 0;
    }
    current = section[0];
    lines.push(["item", section[0], label, amount]);
    subtotal += amount;
    count += 1;
    grand += amount;
  }

  if (current !== "") {
    lines.push(["section", current, "", subtotal]);
    sections.push([current, count, subtotal]);
  }

  lines.push(["grand", "", "", grand]);
  sections.sort(ranked);
  return { lines, sections, grand };
}
```