#include "tile/base/buffer/compression_output_stream.h"

namespace tile {

NoncontiguousBufferCompressionOutputStream::NoncontiguousBufferCompressionOutputStream(
    NoncontiguousBufferBuilder *builder)
    : builder_(builder)
{}

NoncontiguousBufferCompressionOutputStream::~NoncontiguousBufferCompressionOutputStream() { Flush(); }

void
NoncontiguousBufferCompressionOutputStream::Flush()
{
    if (using_bytes_ > 0) {
        builder_->MarkWritten(using_bytes_);
        using_bytes_ = 0;
    }
}

bool
NoncontiguousBufferCompressionOutputStream::Next(void **data, std::size_t *size) noexcept
{
    if (!builder_) { return false; }

    if (using_bytes_) { builder_->MarkWritten(using_bytes_); }

    *data        = builder_->data();
    *size        = builder_->SizeAvailable();
    using_bytes_ = *size;
    TILE_CHECK(*size > 0);
    return true;
}

void
NoncontiguousBufferCompressionOutputStream::BackUp(std::size_t count) noexcept
{
    using_bytes_ -= count;
}
}// namespace tile
