#include "BuildingTarget.h"

#include "FactoryQueries.h"
#include "SelectionContext.h"
#include "Simulation.h"

BuildingTarget resolveBuildingTarget(const SelectionContext& context, BuildingId id)
{
    BuildingTarget target;
    target.building = findBuilding(context.sim->getFactoryState(), id);
    target.site     = target.building
        ? nullptr
        : findSite(context.sim->getFactoryState(), id);
    if (!target.isValid())
    {
        return target;
    }

    target.type       = target.building ? target.building->type       : target.site->type;
    target.recipeId   = target.building ? target.building->recipeId   : target.site->recipeId;
    target.shipLayout = target.building ? target.building->shipLayout : target.site->shipLayout;
    target.anchor     = target.building ? target.building->anchor     : target.site->anchor;
    return target;
}

std::optional<BuildingId> asConstructionSite(const SelectionContext& context,
                                             BuildingId id)
{
    if (findSite(context.sim->getFactoryState(), id) != nullptr)
    {
        return id;
    }
    return std::nullopt;
}
