#include "utils.hpp"
#include "logger.hpp"

namespace simple_object_storage
{

    ScopeFileDeleter::ScopeFileDeleter(const std::filesystem::path &path) : path_(path), released_(false) {}

    ScopeFileDeleter::~ScopeFileDeleter()
    {
        if (!released_)
        {
            try
            {
                if (std::filesystem::exists(path_))
                {
                    std::filesystem::remove(path_);
                }
            }
            catch (const std::filesystem::filesystem_error &e)
            {
                LOG_ERROR("Error deleting temp file: {} - {}", path_.string(), e.what());
            }
        }
    }

    void ScopeFileDeleter::release() { released_ = true; }

    std::string trim(const std::string &s)
    {
        size_t start = s.find_first_not_of(" \t\n\r\f\v");
        if (start == std::string::npos)
            return "";
        size_t end = s.find_last_not_of(" \t\n\r\f\v");
        return s.substr(start, end - start + 1);
    }

}
