#pragma once

#include "tl/ptr.h"
#include "fs/IMapSink.h"
#include "fs/AbsPath.h"
#include "tl/flag_set2.h"
#include "fs/Mode.h"
#include "fs/Api.h"

namespace fs
{

class NativeFilesystem;

class FS_API FileMapSink final : public IMapSink
{
	friend class NativeFilesystem;
public:
	FileMapSink(AbsPathView filepath, size_t size, Mode mode = Mode::CreateOrOpenAndClear, Flags flags = Flags());
	~FileMapSink() override;

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

	size_t write(tl::span<const uint8_t> data) override;
	void seekBeg(uint64_t offset) override;
	void seekRel(int64_t offset) override;
	uint64_t tell() const override;
	uint64_t getSize() const override;
	tl::span<uint8_t> map(size_t size) override;
	size_t getPreferredBufferSize() const override;

private:
	uint64_t	m_offset = 0;
	mutable tl::optional<Error> m_optLastError;

	struct Impl;
	tl::unique_ptr<Impl> m_impl;
};

}

