#pragma once
#include "fs/IStreamSource.h"
#include "tl/memory_buffer.h"
#include "fs/Api.h"
#include "tl/ptr.h"

namespace fs
{

class FS_API DeflateStreamSource final : public IStreamSource
{
public:
	explicit DeflateStreamSource(IStreamSource& source); //non-owning
	explicit DeflateStreamSource(tl::unique_ref<IStreamSource> source); //owning
	~DeflateStreamSource() override;

	tl::optional<Error> getLastError() const override;
	size_t read(tl::span<uint8_t> data) override;

	bool isEOS() const override;

	size_t getPreferredBufferSize() const override;

	tl::unique_ref<IStreamSource> extract();

private:
	tl::unique_ptr<IStreamSource> m_ownedSource;
	IStreamSource& m_source;

	mutable tl::optional<Error> m_optLastError;

	void initializeDeflate();

	struct ZLib;
	tl::unique_ptr<ZLib> m_zlib;

	tl::memory_buffer m_buffer;
};


}
