/*
 * Copyright © 2021-2026 Pierrick Caillon <pierrick.caillon+zipdirfs@megami.fr>
 */
#ifndef ZIPDIRFS_COMPONENTS_CHANGEDSTART_H
#define ZIPDIRFS_COMPONENTS_CHANGEDSTART_H

#include "ZipDirFs/Components/ChangedProxy.h"
#include "ZipDirFs/Containers/Helpers/Changed.h"
#include <functional>
#include <memory>
#include <mutex>

namespace ZipDirFs::Components
{
	/**
	 * @brief A @link ZipDirFs::Container::Helpers::Changed Changed @endlink helper for initial state
	 * @remarks Enables access to real file time before using the final @link ZipDirFs::Container::Helpers::Changed Changed @endlink helper.
	 * @authors Pierrick Caillon <pierrick.caillon+zipdirfs@megami.fr>
	 */
	class ChangedStart : public Containers::Helpers::Changed
	{
	public:
		ChangedStart(std::function<FileTime()>&&,
			std::function<std::unique_ptr<Changed>()>&&,
			std::shared_ptr<ChangedProxy>&);
		bool operator()() override;
		operator FileTime() const override;

	protected:
		std::mutex access;
		FileTime lastChanged;
		bool called;

	private:
		std::function<FileTime()> firstValue;
		std::function<std::unique_ptr<Changed>()> buildReal;
		std::shared_ptr<ChangedProxy>& target;
	};
} // namespace ZipDirFs::Components

#endif // ZIPDIRFS_COMPONENTS_CHANGEDSTART_H
