require 'recipecode'

ConditionInheriter = ConditionInheriter or {}

function ConditionInheriter.FromConditionInheriter(craftRecipeData, player)
    local items = craftRecipeData:getAllConsumedItems();
    local results = craftRecipeData:getAllCreatedItems()
    local box = items:get(0);

    for i = 0, results:size() - 1 do
        local resultItem = results:get(i);
        if box:getModData()["condition" .. i] then
            resultItem:setCondition(box:getModData()["condition" .. i])
        end
    end
end

function ConditionInheriter.ToConditionInheriter(craftRecipeData, player)
    local results = craftRecipeData:getAllCreatedItems()
    local items = craftRecipeData:getAllConsumedItems();

    local box = results:get(0);
    for i = 0, items:size() - 1 do
        local item = items:get(i);
        if item:getCondition() ~= item:getConditionMax() then
            box:getModData()["condition" .. i] = item:getCondition();
        end
    end
end

if type(Recipe) == "table" then
    if type(Recipe.OnCreate) ~= "table" then
        Recipe.OnCreate = {}
    end
    Recipe.OnCreate.FromConditionInheriter = ConditionInheriter.FromConditionInheriter
    Recipe.OnCreate.ToConditionInheriter = ConditionInheriter.ToConditionInheriter
end
