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

namespace fs
{

class FS_API StreamSourceToMapSourceAdapter final : public IMapSource
{
public:
	explicit StreamSourceToMapSourceAdapter(tl::unique_ref<IStreamSource> i_srcSource);
	~StreamSourceToMapSourceAdapter() override = default;

	tl::optional<Error> getLastError() const override;

	size_t read(tl::span<uint8_t> data) override;

	void seekBeg(uint64_t i_offset) override;
	void seekRel(int64_t i_offset) override;

	uint64_t tell() const override;
	uint64_t getSize() const override;

	bool isEOS() const override;

	size_t getPreferredBufferSize() const override;

	tl::span<const uint8_t> map(size_t size) override;

private:
	void readAll() const;

	tl::unique_ref<IStreamSource> m_srcSource;
	mutable tl::optional<Error> m_optLastError;
	mutable tl::memory_buffer m_data;
	mutable bool m_readAll = false;
	uint64_t	m_offset = 0;
};

}
