/*
 * Copyright 2023 - 2025 Gary Talent (gary@drinkingtea.net). All rights reserved.
 */

#include "commands.hpp"
#include "addobject.hpp"

namespace jasper::world {

AddObject::AddObject(WorldObjectSet &doc) noexcept:
		m_doc(doc) {
	m_obj.id = m_doc.objIdIdx + 1;
	m_obj.name = ox::sfmt("Object {}", m_obj.id);
	for (auto const &o : m_doc.objects) {
		if (o.name > m_obj.name) {
			break;
		}
		++m_insertIdx;
	}
}

ox::Error AddObject::redo() noexcept {
	++m_doc.objIdIdx;
	m_doc.objects.emplace(m_insertIdx, std::move(m_obj));
	return {};
}

ox::Error AddObject::undo() noexcept {
	m_obj = std::move(m_doc.objects[m_insertIdx]);
	OX_RETURN_ERROR(m_doc.objects.erase(m_insertIdx));
	--m_doc.objIdIdx;
	return {};
}

int AddObject::commandId() const noexcept {
	return static_cast<int>(WorldObjCommand::AddObject);
}

}
