public function writeRow(iterable $row): void
    {
        // Convert to bag here in case $row cannot be rewound
        $row = Bag::from($row);

        if ($row->isIndexed()) {
            $this->writer->writeRow($row);

            return;
        }

        // If headers have not been set, use the keys of the given row
        if ($this->headers === null) {
            $this->headers = $row->keys();
        }

        $extra = $row->omit(...$this->headers);
        if (!$extra->isEmpty()) {
            throw new \RuntimeException(
                sprintf('Row contains extra fields not found in headers: "%s"', $extra->keys()->join('", "'))
            );
        }

        // Correctly sort values based on headers
        $row = $this->headers
            ->flip()
            ->replace($row)
            ->values()
        ;

        $this->writer->writeRow($row);
    }